Skip to content

Latest commit

 

History

History

rhoas-produce-consume

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Producing and consuming messages by using the OpenShift Application Services CLI

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.

Prerequisites

Creating a Kafka topic in Streams for Apache Kafka

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.

Prerequisites
  • You’ve created a Kafka instance in OpenShift Streams for Apache Kafka and the instance is in the Ready state.

Procedure
  1. 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.

  2. Select the Topics tab.

  3. 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 to Unlimited.

    • 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 to 2. For a trial Kafka instance, the number of replicas and the minimum in-sync replica factor are both set to 1.

    After you complete the setup, the new topic appears on the Topics page.

Verification
  • Verify that test-topic appears on the Topics page.

Producing messages to your Kafka instance

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.

Prerequisites
  • You’ve created test-topic on your Kafka instance.

Procedure
  1. Log in to the rhoas CLI.

    $ rhoas login
  2. Specify the Kafka instance that you want to produce messages to.

    $ rhoas kafka use --name=<my-kafka-instance>
  3. Produce your first message to test-topic.

    rhoas kafka topic produce --name=test-topic

    You’re prompted to enter a message value.

  4. Enter First message as the message value.

  5. Repeat the previous steps to produce your second and third messages to test-topic. Enter Second message and Third message as the message values.

  6. Produce your fourth and final message to test-topic. This time, specify a partition value of 1 and a custom message key. An example is shown below.

    rhoas kafka topic produce --name=test-topic --partition=1 --key="{'location': 'us-east-1'}"
  7. Enter Fourth message as the value of the final message.

  8. To view the messages in the OpenShift Streams for Apache Kafka web console, perform the following actions:

    1. On the Kafka Instances page, click the name of your Kafka instance.

    2. Select the Topics tab, and then click test-topic.

    3. Select the Messages tab.

      The messages table shows the messages you produced.

    4. 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 of 0, 1, and 2.

      • The final message is on partition 1 and has the custom key value that you specified.

Verification
  • Verify that the messages table shows the four messages you produced.

Consuming messages from your Kafka instance

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.

Prerequisites
  • You’ve produced messages to test-topic in your Kafka instance.

  • You’re logged in to the rhoas CLI.

Procedure
  1. Specify the Kafka instance that you want to consume messages from.

    $ rhoas kafka use --name=<my-kafka-instance>
  2. Consume the messages on partition 0 of test-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.

  3. 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
  4. Consume the fourth message, which you produced to partition 1 of the topic.

    rhoas kafka topic consume --name=test-topic --partition=1