arrow_back

Streaming Data to Bigtable

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

Streaming Data to Bigtable

Lab 1 heure 30 minutes universal_currency_alt 1 crédit show_chart Débutant
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP1055

Google Cloud self-paced labs logo

Overview

Bigtable is Google's fully managed, scalable NoSQL database service. Bigtable is ideal for storing large amounts of data in a key-value store and for use cases such as personalization, ad tech, financial tech, digital media, and Internet of Things (IoT). Bigtable supports high read and write throughput at low latency for fast access to large amounts of data for processing and analytics.

For streaming data from sensors, Bigtable can handle high writes to capture large volumes of real-time data.

In this lab, you use commands to create a Bigtable instance with a table to store simulated traffic sensor data. Then you launch a Dataflow pipeline to load the simulated streaming data from Pub/Sub into Bigtable. While the Dataflow job loads streaming data from Pub/Sub into Bigtable, you verify that the table is being successfully populated. You complete the lab by stopping the streaming job and deleting the Bigtable data.

What you'll do

In this lab, you learn how to create a Bigtable instance and table using commands and use Dataflow to load streaming data.

  • Create a Bigtable instance using Google Cloud CLI (gcloud CLI) commands.
  • Create a Bigtable table with column families using Cloud Bigtable CLI (cbt CLI) commands.
  • Launch a Dataflow pipeline to read streaming data from Pub/Sub and write into Bigtable.
  • Verify the streaming data loaded into Bigtable.
  • Delete a Bigtable table and a Bigtable instance using commands.

Prerequisites

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.

Task 1. Create a Bigtable instance and table using commands

To create a new table in Bigtable, you first need to create a Bigtable instance to store your table. To create a Bigtable instance, you can use the Google Cloud console, gcloud CLI commands, or cbt CLI commands.

In this task, you use Cloud Shell to first run gcloud CLI commands to create a new Bigtable instance, and then run cbt CLI commands to connect to Bigtable and create a new table.

For a review of how to access Cloud Shell, click Setup and requirements on the right-side menu of this page.

Create a Bigtable instance

  • To create a new Bigtable instance, in Cloud Shell, run the following command:
gcloud bigtable instances create sandiego \ --display-name="San Diego Traffic Sensors" \ --cluster-storage-type=SSD \ --cluster-config=id=sandiego-traffic-sensors-c1,zone={{{project_0.default_zone | ZONE}}},nodes=1

This command creates a new Bigtable instance with the following properties:

Property Value
Instance ID sandiego
Instance display name San Diego Traffic Sensors
Storage Type SSD
Cluster ID sandiego-traffic-sensors-c1
Zone
Node scaling mode Manual allocation
Number of nodes 1

When you receive the output message, continue to the next step.

Creating bigtable instance sandiego...done.

Configure the Bigtable CLI

To connect to Bigtable using cbt CLI commands, you first need to use Cloud Shell to update the .cbtrc configuration file with your project ID and your Bigtable instance ID.

  1. To modify the .cbtrc file with the project ID and instance ID, run the following commands:
echo project = `gcloud config get-value project` \ >> ~/.cbtrc echo instance = sandiego \ >> ~/.cbtrc
  1. To verify that you have successfully modified the .cbtrc file, run the following command:
cat ~/.cbtrc

The output should resemble the following:

project = <project-id> instance = sandiego

Create a Bigtable table with column families

After you configure the .cbtrc configuration file in Cloud Shell, you can run a simple cbt CLI command to create a new Bigtable table with column families.

  • To create a new table named current_conditions with one column family named lane, run the following command:
cbt createtable current_conditions \ families="lane"

Click Check my progress to verify the objective. Create a Bigtable instance and table.

Task 2. Simulate streaming traffic sensor data into Pub/Sub

In this task, you run a streaming data simulator from a Compute Engine virtual machine (VM) that has been created for this lab. To begin this task, you will enter commands on a VM named training-vm to set up your environment and download the necessary files for the streaming data simulator.

Connect to the VM

  1. In the Google Cloud console, on the Navigation menu, click Compute Engine > VM instances.

  2. Locate the line with the instance called training-vm, and under Connect, click SSH.

    A terminal window for training-vm will open.

    The training-vm is installing some software in the background. In the next step, you verify that setup is complete by checking the contents of the new directory.

  3. To list the contents of the directory named training, run the following command:

ls /training

The VM is ready for you to continue when the output of the ls command yields the following result:

bq_magic.sh project_env.sh sensor_magic.sh

If the three scripts are not listed, wait a few minutes and try again.

Note: It may take 2 to 3 minutes for all background actions to complete.

Run script to simulate streaming data

  1. To download a code repository for use in this lab, run the following command:
git clone https://github.com/GoogleCloudPlatform/training-data-analyst
  1. To set up the required environmental variables, run the following command:
source /training/project_env.sh

This script sets the $DEVSHELL_PROJECT_ID and $BUCKET environment variables so that you do not have to manually set these variables for Project ID and Cloud Storage bucket name.

