Java: Code Example for Apache Kafka®¶
In this tutorial, you will run a Java 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¶
- Java 1.8 or higher to run the demo application.
- Maven to compile the demo application.
Kafka Cluster¶
The easiest way to follow this tutorial is with Confluent Cloud because you don’t have to run a local Kafka cluster.
When you sign up for Confluent Cloud, apply promo code C50INTEG
to receive an additional $50 free usage (details).
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
6.2.0-post
branch.git clone https://github.com/confluentinc/examples cd examples git checkout 6.2.0-post
Change directory to the example for Java.
cd clients/cloud/java/
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 and Kafka Streams¶
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¶
Compile the Java code.
mvn clean package
Run the producer, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the topic name
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.ProducerExample" \ -Dexec.args="$HOME/.confluent/java.config test1"
Verify that the producer sent all the messages. You should see:
... 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} Produced record to topic test1 partition [0] @ offset 0 Produced record to topic test1 partition [0] @ offset 1 Produced record to topic test1 partition [0] @ offset 2 Produced record to topic test1 partition [0] @ offset 3 Produced record to topic test1 partition [0] @ offset 4 Produced record to topic test1 partition [0] @ offset 5 Produced record to topic test1 partition [0] @ offset 6 Produced record to topic test1 partition [0] @ offset 7 Produced record to topic test1 partition [0] @ offset 8 Produced record to topic test1 partition [0] @ offset 9 10 messages were produced to topic test1 ...
View the producer code.
Consume Records¶
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
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.ConsumerExample" \ -Dexec.args="$HOME/.confluent/java.config test1"
Verify the consumer received all the messages. You should see:
... Polling Consumed record with key alice and value {"count":0}, and updated total count to 0 Consumed record with key alice and value {"count":1}, and updated total count to 1 Consumed record with key alice and value {"count":2}, and updated total count to 3 Consumed record with key alice and value {"count":3}, and updated total count to 6 Consumed record with key alice and value {"count":4}, and updated total count to 10 Consumed record with key alice and value {"count":5}, and updated total count to 15 Consumed record with key alice and value {"count":6}, and updated total count to 21 Consumed record with key alice and value {"count":7}, and updated total count to 28 Consumed record with key alice and value {"count":8}, and updated total count to 36 Consumed record with key alice and value {"count":9}, and updated total count to 45 Polling ...
When you are done, press
Ctrl-C
.View the consumer code.
Kafka Streams¶
Run the Kafka Streams application, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the topic name you used earlier
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.StreamsExample" \ -Dexec.args="$HOME/.confluent/java.config test1"
Verify that the Kafka Streams application processed all the messages. You should see:
... [Consumed record]: alice, 0 [Consumed record]: alice, 1 [Consumed record]: alice, 2 [Consumed record]: alice, 3 [Consumed record]: alice, 4 [Consumed record]: alice, 5 [Consumed record]: alice, 6 [Consumed record]: alice, 7 [Consumed record]: alice, 8 [Consumed record]: alice, 9 ... [Running count]: alice, 0 [Running count]: alice, 1 [Running count]: alice, 3 [Running count]: alice, 6 [Running count]: alice, 10 [Running count]: alice, 15 [Running count]: alice, 21 [Running count]: alice, 28 [Running count]: alice, 36 [Running count]: alice, 45 ...
When you are done, press
Ctrl-C
.View the Kafka Streams code.
Avro and Confluent Cloud Schema Registry¶
This example is similar to the previous example, except the value is formatted as Avro and integrates with the Confluent Cloud Schema Registry. Before using Confluent Cloud Schema Registry, check its availability and limits.
As described in the Quick Start for Schema Management on Confluent Cloud in the Confluent Cloud Console, enable Confluent Cloud Schema Registry and create an API key and secret to connect to it.
Verify that your VPC can connect to the Confluent Cloud Schema Registry public internet endpoint.
Update your local configuration file (for example, at
$HOME/.confluent/java.config
) with parameters to connect to Schema Registry.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 # Required connection configs for Confluent Cloud Schema Registry schema.registry.url=https://{{ SR_ENDPOINT }} basic.auth.credentials.source=USER_INFO basic.auth.user.info={{ SR_API_KEY }}:{{ SR_API_SECRET }}
Template configuration file for local host
# Kafka bootstrap.servers=localhost:9092 # Confluent Schema Registry schema.registry.url=http://localhost:8081
Verify your Confluent Cloud Schema Registry credentials by listing the Schema Registry subjects. In the following example, substitute your values for
{{ SR_API_KEY }}
,{{ SR_API_SECRET }}
, and{{ SR_ENDPOINT }}
.curl -u {{ SR_API_KEY }}:{{ SR_API_SECRET }} https://{{ SR_ENDPOINT }}/subjects
Produce Avro Records¶
Run the Avro producer, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the topic name
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.ProducerAvroExample" \ -Dexec.args="$HOME/.confluent/java.config test2"
View the producer Avro code.
Consume Avro Records¶
Run the Avro consumer, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the topic name
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.ConsumerAvroExample" \ -Dexec.args="$HOME/.confluent/java.config test2"
View the consumer Avro code.
Avro Kafka Streams¶
Run the Avro Kafka Streams application, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the same topic name you used earlier
mvn exec:java -Dexec.mainClass="io.confluent.examples.clients.cloud.StreamsAvroExample" \ -Dexec.args="$HOME/.confluent/java.config test2"
View the Kafka Streams Avro code.
Schema Evolution with Confluent Cloud Schema Registry¶
View the schema subjects registered in Confluent Cloud Schema Registry. In the following output, substitute values for
<SR API KEY>
,<SR API SECRET>
, and<SR ENDPOINT>
.curl -u <SR API KEY>:<SR API SECRET> https://<SR ENDPOINT>/subjects
Verify that the subject
test2-value
exists.["test2-value"]
View the schema information for subject test2-value. In the following output, substitute values for
<SR API KEY>
,<SR API SECRET>
, and<SR ENDPOINT>
.curl -u <SR API KEY>:<SR API SECRET> https://<SR ENDPOINT>/subjects/test2-value/versions/1
Verify the schema information for subject
test2-value
.{"subject":"test2-value","version":1,"id":100001,"schema":"{\"name\":\"io.confluent.examples.clients.cloud.DataRecordAvro\",\"type\":\"record\",\"fields\":[{\"name\":\"count\",\"type\":\"long\"}]}"}
For schema evolution, you can test schema compatibility between newer schema versions and older schema versions in Confluent Cloud Schema Registry. The pom.xml hardcodes the Schema Registry subject name to
test2-value
—change this if you didn’t use topic nametest2
. Then test local schema compatibility for DataRecordAvro2a.avsc, which should fail, and DataRecordAvro2b.avsc, which should pass.# DataRecordAvro2a.avsc compatibility test: FAIL mvn schema-registry:test-compatibility "-DschemaRegistryUrl=https://{{ SR_ENDPOINT }}" "-DschemaRegistryBasicAuthUserInfo={{ SR_API_KEY }}:{{ SR_API_SECRET }}" "-DschemaLocal=src/main/resources/avro/io/confluent/examples/clients/cloud/DataRecordAvro2a.avsc" # DataRecordAvro2b.avsc compatibility test: PASS mvn schema-registry:test-compatibility "-DschemaRegistryUrl=https://{{ SR_ENDPOINT }}" "-DschemaRegistryBasicAuthUserInfo={{ SR_API_KEY }}:{{ SR_API_SECRET }}" "-DschemaLocal=src/main/resources/avro/io/confluent/examples/clients/cloud/DataRecordAvro2b.avsc"