AMQ Streams With Kafka Connect on Openshift
AMQ Streams With Kafka Connect on Openshift
Receiving messages from Telegram with Kafka Connect
Join the DZone community and get the full member experience.Join For Free
This article's about a common feature of Apache Kafka, called Kafka Connect. I'll explain how it works with OpenShift.
What Is Kafka Connect?
Kafka Connect is an open-source component of Apache Kafka®. It is a framework for connecting Kafka with external systems, such as databases, key-value stores, search indexes, and file systems. The Telegram platform is one of these systems and in this article, I will demonstrate how to use Kafka Connect deployed on OpenShift to get data from Telegram.
Kafka Connect API Architecture
Basically there are 2 types of Kafka Connectors:
A source connector collects data from a system and sends it to a Kafka topic, for example, we can get messages from Telegram and put it in a Kafka topic.
A sink connector delivers data from Kafka topics into other systems, which might be indexed such as Elasticsearch, batch systems such as Hadoop, or any kind of database.
There are two ways to execute Kafka Connect: Standalone and Distributed. However, the operator supports only Distributed mode in OpenShift. That's not a restriction, because none Kafka distribution in production environments is recommended set standalone mode.
The Kafka Connector uses an environment independent of Kafka Broker, on OpenShift Kafka Connect API runs in a separated pod.
In this example, we will use a source connector.
Before We Start
Firstly will be necessary to create a bot in the Telegram platform and generate an access token to him. We need this token to generate a secret on OpenShift to Kafka Connect can receive messages from Telegram. To create this bot, see the article — Bots: An introduction for developers.
The necessary files to execute this demonstration is on GitHub. Click here to start the download. The files have a number that identifies the sequence of use of them in this article. Any person can download and reproduce this demonstration.
The first step is to deploy AMQ Streams on OpenShit. For this, use the Strimzi operator provided on the Red Hat Web Site. The link of the file to download is "Red Hat AMQ Streams 1.4.1 OpenShift Installation and Example Files".
If you prefer, I generated a package with the operator also available on GitHub.
Use the commands below to install AMQ Stream (Kafka) on OpenShift.
What have we done? Firstly, we have created a project named 'kafkaproject' and deploy the Strimzi operator. When the operator was finished we deploy AMQ Streams through the kafka.yaml template.
The result of these commands is bellow,
The next step is to generate the Openshift secret using Telegram access token. Create the property file, see the example below. I chose 'telegram-credentials.properties' filename. Feel free to choose your filename.
Create the file telegram-credentials.yaml and put the base64 representation of file telegram-credentials.properties.
Use the below commands to create the secret and deploy the Kafka Connect. Making a reference with RHEL version, the kafka-connect.yaml is similar to execute connect-distributed.sh
Notice that a new pod was created on OpenShift.
After deploying Kafka Connect, we need to inform the parameters to connect to the Telegram. For this, we use the file 6-telegram-connector.yaml. See below:
Execute the command below:
After completion, and if the credentials are correct, any message sent to your bot will be received by Kafka Connect and saved in a topic called telegram-topic, which was defined in property camel.source.kafka.topic in the file 6-telegram-connector.yaml. Notice how the API token is mounted from a secret in the property camel.component.telegram.authorizationToken.
You can go to the Telegram app and talk with the bot, in this example I create a bot called
@amqstreams. Run the receiver on a Kafka topic telegram-topic to see the messages sent to the bot.
To consume the messages on the topic, we can use the command below:
A good point to notice here is the property camel.source.url. Internally, when we provide the value , telegram-bots, to property, the connector created a camel route using a Camel Telegram component.
There are a lot of connectors available for Kafka Connet, in our hands-on, we used a Camel Connector, this one allows us to use all power of Apache Camel together Kafka Connect. In this example, we didn't write any code. However, if necessary, we can create customized routes to connect to anything and use the same steps used here to put dada on the Apache Kafka.
The Kafka Connect is a powerful member of the Kafka Ecosystem. In this article I showed a simple integration, however, Kafka Connect allows us to interact with a lot of systems or even other message brokers.
Guard this advice: “If there is data, we can connect and get it.“
Opinions expressed by DZone contributors are their own.