Create an Apache Kafka Client App for kcat

In this tutorial, you will run a kcat client application that produces messages to and consumes messages from an Apache Kafka® cluster.

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

Prerequisites

Client

  • kcat installed on your machine. You must build kcat from the latest master branch to get the -F functionality that makes it easy to pass in the configuration to your Confluent Cloud configuration file.

Kafka Cluster

Setup

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

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

    cd clients/cloud/kafkacat/
    
  3. Create a local file (for example, at $HOME/.confluent/java.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

      # Required connection configs for Kafka producer, consumer, and admin
      bootstrap.servers={{ BROKER_ENDPOINT }}
      security.protocol=SASL_SSL
      sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username='{{ CLUSTER_API_KEY }}' password='{{ CLUSTER_API_SECRET }}';
      sasl.mechanism=PLAIN
      # Required for correctness in Apache Kafka clients prior to 2.6
      client.dns.lookup=use_all_dns_ips
      
      # Best practice for Kafka producer to prevent data loss 
      acks=all
      
    • 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. Create the Kafka topic.

    kafka-topics --bootstrap-server `grep "^\s*bootstrap.server" $HOME/.confluent/java.config | tail -1` --command-config $HOME/.confluent/java.config --topic test1 --create --replication-factor 3 --partitions 6
    
  2. Run kcat, writing messages to topic test1, passing in arguments for:

    • -F $HOME/.confluent/java.config: configuration file for connecting to the Confluent Cloud cluster
    • -K ,: pass key and value, separated by a comma
    kafkacat -F $HOME/.confluent/java.config -K , -P -t test1
    
  3. Type a few messages, using a , as the separator between the message key and value:

    alice,{"count":0}
    alice,{"count":1}
    alice,{"count":2}
    
  4. When you are done, press CTRL-D.

  5. View the producer code.

Consume Records

  1. Run kcat again, reading messages from topic test, passing in arguments for:

    • -F $HOME/.confluent/java.config: configuration file for connecting to the Confluent Cloud cluster
    • -K ,: pass key and value, separated by a comma
    • -e: exit successfully when last message received
    kafkacat -F $HOME/.confluent/java.config -K , -C -t test1 -e
    

    You should see the messages you typed earlier.

    % Reading configuration from file $HOME/.confluent/java.config
    % Reached end of topic test1 [3] at offset 0
    alice,{"count":0}
    alice,{"count":1}
    alice,{"count":2}
    % Reached end of topic test1 [7] at offset 0
    % Reached end of topic test1 [4] at offset 0
    % Reached end of topic test1 [6] at offset 0
    % Reached end of topic test1 [5] at offset 0
    % Reached end of topic test1 [1] at offset 0
    % Reached end of topic test1 [2] at offset 0
    % Reached end of topic test1 [9] at offset 0
    % Reached end of topic test1 [10] at offset 0
    % Reached end of topic test1 [0] at offset 0
    % Reached end of topic test1 [8] at offset 0
    % Reached end of topic test1 [11] at offset 3: exiting
    
  2. View the consumer code.