Installing and running Apache Kafka on macOS involves several steps because Kafka depends on Zookeeper for cluster management. Below are the steps to install Kafka and Zookeeper using Homebrew, create a Kafka topic, and verify that Kafka is running correctly.
First, ensure that Homebrew is installed on your macOS. If not, you can install it by running the following command in the Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Kafka requires Zookeeper to run, so you'll need to install both. You can do this easily with Homebrew:
brew install kafka
This command installs both Kafka and Zookeeper because Kafka has Zookeeper as a dependency.
Before you start Kafka, you need to start Zookeeper. Homebrew makes it simple to start services:
brew services start zookeeper
Once Zookeeper is up and running, you can start Kafka:
brew services start kafka
You can check that Zookeeper and Kafka are running by looking at the list of services managed by Homebrew:
brew services list
You should see kafka
and zookeeper
in the list, each with a status of "started".
Now that Kafka is running, you can create a topic. Let's create a topic named "test" with a single partition and only one replica:
kafka-topics --create --topic test --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
Verify that the topic "test" has been created successfully:
kafka-topics --list --bootstrap-server localhost:9092
You should see "test" listed among the topics.
To ensure everything is set up correctly, you can test producing and consuming messages with the "test" topic.
- Start a Kafka Consumer: Open a new Terminal window and run:
kafka-console-consumer --topic test --from-beginning --bootstrap-server localhost:9092
- Produce Messages to the Topic: Open another Terminal window and run:
kafka-console-producer --topic test --bootstrap-server localhost:9092
Now, you can type messages into the producer terminal. Press Enter after each message, and you should see the message appear in the consumer terminal.
This confirms that Kafka is running correctly on your macOS, and you're ready to use it for your event sourcing and messaging needs in your applications.