Loading...
No results found.

Apply your skills in Google Cloud console

Getting Started with Apache Kafka and Confluent Platform on Google Cloud

Get access to 700+ labs and courses

Confluent: Clickstream Data Analysis Pipeline Using ksqlDB

Lab 1 hour universal_currency_alt 5 Credits show_chart Intermediate
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

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.

GSP731

Overview

This lab will cover how to create a clickstream data analysis pipeline using ksqlDB. First, you will run multiple Docker containers and create source connectors that generate clickstream data, which you will be able to view in the Confluent Control Center UI. You will then load the data into ksqlDB, and verify that it is being streamed through various tables and streams. Next, you will send the ksqlDB tables to Elasticsearch and Grafana via the ksqlDB REST API. Lastly, you will view the streaming data in the Grafana dashboard.

What you'll learn

In this lab, you will:

  • Download and run the Confluent tutorial
  • Create mock clickstream data
  • Load the streaming data to ksqlDB
  • Send the ksqlDB tables to Elasticsearch and Grafana
  • Sessionize the data
  • View the data in Grafana

Setup and requirements

Before you click the Start Lab button

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:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

How to start your lab and sign in to the Google Cloud console

  1. 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:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. 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.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details pane.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details pane.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

Task 1. Download and run the tutorial

The tutorial is built using Docker Compose. It brings together several Docker images with the required networking and dependencies. The images are quite large and depending on your network connection may take a few minutes to download.

  1. In the Cloud console, on the Navigation menu (), click Compute Engine > VM instances. Your instance should be listed.

  2. In your provided VM instance, click SSH to start your SSH session. Accept the authorization prompts when the new SSH window opens.

  3. In your SSH session, to set the active account as the username provided in the lab details panel, run:

    gcloud config set account {{{user_0.username | "STUDENT USER ACCOUNT"}}}
  4. Now, clone the Confluent examples repository:

    git clone https://github.com/confluentinc/examples.git
  5. Navigate to the examples/clickstream directory and switch to the Confluent Platform release branch:

    cd examples/clickstream git checkout 5.5.0-post
  6. Use a sed command to change the client version.

    sed -i "s/version: '2'/version: '3'/g" docker-compose.yml
  7. Run the below command to update the grafana port.

    sed -i "s/3000:3000/4000:3000/g" docker-compose.yml
  8. Get the Jar files for kafka-connect-datagen (source connector) and kafka-connect-elasticsearch (sink connector):

    sudo service docker status Note: Press CTRL+C to terminate the above Query. sudo service docker start
  9. Add user to the docker group and refresh your current terminal session:

    export username=$(whoami) sudo usermod -aG docker $username sudo su -l $username cd examples/clickstream git checkout 5.5.0-post docker run -v $PWD/confluent-hub-components:/share/confluent-hub-components confluentinc/ksqldb-server:0.8.0 confluent-hub install --no-prompt confluentinc/kafka-connect-datagen:0.3.2 docker run -v $PWD/confluent-hub-components:/share/confluent-hub-components confluentinc/ksqldb-server:0.8.0 confluent-hub install --no-prompt confluentinc/kafka-connect-elasticsearch:5.4.1 Note: If you are getting permission denied error please try restarting your SSH session and navigate to the examples/clickstream directory and switch to the Confluent Platform release branch.
  10. Increase the virtual memory for elasticsearch sink connector:

    sudo sysctl -w vm.max_map_count=262144
  11. Remove Unused Docker Images/Containers:

    sudo docker system prune -af
  12. Now, launch the tutorial in Docker:

    docker-compose up -d
  13. After a minute or so, run the docker-compose ps status command to ensure that everything has started correctly:

    docker-compose ps

    Your output should resemble:

    Note: Re-run the docker-compose ps command until your ksqldb-server is in Up (healthy) state before moving forward to the next steps.

    Click Check my progress to verify the objective. Clone the Confluent examples repository.

Task 2. Create the Clickstream data

