Working with Kafka from the Command Line

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

In this lesson, we will have the opportunity to work with a real Kafka cluster using the command line tools provided through Kafka. Ultimately, Kafka makes it easy to build applications that interact with the Kafka cluster, but it also provides some command line tools. We can use these to create ad-hoc producers and consumers, plus perform a variety of administrative tasks that require you to interact with the cluster.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Kafka Topic for the Inventory Purchase Data
  1. Create the topic using the kafka-topics command:

    kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 6 --topic inventory_purchases
Test the Setup by Publishing and Consuming Some Data
  1. Start a command line producer:

    kafka-console-producer --broker-list localhost:9092 --topic inventory_purchases
  2. Type in a few lines of test data. Since we are working with merely test data, a specific format is not required. It could look like this:

    product: apples, quantity: 5
    product: lemons, quantity: 7
  3. Once the test messages are published, we can exit the producer.

  4. Start up a command line consumer:

    kafka-console-consumer --bootstrap-server localhost:9092 --topic inventory_purchases --from-beginning
  5. We should see the test messages that were published earlier:

    product: apples, quantity: 5
    product: lemons, quantity: 7

Additional Resources

Your supermarket company has a three-broker Kafka cluster. They want to use Kafka to track purchases so they can keep track of their inventory and are working on some software that will interact with the Kafka cluster to produce and consume this data.

However, before they can start using the cluster, they need you to create a topic to handle this data. You will also need to test that everything is working by publishing some data to the topic, and then address consuming it. Since the application is being built, you will need to do this using Kafka's command line tools.

Create a topic that meets the following specifications:

  • The topic name should be inventory_purchases.
  • Number of partitions: 6
  • Replication factor: 3

Publish some test data to the topic. Since this is just a test, the data can be anything you want, but here is an example:

product: apples, quantity: 5
product: lemons, quantity: 7

Set up a consumer from the command line and verify that you see the test data you published to the topic.

This cluster is a confluent cluster, so you can access the Kafka command line utilities directly from the path, i.e. kafka-topics.

If you get stuck, feel free to check out the solution video, or the detailed instructions under each objective. Good luck!

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?