As a developer of applications and services, you can use the OpenShift Application Services rhoas command-line interface (CLI) to produce and consume messages for Kafka instances in OpenShift Streams for Apache Kafka. This is a useful way to test and debug your Kafka instances.
-
You have a running Kafka instance in Streams for Apache Kafka (see Getting started with OpenShift Streams for Apache Kafka).
-
You have a command-line terminal application.
-
You’ve installed the latest version of the
rhoasCLI (see Installing and configuring the rhoas CLI).
In this task, you’ll create a new topic in your Kafka instance. You’ll use this topic in later tasks to produce and consume messages.
-
You’ve created a Kafka instance in OpenShift Streams for Apache Kafka and the instance is in the Ready state.
-
On the Kafka Instances page of the Streams for Apache Kafka web console, click the Kafka instance that you want to add a topic to.
-
Select the Topics tab.
-
Click Create topic and follow the guided steps to define the topic details.
You must specify the following topic properties:
-
Topic name: For this quick start, enter
test-topicas the topic name. -
Partitions: Set the number of partitions for the topic. For this quick start, set the value to
2. -
Message retention: Set the message retention time and size. For this quick start, set the retention time to
A weekand the retention size toUnlimited. -
Replicas: For this release of Streams for Apache Kafka, replica values are preconfigured. The number of partition replicas for the topic is set to
3and the minimum number of follower replicas that must be in sync with a partition leader is set to2. For a trial Kafka instance, the number of replicas and the minimum in-sync replica factor are both set to1.
After you complete the setup, the new topic appears on the Topics page.
-
-
Verify that
test-topicappears on the Topics page.
When you have a Kafka instance with a topic, you’re ready to use the CLI to produce messages. In this task, you’ll produce four messages to your Kafka instance.
-
You’ve created
test-topicon your Kafka instance.
-
Log in to the
rhoasCLI.$ rhoas login -
Specify the Kafka instance that you want to produce messages to.
$ rhoas kafka use --name=<my-kafka-instance> -
Produce your first message to
test-topic.rhoas kafka topic produce --name=test-topicYou’re prompted to enter a message value.
-
Enter
First messageas the message value. -
Repeat the previous steps to produce your second and third messages to
test-topic. EnterSecond messageandThird messageas the message values. -
Produce your fourth and final message to
test-topic. This time, specify a partition value of1and a custom message key. An example is shown below.rhoas kafka topic produce --name=test-topic --partition=1 --key="{'location': 'us-east-1'}" -
Enter
Fourth messageas the value of the final message. -
To view the messages in the OpenShift Streams for Apache Kafka web console, perform the following actions:
-
On the Kafka Instances page, click the name of your Kafka instance.
-
Select the Topics tab, and then click
test-topic. -
Select the Messages tab.
The messages table shows the messages you produced.
-
Observe the following details about the messages you produced:
-
Because you didn’t specify a partition value when producing the first three messages, these messages all went to partition
0by default. -
The three messages on partition
0have offset values of0,1, and2. -
The final message is on partition
1and has the custom key value that you specified.
-
-
-
Verify that the messages table shows the four messages you produced.
When you’ve produced messages to your Kafka instance and verified them using the OpenShift Streams for Apache Kafka web console, you’re ready to consume the messages. In this task, you’ll use the CLI to consume the messages.
-
You’ve produced messages to
test-topicin your Kafka instance. -
You’re logged in to the
rhoasCLI.
-
Specify the Kafka instance that you want to consume messages from.
$ rhoas kafka use --name=<my-kafka-instance> -
Consume the messages on partition
0oftest-topic.rhoas kafka topic consume --name=test-topic --partition=0The CLI displays the messages. Observe that because you didn’t specify an offset value, the CLI displays all of the messages on partition
0. -
Consume the third message that you produced by specifying the appropriate partition and offset values.
rhoas kafka topic consume --name=test-topic --partition=0 --offset=2 -
Consume the fourth message, which you produced to partition
1of the topic.rhoas kafka topic consume --name=test-topic --partition=1