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

Kafka Cluster

The easiest way to follow this tutorial is with Confluent Cloud because you don’t have to run a local Kafka cluster. 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

  1. Clone the confluentinc/examples GitHub repository and check out the 7.1.10-post branch.

    git clone https://github.com/confluentinc/examples
    cd examples
    git checkout 7.1.10-post
    
  2. Change directory to the example for Rust.

    cd clients/cloud/rust/
    
  3. 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 }}
      
      # Best practice for higher availability in librdkafka clients prior to 1.7
      session.timeout.ms=45000
      
    • 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

  1. 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
    
  2. 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
    
  3. 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
    
  4. View the producer code.

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
    ./target/debug/consumer --config $HOME/.confluent/librdkafka.config --topic test1
    
  2. 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
    
  3. View the consumer code.