The guide below demonstrates how to quickly get started with Apache Kafka. You'll connect to a broker, create a topic, produce some messages, and consume them. Be sure to also check out the client code examples to learn more.
This quick start uses the Confluent CLI to run Kafka in Docker. While you won't be running any Docker commands directly, you still need to install and start Docker Desktop or Docker Engine if you don't already have it.
Verify that Docker is set up and running properly by ensuring that no errors are output when you run docker info in your terminal.
Install the Confluent CLI if you don't already have it. In your terminal:
brew install confluentinc/tap/cli
If you don't use Homebrew, you can use a different installation method.
This quick start requires version 3.18.0 or later of the Confluent CLI. If you have an older version, run confluent update to get the latest release (or brew upgrade confluentinc/tap/cli if you installed the CLI with Homebrew).
Start Kafka by running this command:
confluent local kafka start
This starts Kafka in KRaft combined mode, meaning both the broker and controller run in a single container process.
Combined mode is only appropriate for local development and testing. Please refer to the documentation here for more information on configuring KRaft for production.
Create a new topic called quickstart that we'll use to write and read some test messages:
confluent local kafka topic create quickstart
You can use the Confluent CLI to write messages to a topic. This is useful for experimentation, but in practice, you’ll use the Producer API in your application code, or Kafka Connect for pulling data in from other systems to Kafka.
Run the following command. You’ll notice that nothing seems to happen, but fear not — it is waiting for your input.
confluent local kafka topic produce quickstart
Type in some lines of text. Each line is a new message.
When you’ve finished, enter Ctrl-C to return to your command prompt.
this is my first Kafka message
hello world!
this is my third Kafka message. I’m on a roll :-D
Now that we’ve written messages to the quickstart topic, we’ll read those messages back with the Confluent CLI:
confluent local kafka topic consume quickstart --from-beginning
As before, this is useful for trying things on the command line, but in practice, you’ll use the Consumer API in your application code, or Kafka Connect for reading data from Kafka to push to other systems.
You’ll see the messages that you entered in the previous step.
this is my first kafka message hello world! this is my third kafka message. I’m on a roll :-D
Leave the consume command from the previous step running. If you’ve already closed it, just rerun it.
Now open a new terminal window and run the produce command again:
confluent local kafka topic produce quickstart
Enter some more messages and note how they are displayed almost instantaneously in the consumer terminal.
Enter Ctrl-C in the producer and consumer terminals to exit each client program.
Once you’ve finished you can shut down Kafka:
confluent local kafka stop
Step through the basics of the CLI, Kafka topics, and building applications.
Learn how to route events, manipulate streams, aggregate data, and more.
Produce to and consume from Kafka in your preferred programming language.
Write your first application using these full code examples in Java, Python, Go, .NET, Node.js, C/C++, REST, Spring Boot, and further languages and CLIs.
We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.