Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 5. Customizing existing Kamelet catalog
This document guides you through customizing an existing kamelet. It involves the following stages:
- Create a simple kamelet.
- Customizing existing kamelet catalog
Red Hat does not support implementing your own kamelet from scratch.
5.1. Creating a simple Kamelet Copier lienLien copié sur presse-papiers!
Apache Camel provides more than 300 components, and it is easy to create a Kamelet starting from one of the components already available. Most of the Kamelets available in the official catalog are simple ones that contain only a remapping of the Kamelet properties into Camel endpoint parameters.
Consider the following example, to provide a Kamelet that allows you to search data on Twitter, providing a stream of information about a given keyword. To create such a Kamelet, you can use the options of the "camel-twitter" component without any rectification.
To write a simple Kamelet, start building a new Kamelet resource using the kamel CLI:
kamel init twitter-search-source.kamelet.yaml
kamel init twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This produces a YAML file like the following :
twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the file to create a route that searches a given keyword on Twitter.
The route provided in the initial scaffold (timer-to-log) is inaccurate, so change it to the following:
twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - keywords is a path parameter in Camel Twitter-search. Some endpoint parameters are just mapped 1-1. The Camel component consumerKey is named apiKey to reflect the actual name in the Twitter developer portal.
- The Camel Twitter component generates Java objects, you must marshal them to JSON. A Source Kamelet sends data to the special endpoint "kamelet:sink", which is replaced at runtime by a different target.
The YAML route template above uses the twitter-search component to search on Twitter. You added a marshaling step to JSON because the output of a Kamelet must have a value to transfer over the wire. To complete the Kamelet, you must document the parameters in a JSON schema format. It is specified in the spec
definition part: twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
General information about the Kamelet in textual format:
- List of required parameters to create a Kamelet.
- A specification for each one of the parameters (flat structure, no nested options allowed)
Optional graphical customization for a specific UI (OpenShift Console)
The final Kamelet looks like the following:
twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can use the Kamelet shared on the Catalog and or created on a OpenShift cluster.
Example:
Apply on a cluster together with a simple binding.
- You must enable the Openshift cluster and connect to a namespace for the camel K operator to act.
Create the Kamelet.
kubectl apply -f twitter-search-source.kamelet.yaml
kubectl apply -f twitter-search-source.kamelet.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a binding as shown in the following example:
twitter-search-source-binding.yaml`
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This can be created using:
kubectl apply -f twitter-search-source-binding.yaml
kubectl apply -f twitter-search-source-binding.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Once created, you can see the logs of the binding using:
kamel logs twitter-search-source-binding
kamel logs twitter-search-source-binding
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - After executing the above steps, you must get a few tweets in the logs after the integration is created.
- For more information on how to use it in different contexts (like Knative, Kafka, etc) see Kamelets User Guide.
5.2. Customizing an existing Kamelet catalog Copier lienLien copié sur presse-papiers!
This is an example to customize an existing kamelet catalog.
Goal: Adding "groupId" option to the Kafka source kamelet.
- Current version kafka source kamelet contains an option "groupId". Following example does not contain the "groupId".
- Through this section, you must add "groupId" option to the existing kamelet catalog.
Customize Kamelet:
Get the original file.
Define the new Kamelet option
Add the information about the new option "consumerGroup" under
spec: definition: properties:
spec: definition: properties:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The contents of the definition would be similar to the following:
consumerGroup: title: Consumer Group description: A string that uniquely identifies the group of consumers to which this source belongs type: string example: "my-group-id"
consumerGroup: title: Consumer Group description: A string that uniquely identifies the group of consumers to which this source belongs type: string example: "my-group-id"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Map the new kamelet option with camel-kafka component’s groupId option
We must define which camel-kafka component option is set by "consumerGroup" under:
template: from: parameters:
template: from: parameters:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The contents of the definition are as follows:
groupId: "\{{?consumerGroup}}"
groupId: "\{{?consumerGroup}}"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Code:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow