Ruby

In this tutorial, you will run a Ruby client application using the ZenDesk Ruby Client for Apache Kafka that produces messages to and consumes messages from an Apache Kafka® cluster.

After you run the tutorial, view the provided source code and use it as a reference to develop your own Kafka client application.

Prerequisites

Client

  • Bundler installed on your machine. Install using gem install bundler.

Kafka Cluster

  • You can use this tutorial with a Kafka cluster in any environment:
  • If you are running on Confluent Cloud, you must have access to a Confluent Cloud cluster with an API key and secret.

Setup

  1. Clone the confluentinc/examples GitHub repository and check out the 6.0.0-post branch.

    git clone https://github.com/confluentinc/examples
    cd examples
    git checkout 6.0.0-post
    
  2. Change directory to the example for Ruby.

    cd clients/cloud/ruby/
    
  3. Create a local file (for example, at $HOME/.confluent/librdkafka.config) with configuration parameters to connect to your Kafka cluster. Starting with one of the templates below, customize the file with connection information to your cluster. Substitute your values for {{ BROKER_ENDPOINT }}, {{CLUSTER_API_KEY }}, and {{ CLUSTER_API_SECRET }} (see Configure Confluent Cloud Clients for instructions on how to manually find these values, or use the ccloud-stack Utility for Confluent Cloud to automatically create them).

    • Template configuration file for Confluent Cloud

      # Kafka
      bootstrap.servers={{ BROKER_ENDPOINT }}
      security.protocol=SASL_SSL
      sasl.mechanisms=PLAIN
      sasl.username={{ CLUSTER_API_KEY }}
      sasl.password={{ CLUSTER_API_SECRET }}
      
    • Template configuration file for local host

      # Kafka
      bootstrap.servers=localhost:9092
      

Basic Producer and Consumer

In this example, the producer application writes Kafka data to a topic in your Kafka cluster. If the topic does not already exist in your Kafka cluster, the producer application will use the Kafka Admin Client API to create the topic. Each record written to Kafka has a key representing a username (for example, alice) and a value of a count, formatted as json (for example, {"count": 0}). The consumer application reads the same Kafka topic and keeps a rolling sum of the count as it processes each record.

Produce Records

  1. Install gems:

    bundle install
    
  2. Run the producer, passing in arguments for:

    • the local file with configuration parameters to connect to your Kafka cluster
    • the topic name
    ruby producer.rb -f $HOME/.confluent/librdkafka.config --topic test1
    
  3. Verify the producer sent all the messages. You should see:

    Creating topic test1
    Producing record: alice {"count":0}
    Producing record: alice {"count":1}
    Producing record: alice {"count":2}
    Producing record: alice {"count":3}
    Producing record: alice {"count":4}
    Producing record: alice {"count":5}
    Producing record: alice {"count":6}
    Producing record: alice {"count":7}
    Producing record: alice {"count":8}
    Producing record: alice {"count":9}
    10 messages were successfully produced to topic test1!
    
  4. View the producer code.

Consume Records

  1. Run the consumer, passing in arguments for:

    • the local file with configuration parameters to connect to your Kafka cluster
    • the topic name you used earlier
    ruby consumer.rb -f $HOME/.confluent/librdkafka.config --topic test1
    
  2. Verify the consumer received all the messages:

    Consuming messages from test1
    Consumed record with key alice and value {"count":0}, and updated total count 0
    Consumed record with key alice and value {"count":1}, and updated total count 1
    Consumed record with key alice and value {"count":2}, and updated total count 3
    Consumed record with key alice and value {"count":3}, and updated total count 6
    Consumed record with key alice and value {"count":4}, and updated total count 10
    Consumed record with key alice and value {"count":5}, and updated total count 15
    Consumed record with key alice and value {"count":6}, and updated total count 21
    Consumed record with key alice and value {"count":7}, and updated total count 28
    Consumed record with key alice and value {"count":8}, and updated total count 36
    Consumed record with key alice and value {"count":9}, and updated total count 45
    ...
    
  3. View the consumer code.