A Cloud Storage bucket was created for you during the initialization of lab resources.

  1. To start the streaming data simulator, run the following command:
/training/sensor_magic.sh

This script reads sample data from a CSV file and publishes it to Pub/Sub. This script will send one hour of data in one minute.

Let the script continue to run in the current terminal, and continue with the next tasks.

Click Check my progress to verify the objective. Simulate streaming traffic sensor data into Pub/Sub.

Task 3. Launch a Dataflow pipeline to write data from Pub/Sub into Bigtable

In this task, you open a second SSH terminal on training_vm and run commands to launch a Dataflow job to write streaming data from Pub/Sub into Bigtable.

Open a second SSH terminal

  1. In the current terminal window, click Terminal settings (ssh_gear_settings.png), and then click New connection.

A second terminal window will open. This new terminal session will not have the required environment variables. In the next step, you set these variables on the new terminal session.

  1. To set the environment variables in the new terminal, run the following command:
source /training/project_env.sh

This script sets the $DEVSHELL_PROJECT_ID and $BUCKET environment variables in the new terminal window.

Launch a Dataflow Pipeline

  1. To navigate to the code directory in the new terminal, run the following command:
cd ~/training-data-analyst/courses/streaming/process/sandiego
  1. To review the script using nano, run the following command:
nano run_oncloud.sh

Do not modify the code.

This script takes three required arguments to run a Dataflow job:

  • Project ID
  • Cloud Storage bucket name
  • Java classname
  • optional fourth argument for options

In the next steps, you use the --bigtable option to direct the Dataflow pipeline to write data into Bigtable.

  1. To exit nano, press CTRL+X.

  2. To launch the Dataflow pipeline to read from Pub/Sub and write into Bigtable, run the following command:

./run_oncloud.sh $DEVSHELL_PROJECT_ID $BUCKET CurrentConditions --bigtable

When the pipeline has been launched successfully, you will see a message similar to the following:

[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:09 min [INFO] Finished at: 2022-06-01T17:21:29+00:00 [INFO] Final Memory: 60M/220M [INFO] ------------------------------------------------------------------------

Explore the Dataflow pipeline

  1. In the Google Cloud console, on the Navigation menu, under Analytics, click Dataflow > Jobs.

  2. Click on the new pipeline job name.

  3. Locate the write:cbt step in the pipeline graph, and to see the details of the writer, click on the down arrow next to write:cbt.

  4. Click on the provided writer, and review the details provided within Step info.

Click Check my progress to verify the objective. Launch a Dataflow pipeline.

Task 4. Verify streaming data loaded into Bigtable

In a previous task, you already configured the .cbtrc configuration file in Cloud Shell. You can now run a simple cbt CLI command to query the first five records of the table.

  • To see the first five rows of data and their values in the lane column family, run the following command:
cbt read current_conditions count=5 \ columns="lane:.*"

The output is structured as follows:

---------------------------------------------- ROW KEY COLUMN_FAMILY:COLUMN_QUALIFIER @ TIMESTAMP VALUE

The output values will resemble the following:

---------------------------------------------- 15#S#1#9223370811310975807 lane:direction @ 1970/01/15-04:25:43.800000 "S" lane:highway @ 1970/01/15-04:25:43.800000 "15" lane:lane @ 1970/01/15-04:25:43.800000 "1.0" lane:latitude @ 1970/01/15-04:25:43.800000 "32.706184" lane:longitude @ 1970/01/15-04:25:43.800000 "-117.120565" lane:sensorId @ 1970/01/15-04:25:43.800000 "32.706184,-117.120565,15,S,1" lane:speed @ 1970/01/15-04:25:43.800000 "71.4" lane:timestamp @ 1970/01/15-04:25:43.800000 "2008-11-01 12:50:00"

Task 5. Stop streaming jobs and delete Bigtable data

In this final task, you stop the streaming data job and delete the Bigtable instance and table using commands.

Stop simulated streaming data

  1. In the first SSH terminal with the streaming data simulator, to stop the simulation, press CONTROL+C.

Stop the Dataflow job

  1. In the Google Cloud console, on the Navigation menu, click Dataflow > Jobs.

  2. Click on the pipeline job name.

  3. Click Stop.

  4. Select Cancel, and then click Stop job.

Delete a Bigtable table and instance

  1. To delete the Bigtable table, in Cloud Shell, run the following command:
cbt deletetable current_conditions
  1. To delete the Bigtable instance, run the following command:
gcloud bigtable instances delete sandiego

If prompted to confirm, type Y.

Click Check my progress to verify the objective. Stop streaming jobs and delete Bigtable data.

Congratulations!

In this lab, you used commands to create a new Bigtable instance and table, streamed data into the table using Dataflow, and confirmed that the data was successfully streaming into Bigtable by running simple cbt CLI commands. You completed the lab by using commands to stop the job and delete the Bigtable table and instance.

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 May 30, 2024

Lab Last Tested August 3, 2023

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.