In this lab, you use Dataflow and Apache Beam to migrate data into Spanner.
Objectives
In this lab, you learn how to:
Write ETL pipelines using Apache Beam.
Run Apache Beam piplines using Google Cloud Dataflow.
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.
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.
Click Activate Cloud Shell 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 YOUR_PROJECT_ID
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
(Optional) You can list the active account name with this command:
gcloud auth list
Click Authorize.
Your output should now look like this:
Output:
ACTIVE: *
ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net
To set the active account, run:
$ gcloud config set account `ACCOUNT`
(Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = <project_ID>
Example output:
[core]
project = qwiklabs-gcp-44776a13dea667a6
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Creating an Apache Beam pipeline to import data into Spanner
On the Google Cloud Console title bar, click Activate Cloud Shell (). If prompted, click Continue.
Run the following command to set your project ID:
gcloud config set project {{{project_0.project_id|placeholder_project_id}}}
Run the following commands to download the files that we will need for this lab:
git clone https://github.com/GoogleCloudPlatform/training-data-analyst
cd training-data-analyst/courses/understanding_spanner/dataflow
Run the following script to create a Spanner database.
Run the following command to see the Schema. In this lab, there is just one table and the PetID and OwnerID fields are both intergers not strings.
cat pets-db-schema.sql
Run the following command to see the data you import. Notice the primary and foreign keys use counters. As you learned earlier in the course, this is an anti-pattern when using Spanner.
To solve this, you use a Dataflow pipeline written in Apache Beam to reverse the bits of the intergers prior to importing the data into Spanner.
This solves the problem of the integers while maintaining the relationships.
cat pets.csv
Click the Open Editor button and open the training-data-analyst/courses/understanding_spanner/dataflow/csv-to-spanner.py code file. Notice the pipeline is created in the main function (lines 53 to 68).
The pipeline reads from the CSV file, then reverses the bits on the PetID and OwnerID fields, before writing the data to Spanner.
The reverse_bits function begins at line 21.
Return to the terminal. Let's try to run this pipeline. First, you must install the Python prerequisites with the following commands.
Run the pipeline. (This code runs the pipeline locally in Cloud Shell. There is no need to navigate to Dataflow.)
python csv-to-spanner.py
When the pipeline completes, run the followng query to see the results:
gcloud spanner databases execute-sql pets-db --instance=test-spanner-instance --sql='SELECT * FROM Pets'
Run the following command to remove the data you just added:
gcloud spanner databases execute-sql pets-db --instance=test-spanner-instance --sql='DELETE FROM Pets WHERE True'
Next, you run the code using the Dataflow service.
Task 2. Running a Dataflow job
To run the job using Dataflow, you need a Cloud Storage bucket for inputs, staging, and outputs. Use the command below to create a bucket that contains your Project ID (this should guarantee a unique name for the bucket). Also, copy the pets.csv file into the bucket. Run each of these individually, not together.
Use the Navigation menu to go to Dataflow Jobs. It may take a few moments to see the job show up, so click the Refresh button until you see it. Then you can click the job and see the job details. It takes several minutes to run the job in the Dataflow service since it creates a cluster or one or more VMs to submit the job to.
As you did before, verify the data was added to your Spanner database. Run the followng query to see the data that was loaded:
gcloud spanner databases execute-sql pets-db --instance=test-spanner-instance --sql='SELECT * FROM Pets'
Delete the Spanner instance so you are no longer being charged for it.
Congratulations! You used Dataflow and Apache Beam to migrate data into Spanner.
End your lab
When you have completed your lab, click End Lab. Your account and the resources you've used are removed from the lab platform.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
1 star = Very dissatisfied
2 stars = Dissatisfied
3 stars = Neutral
4 stars = Satisfied
5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
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.
Moduły tworzą projekt Google Cloud i zasoby na określony czas.
Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.
Użyj przeglądania prywatnego
Skopiuj podaną nazwę użytkownika i hasło do modułu.
Kliknij Otwórz konsolę w trybie prywatnym.
Zaloguj się w konsoli
Zaloguj się z użyciem danych logowania do modułu. Użycie innych danych logowania może spowodować błędy lub naliczanie opłat.
Zaakceptuj warunki i pomiń stronę zasobów przywracania.
Nie klikaj Zakończ moduł, chyba że właśnie został przez Ciebie zakończony lub chcesz go uruchomić ponownie, ponieważ spowoduje to usunięcie wyników i projektu.
Ta treść jest obecnie niedostępna
Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem
Świetnie
Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem
Jeden moduł, a potem drugi
Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten
Aby uruchomić moduł, użyj przeglądania prywatnego
Uruchom ten moduł w oknie incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.
In this lab, you use Dataflow and Apache Beam to migrate data into Spanner.
Czas trwania:
Konfiguracja: 0 min
·
Dostęp na 60 min
·
Ukończono w 60 min