Apache Kafka

Apache Kafka Producer Example in Ruby

Apache Kafka is a free and open-source, distributed, scalable, and high-throughput message broker that enables the real-time data streaming and event-driven architecture. In this tutorial, we will learn how to create a Kafka producer in Ruby which allows us to write the messages to an existing Kafka topic.

By following this tutorial, we assume that you have a basic understanding of Kafka concepts such as topics, partitions, and brokers. We also assume that you are not new to Ruby and are able to install and configure the Ruby gems in easy steps.

We use the rdkakfa gem, a modern and well-optimized client library to interact with Apache Kafka using Ruby. It is based on the librdkafka implementation which is created in C. It also supports Apache Kafka 1.0 and above, making it extensible in a wide array of Kafka versions.

At the end of this tutorial, you will have a working Kafka producer in Ruby that can write the data to a Kafka topic. It is good to note that this is a basic tutorial and is not intended as a full-fledged producer application.

Project Setup

We need to start by setting up the project structure. For this demonstration, we use the combine Kakfa CLI tools to setup the various blocks such as topics.

Start by creating the directory to store your source code:

$ mkdir ~/projects/kafka_ruby_prod

Navigate into the project directory and add the file for your source code:

$ cd ~/projects/kafka_ruby_prod && touch src.rb

The next step is to create a Kafka topic to store the messages. We can do this using the Kafka Topic CLI utility.

The command is as shown in the following:

kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic kafka_rb

The previous command should create a new Kafka topic called “kafka_rb” with a replication factor of 1 and one partition.

Once successful, we can proceed and setup the producer applications to write the messages to the topic.

Start by installing the rdkafka gem with the following command:

$ sudo gem install rdkafka

Once installed, edit the source file and add the code as follows:

$ vim src.rb

Source code:

require 'rdkafka'

config = {:"bootstrap.servers" => "localhost:9092"}
producer = Rdkafka::Config.new(config).producer
delivery_handles = []

index = 0
File.open("user_data.txt").each do |line|
   index += 1
   delivery_handles << producer.produce(
      topic: "kafka_rb",
      payload: "#{line}",
      key: "#index}"
   )
end
delivery_handles.each(&:wait)

The previous code uses the Rdkafka module to create a new Producer instance on the server which is specified in the config property.

We then define the delivery_handles as an empty array which is used to store the messages that are read from the file.

The contents of the user_data.txt file is as follows:

Fran,[email protected],86.198.46.47
Clareta,[email protected],55.177.226.116
Clem,[email protected],245.235.202.52
Alene,[email protected],70.240.174.255
Elysha,[email protected],133.220.136.158

Finally, we use the producer.produce() method to write the messages to the kafka_rb topic in the Kafka instance. Once completed, we can run the code as follows:

$ ruby src.rb

This should read the file and allow you to write the messages from the file to the topic. We can verify that the messages are produced to the topic by reading from the topic with the following command:

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic kafka_rb --from-beginning

The previous command should allow you to read the messages in the topic as shown in the following output:

Conclusion

You now learned how to create a Kafka producer application in Ruby that reads the messages from a file and writes them to an existing Kafka topic.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list