Контрольні точки
Insert data through a client library
/ 30
Insert batch data through a client library
/ 30
Load data using Dataflow
/ 40
Cloud Spanner - Loading Data and Performing Backups
GSP1049
Overview
Cloud Spanner is Google’s fully managed, horizontally scalable relational database service. Customers in financial services, gaming, retail and many other industries trust it to run their most demanding workloads, where consistency and availability at scale are critical.
In this lab, you explore various ways to load data into Cloud Spanner and perform a backup of your database.
What you'll do
- Insert data with DML
- Insert data through a client library
- Insert batch data through a client library
- Load data using Dataflow
- Backup your database
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
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
-
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. -
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.
-
Click Next.
-
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.
-
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. -
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.
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,
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:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Explore the instance
During deployment, a Cloud Spanner instance, database, and table were created for you.
- From the Console, open the navigation menu (), under Databases click Spanner.
The instance name is banking-instance, click on it to explore the databases. The associated database is named banking-db. Click on it to explore and you will see there is already a table named Customer. Click on it and you will be able to check the schema:
- The table is currently empty. On the left menu, click Query and then run the following:
- No results are returned.
Task 2. Insert data with DML
The easiest way to insert data into Spanner is via DML. Using the cloud shell and gcloud you can run any DML statement, including INSERT.
- In Cloud Shell, run the following command:
- Return to the Console, on the left menu click Data and you will see the row you just inserted.
As mentioned before, you can use gcloud to run any DML command. Check the documentation for DML and Spanner.
Of course, loading a database row by row is not very efficient.
Task 3. Insert data through a client library
The optimal way to access Spanner is via a programmatic interface. There are a wide variety of client libraries including C++, C#, Go, Java, Node.js, PHP, Python and Ruby.
- In the Cloud Shell enter the following command to invoke the Nano text editor and create a new empty configuration file named insert.py.
- Paste the code block listed below.
-
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
-
Run the python code.
- Refresh the Cloud Console, or click on a different item on the left menu and then click again on Data and you will see the new row in your database.
Like with gcloud, you can run any DML statement from the client libraries. You can find multiple examples for all the different languages in the documentation.
This is more flexible than loading data using gcloud, but still has limitations when loading a source containing a large number of rows.
- Click Check my progress to verify the objective.
Task 4. Insert batch data through a client library
A more optimal way to load data into Spanner is doing so in batches. All of the client libraries support batch loading. This example uses Python.
- In the Cloud Shell enter the following command to invoke the Nano text editor and create a new empty configuration file named batch_insert.py.
- Paste the code block listed below.
-
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
-
Run the python code.
- Go back to Cloud Console, refresh to see the new data you just inserted.
The batch method is more efficient, since it's run as a single request. Only one client-server round trip is needed, reducing latency.
However this is a very slow and resource consuming method to load data.
- Click Check my progress to verify the objective.
Task 5. Load data using Dataflow
Dataflow is a Google Cloud service for streaming and batch data processing at large scale. Dataflow uses multiple workers to run data processing in parallel. The way in which data is processed is defined using pipelines that transform data from its origin (sources) to its destination (sinks).
There are connectors for Spanner that allow you to connect a database as a source or a sink in Dataflow.
In order to load big amounts of data, you can use the serverless distributed power of Dataflow to read data from a source (for example, a CSV file in Google Cloud Storage) and load it into your Spanner database using a sink connector.
- To prepare for the Dataflow job, in the Cloud Shell run these commands to create a bucket in your project and a folder with an empty file inside it.
- To ensure that the proper APIs and permissions are set, execute the following block of code in the Cloud Shell.
-
From the Console, open the navigation menu (), under Analytics click Dataflow.
-
On the top of the screen, click Create Job From Template.
-
Place the following values in the template:
Job Name: spanner-load
Regional endpoint:
- Scroll down the Dataflow template selector and you will see all the different blueprints you can use with Dataflow. Of course, you can also create your own tailored pipelines, using the Beam SDK.
There are two main types of templates:
- Stream will create a pipeline for data that is flowing and is processed continuously (for example, online orders from a website).
- Batch will process a dataset that has a beginning and an end (for example, files stored in Google Cloud Storage).
In your scenario, you will load data into Spanner banking database from a CSV file with over 150,000 rows.
-
Select the Text Files on Cloud Storage to Cloud Spanner template.
-
Place the following values in the template:
Item | Value |
---|---|
Cloud Spanner Instance Id | banking-instance |
Cloud Spanner Database Id | banking-db |
Text Import Manifest file | cloud-training/OCBL372/manifest.json |
The manifest.json
file format is explained in the tutorial for this template (you can access it by clicking open tutorial just above the parameter input fields).
The manifest file must be stored in a Google Cloud Storage bucket that Dataflow can access to and read from. For this lab, this is the content of manifest.json:
The manifest file specifies the table, name and type of the columns (in the order that they appear in the CSV file), and the CSV file itself, which is also stored in a Google Cloud Storage bucket.
This is what the CSV file looks like:
- For the Temporary Location parameter input the following value:
-
Expand Optional Parameters.
-
Uncheck Use default machine type.
-
Under General purpose, choose the following:
Series: E2
Machine type: e2-medium (2 vCPU, 4 GB memory)
-
Click Run Job to start the pipeline.
-
The process will take around 12 to 16 minutes. You will see Dataflow go through multiple stages, first starting up the workers and analyzing the pipeline from the template. Then it will read the manifest file and will start processing the CSV file.
Wait until Dataflow finishes processing before proceeding. It will have a status of Succeeded when complete.
-
Go back to Spanner by selecting it in the left menu on Cloud Console. Navigate to the Customer table and select Data. You will see all the new rows that have been loaded using Dataflow.
-
Click Query on the left menu and run the following to see the total number of rows in the Customer table:
With Dataflow templates it is easy (and quick!) to load big amounts of data. You can load dumps from other databases, and load not only CSV but also Avro files following the same procedure. You can even run the process the other way around, using your Spanner database as a source in Dataflow to export the data in CSV or Avro.
- Click Check my progress to verify the objective.
Task 6. Backup your database
Using Dataflow as explained above is a way to create backups of your data. But Spanner has its own tool for backups. You can backup a Spanner database from the Cloud Console, client libraries or gcloud commands. Check the previous links for documentation.
In this lab, you will use the Cloud Console to backup your database.
-
Select Backup/Restore on the left menu of the Spanner banking-instance overview page.
-
Click Create Backup.
-
Place or select the following values in the wizard:
Item | Value |
---|---|
Database Name | banking-db |
Backup Name | banking-backup-001 |
Expiration Date | 1 year |
-
Click Create.
-
The backup will take around 15 minutes to complete and will appear in the Backups list while being created.
Congratulations!
You now have a solid understanding of various ways to load data into Cloud Spanner Instance and perform backups.
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 25, 2024
Lab Last Tested July 12, 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.