I really like Homebrew to install stuff on macOS. So in this tutorial I’ll be using Hombrew to install Apache Kafka on macOS.
Open Terminal, paste the following command and then press Return key:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
The above command will install Homebrew. Once it is installed, follow the next steps:
1) Install Java8:
After Homebrew is installed, you can just paste the following commands in Terminal and press Return to install java on macOS:
$ brew tap adoptopenjdk/openjdk $ brew cask install adoptopenjdk8
The above commands will install openjdk 8. In order to validate the java installation, execute the following command in Terminal:
$ java -version
If you get the following output, it means that java is installed correctly:
openjdk version "1.8.0_212" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b04, mixed mode)
2) Install Kafka:
To install Kafka on macOS: open Terminal, paste the following command and then press Return:
$ brew install kafka
The above command will install Kafka along with Zookeeper.
To start ZooKeeper, paste the following command in the Terminal and press Return:
$ zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
Once ZooKeeper is started you’ll see something like this in the console:
INFO binding to port 0.0.0.0/0.0.0.0:2181 (org.apache.zookeeper.server.NIOServerCnxnFactory)
To start Kafka, paste the following command in another Terminal and press Return:
$ kafka-server-start /usr/local/etc/kafka/server.properties
Once Kafka is started you’ll see something like this in the console:
INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
Create a Kafka topic:
Open another Terminal and execute the following command to create a topic testTopic with 1 partition and 1 replication factor:
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testTopic
Produce messages to the created topic:
In the same Terminal prompt, start a kafka-console-producer and produce some messages:
$ kafka-console-producer --broker-list localhost:9092 --topic testTopic >Hello >How are you?
Consume the messages produced by the producer:
Open another Terminal and start a kafka-console-consumer which subscribes to testTopic:
$ kafka-console-consumer --bootstrap-server localhost:9092 --topic testTopic --from-beginning Hello How are you?
The messages which are produced by the kafka-console-producer will be visible on the kafka-console-consumer console.
Congratulations on setting up a single broker Kafka setup on macOS!