C (librdkafka): Code Example for Apache Kafka®

In this tutorial, you will run a C 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

Configure SSL trust store

Depending on your operating system or Linux distribution you may need to take extra steps to set up the SSL CA root certificates. If your system doesn’t have the SSL CA root certificates properly set up, you may receive a SSL handshake failed error message similar to the following:

%3|1605776788.619|FAIL|rdkafka#producer-1| [thrd:sasl_ssl://...confluent.cloud:9092/bootstr]: sasl_ssl://...confluent.cloud:9092/bootstrap: SSL handshake failed: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed: broker certificate could not be verified, verify that ssl.ca.location is correctly configured or root CA certificates are installed (brew install openssl) (after 258ms in state CONNECT)

In this case, you need to manually install a bundle of validated CA root certificates and potentially modify the client code to set the ssl.ca.location configuration property. (For more information, see the documentation for librdkafka on which this client is built)

macOS

On newer versions of macOS (for example, 10.15), you may need to add an additional dependency.

For the Python client:

pip install certifi

For other clients:

brew install openssl

Once you install the CA root certificates, set the ssl.ca.location property in the client code. Edit both the producer and consumer code files, and add the ssl.ca.location configuration parameter into the producer and consumer properties. The value should correspond to the location of the appropriate CA root certificates file on your host.

For the Python client, use certifi.where() to determine the location of the certificate files:

ssl.ca.location: certifi.where()

For other clients, check the install path and provide it in the code:

ssl.ca.location: '/usr/local/etc/openssl@1.1/cert.pem'
CentOS

You may need to install CA root certificates in the following way:

sudo yum reinstall ca-certificates

This should be sufficient for the Kafka clients to find the certificates. However, if you still get the same error, you can set the ssl.ca.location property in the client code. Edit both the producer and consumer code files, and add the ssl.ca.location configuration parameter into the producer and consumer properties. The value should correspond to the location of the appropriate CA root certificates file on your host, for example:

ssl.ca.location: '/etc/ssl/certs/ca-bundle.crt'

Kafka Cluster

  • You can use this tutorial with a Kafka cluster in any environment:
  • If you are running on Confluent Cloud, you must have access to a Confluent Cloud cluster with an API key and secret.

Setup

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

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

    cd clients/cloud/c/
    
  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 }}
      
    • Template configuration file for local host

      # Kafka
      bootstrap.servers=localhost:9092
      

Basic Producer and Consumer

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 example producer and consumer applications.

    make
    
  2. Verify the build worked. You should see:

    cc   consumer.c common.c json.c -o consumer -lrdkafka -lm
    cc   producer.c common.c json.c -o producer -lrdkafka -lm
    
  3. Run the producer, passing in arguments for:

    • the local file with configuration parameters to connect to your Kafka cluster
    • the topic name
    ./producer test1 $HOME/.confluent/librdkafka.config
    
  4. Verify the producer sent all the messages. You should see:

    Creating topic test1
    Topic test1 successfully created
    Producing message #0 to test1: alice={ "count": 1 }
    Producing message #1 to test1: alice={ "count": 2 }
    Producing message #2 to test1: alice={ "count": 3 }
    Producing message #3 to test1: alice={ "count": 4 }
    Producing message #4 to test1: alice={ "count": 5 }
    Producing message #5 to test1: alice={ "count": 6 }
    Producing message #6 to test1: alice={ "count": 7 }
    Producing message #7 to test1: alice={ "count": 8 }
    Producing message #8 to test1: alice={ "count": 9 }
    Producing message #9 to test1: alice={ "count": 10 }
    Waiting for 10 more delivery results
    Message delivered to test1 [0] at offset 0 in 22.75ms: { "count": 1 }
    Message delivered to test1 [0] at offset 1 in 22.77ms: { "count": 2 }
    Message delivered to test1 [0] at offset 2 in 22.77ms: { "count": 3 }
    Message delivered to test1 [0] at offset 3 in 22.78ms: { "count": 4 }
    Message delivered to test1 [0] at offset 4 in 22.78ms: { "count": 5 }
    Message delivered to test1 [0] at offset 5 in 22.78ms: { "count": 6 }
    Message delivered to test1 [0] at offset 6 in 22.78ms: { "count": 7 }
    Message delivered to test1 [0] at offset 7 in 22.79ms: { "count": 8 }
    Message delivered to test1 [0] at offset 8 in 22.80ms: { "count": 9 }
    Message delivered to test1 [0] at offset 9 in 22.81ms: { "count": 10 }
    10/10 messages delivered
    
  5. View the producer code.

Consume Records

  1. Run the consumer, passing in arguments for:

    • the topic name you used earlier
    • the local file with configuration parameters to connect to your Kafka cluster.
    ./consumer test1 $HOME/.confluent/librdkafka.config
    
  2. Verify the consumer received all the messages. You should see:

    Subscribing to test1, waiting for assignment and messages...
    Press Ctrl-C to exit.
    Received message on test1 [0] at offset 0: { "count": 1 }
    User alice sum 1
    Received message on test1 [0] at offset 1: { "count": 2 }
    User alice sum 3
    Received message on test1 [0] at offset 2: { "count": 3 }
    User alice sum 6
    Received message on test1 [0] at offset 3: { "count": 4 }
    User alice sum 10
    Received message on test1 [0] at offset 4: { "count": 5 }
    User alice sum 15
    Received message on test1 [0] at offset 5: { "count": 6 }
    User alice sum 21
    Received message on test1 [0] at offset 6: { "count": 7 }
    User alice sum 28
    Received message on test1 [0] at offset 7: { "count": 8 }
    User alice sum 36
    Received message on test1 [0] at offset 8: { "count": 9 }
    User alice sum 45
    Received message on test1 [0] at offset 9: { "count": 10 }
    User alice sum 55
    
  3. Press Ctrl-C to exit.

  4. View the consumer code.