Once you've confirmed all the Docker containers are running, create the source connectors that generate mock data. This lab leverages the embedded Connect worker in ksqlDB.

  1. Launch the ksqlDB CLI:

    Note: Make sure your ksqldb-server is Up (healthy) and running before you execute the following command. This may take several minutes to start up. docker-compose exec ksqldb-cli ksql http://ksqldb-server:8088
  2. Run the script create-connectors.sql that executes the ksqlDB statements to create three source connectors for generating mock data:

    RUN SCRIPT '/scripts/create-connectors.sql';

    The output will be similar to this:

    CREATE SOURCE CONNECTOR datagen_clickstream_codes WITH ( 'connector.class' = 'io.confluent.kafka.connect.datagen.DatagenConnector', 'kafka.topic' = 'clickstream_codes', 'quickstart' = 'clickstream_codes', 'maxInterval' = '20', 'interations' = '100', 'format' = 'json', 'key.converter' = 'org.apache.kafka.connect.converters.IntegerConverter'); Message --------------------------------------------- Created connector DATAGEN_CLICKSTREAM_CODES --------------------------------------------- CREATE SOURCE CONNECTOR datagen_clickstream_users WITH ( 'connector.class' = 'io.confluent.kafka.connect.datagen.DatagenConnector', 'kafka.topic' = 'clickstream_users', 'quickstart' = 'clickstream_users', 'maxInterval' = '10', 'interations' = '1000', 'format' = 'json', 'key.converter' = 'org.apache.kafka.connect.converters.IntegerConverter'); Message --------------------------------------------- Created connector DATAGEN_CLICKSTREAM_USERS --------------------------------------------- CREATE SOURCE CONNECTOR datagen_clickstream WITH ( 'connector.class' = 'io.confluent.kafka.connect.datagen.DatagenConnector', 'kafka.topic' = 'clickstream', 'quickstart' = 'clickstream', 'maxInterval' = '30', 'format' = 'json'); Message --------------------------------------- Created connector DATAGEN_CLICKSTREAM ---------------------------------------
  3. After the RUN SCRIPT command completes, exit out of the ksqldb-cli by typing exit in the prompt to go back to your terminal. Now the clickstream generator is running, simulating the stream of clicks. Sample these messages in clickstream:

    docker-compose exec tools confluent local consume clickstream -- --bootstrap-server kafka:29092 --property print.key=true --max-messages 5
  4. The second data generator running is for the HTTP status codes. Sample these messages in clickstream_codes to view the HTTP status codes:

    docker-compose exec tools confluent local consume clickstream_codes -- --bootstrap-server kafka:29092 --from-beginning --property print.key=true --property key.deserializer=org.apache.kafka.common.serialization.IntegerDeserializer --max-messages 3

    Your output should resemble something like the following:

    407 {"code":407,"definition":"Proxy authentication required"} 302 {"code":302,"definition":"Redirect"} 407 {"code":407,"definition":"Proxy authentication required"}
  5. The third data generator is for the user information. Samples these messages in clickstream_users to view the user information:

    docker-compose exec tools confluent local consume clickstream_users -- --bootstrap-server kafka:29092 --from-beginning --property print.key=true --property key.deserializer=org.apache.kafka.common.serialization.IntegerDeserializer --max-messages 3

    Your output should resemble:

    1 {"user_id":1,"username":"Ferd88","registered_at": 1490759617459,"first_name":"Curran", "last_name":"Vanyard","city":"Palo Alto","level":"Gold"} 2 {"user_id":2,"username":"bobk_43","registered_at": 1457530469406,"first_name":"Antonio", "last_name":"De Banke","city":"London","level":"Platinum"} 3 {"user_id":3,"username":"Ferd88","registered_at": 1481373367271,"first_name":"Dimitri", "last_name":"Jushcke","city":"Raleigh","level":"Silver"}

    Click Check my progress to verify the objective. Create source connectors.

  6. Navigate to the Compute Engine > VM Instances page and copy the External IP of your VM instance. Paste it in a new browser tab and add :9021 along with the IP. Your URL should now look something like: http://34.86.135.207:9021/. Press Enter.

    If required, click Continue to site.

  7. Click Cluster 1 > Connect > connect-default.

    You should now be able to view the three kafka-connect-datagen source connectors created with the ksqlDB CLI.

