/apache-kafka-book-examples

Fixed and updated code examples from the book "Apache Kafka"

Primary LanguageBatchfile

Fixed and updated code examples from the book "Apache Kafka"

  • Updated to Apache Kafka 0.8.1.1
  • Configuration optimized for usage on Windows machines
  • Windows batch scripts fixed (taken from https://github.com/HCanber/kafka by @HCanber)
  • Code examples repaired and refactored

Initial Setup

  1. Download and install Apache Kafka 0.8.1.1 (I used the recommended Scala 2.9.2 binary)
  2. Copy the scripts from /bat into /bin/windows of your Kafka installation folder (overwrite existing scripts)
  3. Copy the property files from /config into /config of your Kafka installation folder (overwrite existing files)

Simple Java Producer (Chapter 5, Page 35ff.)

  1. Open command line in your Kafka installation folder
  2. Launch Zookeeper with .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
  3. Open a second command line in your Kafka installation folder
  4. Launch single Kafka broker: .\bin\windows\kafka-server-start.bat .\config\server.properties
  5. Open a third command line in your Kafka installation folder
  6. Create a topic: .\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  7. Start a console consumer for that topic: .\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test --from-beginning
  8. From a fourth command line or your IDE run SimpleProducer with topic and message as arguments: java SimpleProducer test HelloKafka
  9. The message HelloKafka should appear in the console consumer's log

Java Producer with Message Partitioning (Chapter 5, Page 37ff.)

  1. Open command line in your Kafka installation folder
  2. Launch Zookeeper with .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
  3. Open a second command line in your Kafka installation folder
  4. Launch first Kafka broker: .\bin\windows\kafka-server-start.bat .\config\server-1.properties
  5. Open a third command line in your Kafka installation folder
  6. Launch second Kafka broker: .\bin\windows\kafka-server-start.bat .\config\server-2.properties
  7. Open a fourth command line in your Kafka installation folder
  8. Create a topic: .\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 2 --partitions 4 --topic kafkatest
  9. Start a console consumer for that topic: .\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic kafkatest --from-beginning
  10. From a fifth command line or your IDE run MultiBrokerProducer with topic as argument: java MultiBrokerProducer kafkatest
  11. Ten messages starting with This message is for key - (...) should appear in the console consumer's log

Simple High Level Java Consumer (Chapter 6, Page 47ff.)

  1. Launch multi-broker Kafka cluster and create topic kafkatest as described in step 1-8 of Java Producer with Message Partitioning
  2. From another command line or your IDE run SimpleHLConsumer with topic as argument: java SimpleHLConsumer kafkatest
  3. From another command line or your IDE run MultiBrokerProducer with same topic as argument: java MultiBrokerProducer kafkatest
  4. Ten messages starting with This message is for key - (...) should appear in the log of the SimpleHLConsumer

Multithreaded Consumer for Multipartition Topics (Chapter 6, Page 50ff.)

  1. Launch multi-broker Kafka cluster and create topic kafkatest as described in step 1-8 of Java Producer with Message Partitioning
  2. From another command line or your IDE run MultiThreadHLConsumer with topic and number of threads as argument: java MultiThreadHLConsumer kafkatest 4
  3. From another command line continuously produce messages by running MultiBrokerProducer several times in a row: java MultiBrokerProducer kafkatest (Note: You must start producing messages within 10sec after starting the consumer class, otherwise the consumer will shut down)
  4. Messages starting with Message from thread (...) should appear in the log of the MultiThreadHLConsumer spread among the four threads