arrow_back

Confluent: Clickstream Data Analysis Pipeline Using ksqlDB

Join Sign in
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Confluent: Clickstream Data Analysis Pipeline Using ksqlDB

Lab 1 hour universal_currency_alt 5 Credits show_chart Intermediate
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, 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

Google Cloud self-paced labs logo

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 will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that 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 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.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your 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 pop-up opens for you to select your payment method. On the left is the Lab Details panel 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 panel.

  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 panel.

  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 view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
  1. Close the main Navigation Menu (Navigation menu icon) by clicking the three lines at the top left of the screen (hamburger), next to the Google Cloud logo.

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 Cloud Shell, clone the Confluent examples repository:
git clone https://github.com/confluentinc/examples.git
  1. Navigate to the examples/clickstream directory and switch to the Confluent Platform release branch:
cd examples/clickstream git checkout 5.5.0-post
  1. Use a sed command to change the client version.
sed -i "s/version: '2'/version: '3'/g" docker-compose.yml
  1. Run the below command to update the grafana port.
sed -i "s/3000:3000/4000:3000/g" docker-compose.yml
  1. Get the Jar files for kafka-connect-datagen (source connector) and kafka-connect-elasticsearch (sink connector):
sudo service docker status sudo service docker start 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
  1. Increase the virtual memory for elasticsearch sink connector:
sudo sysctl -w vm.max_map_count=262144
  1. Now, launch the tutorial in Docker:
docker-compose up -d
  1. 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:

Output that displays 9 files under four column headings: Name, Command, State, and Ports

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
  1. 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 ---------------------------------------
  1. After the RUN SCRIPT command completes, exit out of the ksqldb-cli with the CTRL+D command. You can also type 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
  1. 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"}
  1. 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"}
  1. Go to the Confluent Control Center UI. Click Web Preview in Cloud Shell, then click Preview on port 8080. It should open up a new page.

The expanded Web preview menu dispalying the Preview on port 8080 option

  1. Now, in the URL, change the beginning of the line from 8080 to 9021 and refresh the page. Your URL should now look something like: https://9021-cs-5ed290dd-ea41-43ed-9788-1e8245e4bc27.ql-asia-southeast1-dsfl.cloudshell.dev/?authuser=0.

  2. 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.

The Connectors page displaying the Connectors list which includes three running connectors

Task 3. Load the streaming data to ksqlDB

  1. Back in Cloud Shell, launch the ksqlDB CLI:
docker-compose exec ksqldb-cli ksql http://ksqldb-server:8088
  1. 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';

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;
  1. Press CTRL+D to terminate the above Query.

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 with the CTRL+D command. You can also type 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'
  1. 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'
  1. Now, you can the dashboard into Grafana:
docker-compose exec grafana bash -c '/scripts/clickstream-analysis-dashboard.sh'
  1. 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 Grafana dashboard. In Cloud Shell, click Web Preview, then click Change port and enter 4000. Click CHANGE AND PREVIEW.

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

  3. One 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.

Manage tabbed page displaying Dashboard list

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.

Click Stream Analysis page

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

  2. Once you've viewed and tested your data sources, you go back to Cloud Shell 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 08, 2024

Lab Last Tested April 08, 2024

Copyright 2024 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.