Task 3. Load the streaming data to ksqlDB

  1. Back in your SSH session, launch the ksqlDB CLI:

    docker-compose exec ksqldb-cli ksql http://ksqldb-server:8088
  2. Load the statements.sql file that runs the tutorial app. Important: before running this step, you must have already run ksql-datagen utility to create the clickstream data, status codes, and set of users.

    RUN SCRIPT '/scripts/statements.sql';

    Click Check my progress to verify the objective. Load streaming data to ksqlDB.

Verify the data

  1. Go back to the Confluent Control Center UI. Navigate to the ksqlDB menu, click on your clickstream application, and then click on Flow.

  2. Go back to the ksqlDB CLI and verify that data is being streamed through various tables and streams. Query one of the streams CLICKSTREAM:

    select * from CLICKSTREAM emit changes LIMIT 100;

Task 4. Load the Clickstream data in Grafana

In this section, you'll send the ksqlDB tables to Elasticsearch and Grafana.

  1. Exit the ksqldb-cli by typing exit in the prompt to go back to your terminal.

  2. Set up the required Elasticsearch document mapping template:

    docker-compose exec elasticsearch bash -c '/scripts/elastic-dynamic-template.sh'
  3. Run this command to send the ksqlDB tables to Elasticsearch and Grafana:

    docker-compose exec ksqldb-server bash -c '/scripts/ksql-tables-to-grafana.sh'
  4. Now, you can load the dashboard into Grafana:

    docker-compose exec grafana bash -c '/scripts/clickstream-analysis-dashboard.sh'
  5. In the Confluent Control Center UI, view the running connectors again. The three kafka-connect-datagen source connectors were created with the ksqlDB CLI, and the seven Elasticsearch sink connectors were created with the ksqlDB REST API.

Task 5. Sessionize the data

One of the tables created by the demo, CLICK_USER_SESSIONS, shows a count of user activity for a given user session. All clicks from the user count towards the total user activity for the current session. If a user is inactive for 30 seconds, then any subsequent click activity is counted towards a new session.

The clickstream demo simulates user sessions with a script. The script pauses the DATAGEN_CLICKSTREAM connector every 90 seconds for a 35 second period of inactivity. By stopping the DATAGEN_CLICKSTREAMconnector for some time greater than 30 seconds, you will see distinct user sessions.

You'll probably use a longer inactivity gap for session windows in practice. But the demo uses 30 seconds so you can see the sessions in action in a reasonable amount of time.

Session windows are different because they monitor user behavior and other window implementations consider only time.

  • To generate the session data execute the following statement from the examples/clickstream directory. The script will issue some statements to the console about where it is in the process.

    cd ~/examples/clickstream ./sessionize-data.sh

Task 6. View the data in Grafana

  1. Navigate to the Compute Engine > VM Instances page and copy the External IP of your VM instance. Paste it in a new browser tab and add :4000 along with the IP. Your URL should now look something like: http://34.86.135.207:4000/. Press Enter.

  2. The Grafana login interface should appear. You can login with user ID user and password user.

  3. Once you're in, from the left-side pane, click the tile icon: Dashboards > Manage. Now you can click your Click Stream Analysis Dashboard to enter.

    This dashboard demonstrates a series of streaming functionality where the title of each panel describes the type of stream processing required to generate the data. For example, the large chart in the middle is showing web-resource requests on a per-username basis using a Session window - where a session expires after 300 seconds of inactivity. Editing the panel allows you to view the datasource - which is named after the streams and tables captured in the statements.sql file.

  4. To check the Data Sources page in Grafana, click the Configuration gear icon on the left-side pane, and go to Data Sources. Select one of your data sources and scroll to the bottom and click the Save & Test button. This will indicate whether your data source is valid.

  5. Once you've viewed and tested your data sources, you go back to SSH terminal and exit out of the clickstream demo with the CTRL+C command.

Task 7. Test your understanding

Below are multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

Congratulations!

In this lab, you ran multiple Docker containers, created source connectors that generate mock clickstream data and loaded them into ksqlDB. You then sent the ksqlDB tables to Elasticsearch and Grafana, sessionized the data, and viewed the data in Grafana.

Next steps / Learn more

Google Cloud training and certification

...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 April 29, 2025

Lab Last Tested April 29, 2025

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.

Previous Next

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

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

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
Preview