Create a Kafka Client App for kcat for Use With Confluent Cloud¶
In this tutorial, you will run a kcat
(formerly known as kafkacat
) 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. - Local install of Confluent CLI v2.5.0 or later.
Kafka Cluster¶
The easiest way to follow this tutorial is with Confluent Cloud because you don’t have to run a local Kafka cluster.
From the Console, click on LEARN to provision a cluster and click on Clients
to get the cluster-specific configurations and credentials to set for your client application.
You can alternatively use the supported CLI or REST API, or the community-supported ccloud-stack utility for Confluent Cloud.
If you don’t want to use Confluent Cloud, you can also use this tutorial with a Kafka cluster running on your local host or any other remote server.
Setup¶
Clone the confluentinc/examples GitHub repository and check out the
latest
branch.git clone https://github.com/confluentinc/examples cd examples git checkout latest
Change directory to the example for kcat.
cd clients/cloud/kcat/
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
# Required connection configs for Kafka producer, consumer, and admin bootstrap.servers={{ BROKER_ENDPOINT }} security.protocol=SASL_SSL sasl.mechanisms=PLAIN sasl.username={{ CLUSTER_API_KEY }} sasl.password={{ CLUSTER_API_SECRET }} # Best practice for higher availability in librdkafka clients prior to 1.7 session.timeout.ms=45000
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¶
Create the Kafka topic. If you are using Confluent Cloud then use the Confluent CLI:
confluent kafka topic create --if-not-exists test1
If you are using a local Kafka cluster then use the Kafka CLI:
kafka-topics --bootstrap-server localhost:9092 --topic $topic_name --create --if-not-exists
Run kcat, writing messages to topic
test1
, passing in arguments for:-F $HOME/.confluent/librdkafka.config
: configuration file for connecting to the Confluent Cloud cluster-K ,
: pass key and value, separated by a comma
kcat -F $HOME/.confluent/librdkafka.config -K , -P -t test1
Type a few messages, using a
,
as the separator between the message key and value:alice,{"count":0} alice,{"count":1} alice,{"count":2}
When you are done, press
CTRL-D
.View the producer code.
Consume Records¶
Run kcat again, reading messages from topic
test
, passing in arguments for:-F $HOME/.confluent/librdkafka.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
kcat -F $HOME/.confluent/librdkafka.config -K , -C -t test1 -e
You should see the messages you typed earlier.
% Reading configuration from file $HOME/.confluent/librdkafka.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
View the consumer code.