Important
You are viewing documentation for an older version of Confluent Platform. For the latest, click here.
Scala¶
In this tutorial, you will run a Scala client application that produces messages to and consumes messages from an Apache Kafka® cluster.
After you run the tutorial, view the provided source code and use it as a reference to develop your own Kafka client application.
Prerequisites¶
Kafka Cluster¶
- You can use this tutorial with a Kafka cluster in any environment:
- In Confluent Cloud
- On your local host
- Any remote Kafka cluster
- If you are running on Confluent Cloud, you must have access to a Confluent Cloud cluster
- The first 20 users to sign up for Confluent Cloud and use promo code
C50INTEG
will receive an additional $50 free usage (details)
- The first 20 users to sign up for Confluent Cloud and use promo code
Setup¶
Clone the confluentinc/examples GitHub repository and check out the
5.5.15-post
branch.git clone https://github.com/confluentinc/examples cd examples git checkout 5.5.15-post
Change directory to the example for Scala.
cd clients/cloud/scala/
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 Connecting Clients to Confluent Cloud for instructions on how to create or find those values).Template configuration file for Confluent Cloud
# Kafka 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 }}"; ssl.endpoint.identification.algorithm=https sasl.mechanism=PLAIN
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.
Consume Records¶
Build the client examples:
sbt clean compile
Run the consumer:
sbt “runMain io.confluent.examples.clients.scala.Consumer $HOME/.confluent/java.config test1”
You should see:
<snipped> Polling .... <snipped>
View the consumer code.
Kafka Streams¶
In a new window, run the Streams app:
cd examples/clients/cloud/scala
Build the client examples:
sbt clean compile
Run the streams app:
sbt "runMain io.confluent.examples.clients.scala.Streams $HOME/.confluent/java.config test1"
View the Kafka Streams code.
Produce Records¶
In new a window, run the Kafka producer application to write records to the Kafka cluster:
sbt "runMain io.confluent.examples.clients.scala.Producer $HOME/.confluent/java.config test1"
You see the following output:
<snipped> Produced record at test1-0@120 Produced record at test1-0@121 Produced record at test1-0@122 Produced record at test1-0@123 Produced record at test1-0@124 Produced record at test1-0@125 Produced record at test1-0@126 Produced record at test1-0@127 Produced record at test1-0@128 Produced record at test1-0@129 Wrote ten records to test1 [success] Total time: 6 s, completed 10-Dec-2018 16:50:13
In the consumer window, verify you see the following output:
<snipped> Polling 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 Consumed record with key alice and value {"count":10}, and updated total count to 55 Polling
In the streams app, verify you see the following output:
[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 [Consumed record]: alice, 10 [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 [Running count]: alice, 55
When you are done, press
CTRL-C
in both windows to stop the Consumer and Streams.View the producer code.