CONFLUENT PLATFORM
In this tutorial, you will run a 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.
-F
C50INTEG
Clone the confluentinc/examples GitHub repository and check out the 6.1.0-post branch.
6.1.0-post
git clone https://github.com/confluentinc/examples cd examples git checkout 6.1.0-post
Change directory to the example for kafkacat.
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).
$HOME/.confluent/java.config
{{ BROKER_ENDPOINT }}
{{CLUSTER_API_KEY }}
{{ CLUSTER_API_SECRET }}
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
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.
alice
{"count": 0}
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 kafkacat, writing messages to topic test1, passing in arguments for:
test1
-F $HOME/.confluent/java.config
-K ,
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.
CTRL-D
View the producer code.
Run kafkacat again, reading messages from topic test, passing in arguments for:
test
-e
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.