Create an Apache Kafka Client App for Rust¶
In this tutorial, you will run a Rust 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¶
- Rust client for Apache Kafka installed on your machine
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 Rust.
cd clients/cloud/rust/
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
# Kafka bootstrap.servers={{ BROKER_ENDPOINT }} security.protocol=SASL_SSL sasl.mechanisms=PLAIN sasl.username={{ CLUSTER_API_KEY }} sasl.password={{ CLUSTER_API_SECRET }}
Template configuration file for local host
# Kafka bootstrap.servers=localhost:9092
Basic Consumer and Producer¶
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¶
Build the producer and consumer binaries:
cargo build
You should see:
Compiling rust_kafka_client_example v0.1.0 (/path/to/repo/examples/clients/cloud/rust) Finished dev [unoptimized + debuginfo] target(s) in 2.85s
Run the producer, passing in arguments for:
- the local file with configuration parameters to connect to your Kafka cluster
- the topic name
./target/debug/producer --config $HOME/.confluent/librdkafka.config --topic test1
Verify the producer sent all the messages. You should see:
Preparing to produce record: alice 0 Preparing to produce record: alice 1 Preparing to produce record: alice 2 Preparing to produce record: alice 3 Preparing to produce record: alice 4 Preparing to produce record: alice 5 Preparing to produce record: alice 6 Preparing to produce record: alice 7 Preparing to produce record: alice 8 Successfully produced record to topic test1 partition [5] @ offset 117 Successfully produced record to topic test1 partition [5] @ offset 118 Successfully produced record to topic test1 partition [5] @ offset 119 Successfully produced record to topic test1 partition [5] @ offset 120 Successfully produced record to topic test1 partition [5] @ offset 121 Successfully produced record to topic test1 partition [5] @ offset 122 Successfully produced record to topic test1 partition [5] @ offset 123 Successfully produced record to topic test1 partition [5] @ offset 124 Successfully produced record to topic test1 partition [5] @ offset 125
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
./target/debug/consumer --config $HOME/.confluent/librdkafka.config --topic test1
Verify the consumer received all the messages. You should see:
Consumed record from topic test1 partition [5] @ offset 117 with key alice and value 0 Consumed record from topic test1 partition [5] @ offset 118 with key alice and value 1 Consumed record from topic test1 partition [5] @ offset 119 with key alice and value 2 Consumed record from topic test1 partition [5] @ offset 120 with key alice and value 3 Consumed record from topic test1 partition [5] @ offset 121 with key alice and value 4 Consumed record from topic test1 partition [5] @ offset 122 with key alice and value 5 Consumed record from topic test1 partition [5] @ offset 123 with key alice and value 6 Consumed record from topic test1 partition [5] @ offset 124 with key alice and value 7 Consumed record from topic test1 partition [5] @ offset 125 with key alice and value 8
View the consumer code.