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

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

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

- Java 1.8 or higher to run the demo application.

### 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 Groovy.
   ```bash
   cd clients/cloud/groovy/
   ```
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/java.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.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username='{{ CLUSTER_API_KEY }}' password='{{ CLUSTER_API_SECRET }}';
     sasl.mechanism=PLAIN
     # Required for correctness in Apache Kafka clients prior to 2.6
     client.dns.lookup=use_all_dns_ips

     # Best practice for higher availability in Apache Kafka clients prior to 3.0
     session.timeout.ms=45000

     # Best practice for Kafka producer to prevent data loss 
     acks=all
     ```
   - 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. Build the client examples:
   ```text
   ./gradlew clean build
   ```
2. Run the producer, passing in arguments for:
   - the local file with configuration parameters to connect to your Kafka cluster
   - the topic name

   ```text
   ./gradlew runApp -PmainClass="io.confluent.examples.clients.cloud.ProducerExample" \
      -PconfigPath="$HOME/.confluent/java.config" \
      -Ptopic="test1"
   ```
3. Verify the producer sent all the messages. You should see:
   ```text
   ...
   Producing record: alice        {"count":0}
   Producing record: alice        {"count":1}
   Producing record: alice        {"count":2}
   Producing record: alice        {"count":3}
   Producing record: alice        {"count":4}
   Producing record: alice        {"count":5}
   Producing record: alice        {"count":6}
   Producing record: alice        {"count":7}
   Producing record: alice        {"count":8}
   Producing record: alice        {"count":9}
   Produced record to topic test1 partition [0] @ offset 0
   Produced record to topic test1 partition [0] @ offset 1
   Produced record to topic test1 partition [0] @ offset 2
   Produced record to topic test1 partition [0] @ offset 3
   Produced record to topic test1 partition [0] @ offset 4
   Produced record to topic test1 partition [0] @ offset 5
   Produced record to topic test1 partition [0] @ offset 6
   Produced record to topic test1 partition [0] @ offset 7
   Produced record to topic test1 partition [0] @ offset 8
   Produced record to topic test1 partition [0] @ offset 9
   10 messages were produced to topic test1
   ...
   ```
4. View the [producer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/groovy/src/main/groovy/io/confluent/examples/clients/cloud/ProducerExample.groovy).

### Consume Records

1. Run the consumer, passing in arguments for:
   - the local file with configuration parameters to connect to your Kafka cluster
   - the same topic name you used earlier.

   ```bash
   ./gradlew runApp -PmainClass="io.confluent.examples.clients.cloud.ConsumerExample"\
        -PconfigPath="$HOME/.confluent/java.config"\
        -Ptopic="test1"
   ```
2. Verify the consumer received all the messages. You should see:
   ```text
   ...
   Consumed record with key alice and value {"count":0}, and updated total count to 0
   Consumed record with key alice and value {"count":1}, and updated total count to 1
   Consumed record with key alice and value {"count":2}, and updated total count to 3
   Consumed record with key alice and value {"count":3}, and updated total count to 6
   Consumed record with key alice and value {"count":4}, and updated total count to 10
   Consumed record with key alice and value {"count":5}, and updated total count to 15
   Consumed record with key alice and value {"count":6}, and updated total count to 21
   Consumed record with key alice and value {"count":7}, and updated total count to 28
   Consumed record with key alice and value {"count":8}, and updated total count to 36
   Consumed record with key alice and value {"count":9}, and updated total count to 45
   ```
3. When you are done, press `CTRL-C`.
4. View the [consumer code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/groovy/src/main/groovy/io/confluent/examples/clients/cloud/ConsumerExample.groovy).

### Kafka Streams

1. Run the Kafka Streams application, passing in arguments for:
   - the local file with configuration parameters to connect to your Kafka cluster
   - the same topic name you used earlier

   ```bash
   ./gradlew runApp -PmainClass="io.confluent.examples.clients.cloud.StreamsExample" \
        -PconfigPath="$HOME/.confluent/java.config" \
        -Ptopic="test1"
   ```
2. Verify the consumer received all the messages. You should see:
   ```text
   ...
   [Consumed record]: alice, 0
   [Consumed record]: alice, 1
   [Consumed record]: alice, 2
   [Consumed record]: alice, 3
   [Consumed record]: alice, 4
   [Consumed record]: alice, 5
   [Consumed record]: alice, 6
   [Consumed record]: alice, 7
   [Consumed record]: alice, 8
   [Consumed record]: alice, 9
   ...
   [Running count]: alice, 0
   [Running count]: alice, 1
   [Running count]: alice, 3
   [Running count]: alice, 6
   [Running count]: alice, 10
   [Running count]: alice, 15
   [Running count]: alice, 21
   [Running count]: alice, 28
   [Running count]: alice, 36
   [Running count]: alice, 45
   ...
   ```
3. When you are done, press `CTRL-C`.
4. View the [Kafka Streams code](https://github.com/confluentinc/examples/tree/latest/clients/cloud/groovy/src/main/groovy/io/confluent/examples/clients/cloud/StreamsExample.groovy).

## 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/)
