<!-- 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-kcat"></a>

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

In this tutorial, you will run a `kcat` (formerly known as `kafkacat`) 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

- [kcat](https://github.com/edenhill/kcat) installed on your
  machine. You must [build](https://github.com/edenhill/kcat#build)
  kcat from the latest master branch to get the `-F` functionality
  that makes it easy to pass in the configuration to your Confluent Cloud
  configuration file.
- Local install of [Confluent CLI](https://docs.confluent.io/confluent-cli/current/overview.html) v2.5.0 or later.

### 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 kcat.
   ```bash
   cd clients/cloud/kcat/
   ```
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 Producer and Consumer

<!-- 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. Create the Kafka topic. If you are using Confluent Cloud then use the Confluent CLI:
   ```text
   confluent kafka topic create --if-not-exists test1
   ```

   If you are using a local Kafka cluster then use the Kafka CLI:
   ```text
   kafka-topics --bootstrap-server localhost:9092 --topic $topic_name --create --if-not-exists
   ```
2. Run kcat, writing messages to topic `test1`, passing in arguments for:
   - `-F $HOME/.confluent/librdkafka.config`: configuration file for
     connecting to the Confluent Cloud cluster
   - `-K ,`: pass key and value, separated by a comma

   ```bash
   kcat -F $HOME/.confluent/librdkafka.config -K , -P -t test1
   ```
3. Type a few messages, using a `,` as the separator between the message key
   and value:
   ```text
   alice,{"count":0}
   alice,{"count":1}
   alice,{"count":2}
   ```
4. When you are done, press `CTRL-D`.
5. View the [producer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/kcat/kcat-example.sh).

### Consume Records

1. Run kcat again, reading messages from topic `test`, passing
   in arguments for:
   - `-F $HOME/.confluent/librdkafka.config`: configuration file for
     connecting to the Confluent Cloud cluster
   - `-K ,`: pass key and value, separated by a comma
   - `-e`: exit successfully when last message received

   ```bash
   kcat -F $HOME/.confluent/librdkafka.config -K , -C -t test1 -e
   ```

   You should see the messages you typed earlier.
   ```bash
   % Reading configuration from file $HOME/.confluent/librdkafka.config
   % Reached end of topic test1 [3] at offset 0
   alice,{"count":0}
   alice,{"count":1}
   alice,{"count":2}
   % Reached end of topic test1 [7] at offset 0
   % Reached end of topic test1 [4] at offset 0
   % Reached end of topic test1 [6] at offset 0
   % Reached end of topic test1 [5] at offset 0
   % Reached end of topic test1 [1] at offset 0
   % Reached end of topic test1 [2] at offset 0
   % Reached end of topic test1 [9] at offset 0
   % Reached end of topic test1 [10] at offset 0
   % Reached end of topic test1 [0] at offset 0
   % Reached end of topic test1 [8] at offset 0
   % Reached end of topic test1 [11] at offset 3: exiting
   ```
2. View the [consumer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/kcat/kcat-example.sh).
