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
rhoas
CLI (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-topic
as 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 week
and 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
3
and 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-topic
appears 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-topic
on your Kafka instance.
-
Log in to the
rhoas
CLI.$ 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-topic
You’re prompted to enter a message value.
-
Enter
First message
as the message value. -
Repeat the previous steps to produce your second and third messages to
test-topic
. EnterSecond message
andThird message
as the message values. -
Produce your fourth and final message to
test-topic
. This time, specify a partition value of1
and 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 message
as 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
0
by default. -
The three messages on partition
0
have offset values of0
,1
, and2
. -
The final message is on partition
1
and 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-topic
in your Kafka instance. -
You’re logged in to the
rhoas
CLI.
-
Specify the Kafka instance that you want to consume messages from.
$ rhoas kafka use --name=<my-kafka-instance>
-
Consume the messages on partition
0
oftest-topic
.rhoas kafka topic consume --name=test-topic --partition=0
The 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
1
of the topic.rhoas kafka topic consume --name=test-topic --partition=1