
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Insert data through a client library
/ 30
Insert batch data through a client library
/ 30
Load data using Dataflow
/ 40
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.
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:
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:
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.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
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.
Click through the following windows:
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.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
During deployment, a Cloud Spanner instance, database, and table were created for you.
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.
banking-db
overview page. On the left menu, click Spanner Studio and then run the following: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.
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.
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.
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
Run the python code.
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.
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.
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
Run the python code.
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.
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.
From the Console, open the navigation menu () > View All Products. Under Analytics section, 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:
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:
Expand Optional Parameters.
Uncheck Use default machine type.
Under General purpose, choose the following:
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.
Navigate back to banking-db
overview page. On the left menu, click Spanner Studio 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.
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 from the left menu.
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.
You now have a solid understanding of various ways to load data into Cloud Spanner Instance and perform backups.
...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 July 17, 2024
Lab Last Tested July 17, 2024
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.
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