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¶
Kafka Cluster¶
- You can use this tutorial with a Kafka cluster in any environment:
- In Confluent Cloud
- On your local host
- Any remote Kafka cluster
- If you are running on Confluent Cloud, you must have access to a
Confluent Cloud cluster
with an API key and secret.
- For an automated way to create a Kafka cluster, credentials, and ACLs in Confluent Cloud, see ccloud-stack Utility for Confluent Cloud.
Setup¶
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
Change directory to the example for kcat.
cd clients/cloud/kafkacat/
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¶
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
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
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/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
View the consumer code.