
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Creating a Apache Kafka deployment manager
/ 40
Create topics in Kafka
/ 30
Process the input data with Kafka Streams
/ 30
This lab was developed with our partner, Confluent. Your personal information may be shared with Confluent, the lab sponsor, if you have opted-in to receive product updates, announcements, and offers in your Account Profile.
In this lab, you create a streaming data pipeline with Kafka providing you a hands-on look at the Kafka Streams API. You will run a Java application that uses the Kafka Streams library by showcasing a simple end-to-end data pipeline powered by Apache Kafka®.
In this lab, you will:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
In the Cloud Console, open the Navigation menu and click Marketplace.
Locate the Apache Kafka® deployment by searching for Apache Kafka.
Click on Apache Kafka Server on Ubuntu Server 20.04. It should look like this:
Click on Get Started, and Accept the Terms and Conditions checkbox and then click the Agree button.
Click Deploy and click on Enable the Required APIs.
Under New Apache Kafka Server on Ubuntu Server 20.04 deployment page.
Select Deployment Service Account as New account and enter the Service account name as apache-kafka
Select a zone
Under Machine Type, change the Series to E2, and select the e2-medium
Machine type.
Leave all the other values as default, and click Deploy.
Once the deployment completes, the VM instance is available in the zone you selected.
Click Check my progress to verify the objective.
While you're waiting for deployment, you can check out this quick start which shows how to run the WordCount demo application that is included in Kafka.
Here's the gist of the code, converted to use Java 8 lambda expressions so that it is easier to read (taken from the variant WordCountLambdaExample):
In the Console, open the Navigation Menu and select Compute Engine > VM Instances.
Next to the VM name kafka-1-vm
, click the SSH button to connect to the Kafka VM.
For reference, the installation of Apache Kafka is in the following directory: /opt/kafka/
.
In the SSH window, you will run the following commands to start all services in the correct order.
You will now need to open another terminal session to complete the next steps.
Once all services have successfully launched, you will have a basic Kafka environment running and ready to use.
You will now need to open one final terminal session to complete the next steps.
You will now send some input data to a Kafka topic, which will be subsequently processed by a Kafka Streams application.
Now you'll need to create the input topic streams-plaintext-input
.
streams-wordcount-output
:Click Check my progress to verify the objective.
/tmp/file-input.txt
:The resulting file will have the following contents:
all streams lead to kafka
hello kafka streams
join kafka summit
The Kafka console producer reads the data from STDIN
line-by-line, and publishes each line as a separate Kafka message to the topic streams-plaintext-input
, where the message key is null
and the message value is the respective line such as all streams lead to kafka
, encoded as a string.
Now that you have generated some input data, you can run your first Kafka Streams based Java application.
You will run the WordCount demo application, which is included in Kafka. It implements the WordCount algorithm, which computes a word occurrence histogram from an input text.
However, unlike other WordCount examples you might have seen before that operate on finite, bounded data, the WordCount demo application behaves slightly differently because it is designed to operate on an infinite, unbounded stream of input data.
Similar to the bounded variant, it is a stateful algorithm that tracks and updates the counts of words. However, since it must assume potentially unbounded input data, it will periodically output its current state and results while continuing to process more data because it cannot know when it has processed "all" the input data.
This is a typical difference between the class of algorithms that operate on unbounded streams of data and, say, batch processing algorithms such as Hadoop MapReduce. It will be easier to understand this difference once you inspect the actual output data later on.
Kafka's WordCount demo application is bundled with Confluent Platform, which means you can run it without further ado, i.e. you do not need to compile any Java sources and so on.
The WordCount demo application will read from the input topic streams-plaintext-input
, perform the computations of the WordCount algorithm on the input data, and continuously write its current results to the output topic streams-wordcount-output
(the names of its input and output topics are hardcoded). You can terminate the demo at any point by entering Ctrl+C
from the keyboard.
On the VM Instances page, click the SSH button next to the VM name kafka-1-vm
to start a new connection to the instance.
You can now inspect the output of the WordCount demo application by reading from its output topic streams-wordcount-output
:
The following output data should be printed to the console:
Here, the first column is the Kafka message key in java.lang.String
format, and the second column is the message value in java.lang.Long
format. You can terminate the console consumer at any point by entering Ctrl+C
from the keyboard.
As discussed above, a streaming word count algorithm continuously computes the latest word counts from the input data, and, in this specific demo application, continuously writes the latest counts of words as its output.
You can check out the Confluent documentation to learn more about the duality between streams and tables. In fact, the output you have seen above is actually the changelog stream of a KTable, with the KTable being the result of the aggregation operation performed by the WordCount demo application.
Click Check my progress to verify the objective.
Once you are done with the previous steps, you can shut down the Kafka cluster in the following order:
Ctrl+C
in the terminal it is running in. Alternatively, you can kill
the broker process.Ctrl+C
in its respective terminal. Alternatively, you can kill
the ZooKeeper process.You have now run your first Kafka Streams applications against data stored in a single-node Kafka cluster. In this lab you started a Kafka cluster, used the console producer to write example input data to a Kafka topic, and processed the data with WordCount using the Kafka Streams library. You then inspected the output data using the console consumer and stopped the Kakfa cluster.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual last updated November 05, 2024
Lab last tested November 05, 2024
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one