<!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

<a id="client-examples-rust"></a>

# Create a Kafka Client App for Rust for Use With Confluent Platform

In this tutorial, you will run a Rust client application that produces messages
to and consumes messages from an Apache Kafka® cluster.

<!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

After you run the tutorial, use the provided [source code](https://github.com/confluentinc/examples/tree/latest/clients/cloud) as a reference to develop your own Kafka client application.

## Prerequisites

### Client

- [Rust client for Apache
  Kafka](https://github.com/fede1024/rust-rdkafka#installation) installed on
  your machine

### Kafka Cluster

<!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

The easiest way to follow this tutorial is with [Confluent Cloud](https://www.confluent.io/confluent-cloud/tryfree/) because you don’t have to run a local Kafka cluster.
From the Console, click on [LEARN](https://confluent.cloud/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](https://docs.confluent.io/confluent-cli/current/) or [REST API](https://docs.confluent.io/cloud/current/client-apps/kafka-rest/krest-qs.html), or the community-supported [ccloud-stack utility for Confluent Cloud](/cloud/current/examples/ccloud/docs/ccloud-stack.html).

If you don’t want to use Confluent Cloud, you can also use this tutorial with a Kafka cluster running on your [local host](/platform/current/platform-quickstart.html) or any other remote server.

## Setup

1. <!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

   Clone the [confluentinc/examples](https://github.com/confluentinc/examples)
   GitHub repository and check out the `latest` branch.
   ```bash
   git clone https://github.com/confluentinc/examples
   cd examples
   git checkout latest
   ```
2. Change directory to the example for Rust.
   ```bash
   cd clients/cloud/rust/
   ```
3. <!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

   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](https://docs.confluent.io/cloud/current/client-apps/config-client.html)
   for instructions on how to manually find these values, or use the [ccloud-stack utility for Confluent Cloud](/cloud/current/examples/ccloud/docs/ccloud-stack.html) to automatically create them).
   - Template configuration file for Confluent Cloud
     ```none
     # 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
     ```none
     # Kafka
     bootstrap.servers=localhost:9092
     ```

## Basic Consumer and Producer

<!-- WARNING: THIS IS A SHARED FILE AND THE SOURCE IS LOCATED IN DOCS-COMMON. DO NOT ADD TO ANY OTHER REPO. -->

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

1. Build the producer and consumer binaries:
   ```bash
   cargo build
   ```

   You should see:
   ```text
   Compiling rust_kafka_client_example v0.1.0 (/path/to/repo/examples/clients/cloud/rust)
   Finished dev [unoptimized + debuginfo] target(s) in 2.85s
   ```
2. Run the producer, passing in arguments for:
   - the local file with configuration parameters to connect to your Kafka cluster
   - the topic name

   ```bash
   ./target/debug/producer --config $HOME/.confluent/librdkafka.config --topic test1
   ```
3. Verify the producer sent all the messages. You should see:
   ```text
   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
   ```
4. View the [producer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/rust/src/producer.rs).

### Consume Records

1. 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

   ```bash
   ./target/debug/consumer --config $HOME/.confluent/librdkafka.config --topic test1
   ```
2. Verify the consumer received all the messages. You should see:
   ```text
   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
   ```
3. View the [consumer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/rust/src/consumer.rs).

## Suggested Resources

* [How to build your first Apache Kafka Streams Application](https://kafka-tutorials.confluent.io/creating-first-apache-kafka-streams-application/confluent.html)
* [Getting started with Apache Kafka and your favorite language](https://developer.confluent.io/get-started/)
