Cloud Spanner - Database Fundamentals - GSP1048

A passionate full-stack developer from @ePlus.DEV
Search for a command to run...

A passionate full-stack developer from @ePlus.DEV
No comments yet. Be the first to comment.
Challenge overview In a challenge lab you’re given a scenario and a set of tasks. Instead of following step-by-step instructions, you will use the skills learned from the labs in the course to figure

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI công bố mười kết quả mới cho các bài toán lâu năm trong toán h

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI công bố mười kết quả mới cho các bài toán lâu năm trong toán h

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary GitHub thử nghiệm policy theo enterprise team, cho phép cấp thêm mode

Một bản tin giúp Developer cập nhật nhanh AI, Cloud, Open Source và những công nghệ đáng chú ý trong ngày. 📌 Executive Summary OpenAI giảm mạnh giá GPT-5.6 Luna và Terra, đồng thời chuyển Priority

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 perform basic administrative tasks within a Cloud Spanner instance.
In this lab, you learn how to perform the following tasks:
Create an instance.
Create a database.
Create tables.
Insert and modify data.
Use the Google Cloud CLI with Cloud Spanner.
Use Automation Tools with Cloud Spanner.
Delete an instance.
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:
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.
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:
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.
student-04-182b08b200a0@qwiklabs.net
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
ivpPLSRYHmka
You can also find the Password in the Lab Details pane.
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.
Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.
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:
Continue through the Cloud Shell information window.
Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
When you are connected, you are already authenticated, and the project is set to your Project_ID, qwiklabs-gcp-02-069e6513c1d6. The output contains a line that declares the Project_ID for this session:
Your Cloud Platform project in this session is set to qwiklabs-gcp-02-069e6513c1d6
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
gcloud auth list
Output:
ACTIVE: *
ACCOUNT: student-04-182b08b200a0@qwiklabs.net
To set the active account, run:
$ gcloud config set account `ACCOUNT`
gcloud config list project
Output:
[core]
project = qwiklabs-gcp-02-069e6513c1d6
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
The first step in using Cloud Spanner is to create an instance. An instance is an allocation of Google Cloud compute and storage resources. From the Console, open the navigation menu () > View All Products. Under Databases section, click Spanner.
Accept any acknowledgement or information window that may appear.
Then click Create a Provisioned Instance.
Fill in the following fields, leave the remainder with the default values:
| Item | Value |
| Select an edition | Enterprise |
| Instance Name | banking-instance |
| Select a configuration | us-east1 |
| Configure compute capacity | Unit - Nodes // Quantity - 1 |
From the instance details page, click Create database.
For the database name, enter banking-db.
Skip the Define your schema (optional) step for now. You'll define your schema in the next section.
Click Create.
You're now on the Overview page for the new database you created. You can see that the page is similar to the Instance one, but the statistics refer to the specific database. Also note the new options on the left menu.
Click Check my progress to verify the objective.
Create an instance and database
Check my progress
On the Database Details page for your banking-db database, scroll down the page and click Create table.
Click the blue + icon to open the Query page, enter:
CREATE TABLE Customer (
CustomerId STRING(36) NOT NULL,
Name STRING(MAX) NOT NULL,
Location STRING(MAX) NOT NULL,
) PRIMARY KEY (CustomerId);
Click Run.
When the operation is complete, click Overview under Database in the left menu. Then scroll down to Tables and click Customer to see the schema details:
Click Check my progress to verify the objective.
Create a schema for your database
Check my progress
The Cloud Console provides an interface for inserting, editing, and deleting data.
While on the Schema page, click Data in the left menu. Then click Insert.
This takes you to the Query tab of the Spanner Studio automatically. Click Clear Query, paste the query below, and click Run:
INSERT INTO
Customer (CustomerId,
Name,
Location)
VALUES
('bdaaaa97-1b4b-4e58-b4ad-84030de92235',
'Richard Nelson',
'Ada Ohio'
);
The lower page of the screen shows the result. The Customer table now has one row.
Add a second row. Replace the previous statement with the following, and click Run:
INSERT INTO
Customer (CustomerId,
Name,
Location)
VALUES
('b2b4002d-7813-4551-b83b-366ef95f9273',
'Shana Underwood',
'Ely Iowa'
);
You can execute a SQL statement on the query page of your database.
In the left pane of the Cloud Platform Console, click Spanner Studio to navigate to the Query UI window.
Click the blue + icon to open the Query page. Click Clear Query, paste the query below, and click Run:
SELECT * FROM Customer;
Click Run.
The Cloud Console displays the result of your query.
The Cloud Console is very useful, but in some use cases you want to manage Spanner instances using other methods. Google Cloud services can also be managed through the command line tool named gcloud. The easiest way to use the gcloud CLI is via the Cloud Shell but it can also be installed on a wide variety of operating systems.
gcloud spanner instances create [INSTANCE-ID] \
--config=[INSTANCE-CONFIG] \
--description="[INSTANCE-NAME]" \
--nodes=[NODE-COUNT]
gcloud spanner instances create banking-instance-2 \
--config=regional-us-east1 \
--description="Banking Instance 2" \
--nodes=2
gcloud spanner instances list
You can also create databases in a Spanner instance using gcloud.
In the Cloud Shell, create a new database using the command below.
gcloud spanner databases create banking-db-2 --instance=banking-instance-2
Create an instance and database with CLI
Check my progress
Remember that it is important to provision enough nodes to keep CPU utilization and storage utilization below the recommended maximum values. However, sometimes it is necessary to reduce the number of nodes.
You are now going to reduce the number of nodes of the instance banking-instance-2 from two to one.
Use the following gcloud command to adjust the instance:
gcloud spanner instances update banking-instance-2 --nodes=1
gcloud spanner instances list
As your Spanner architectures grow and get more complex, you want to automate the deployment and management of Spanner instances. One of the tools available to deploy and manage Spanner is Terraform.
The goal of this lab is not to explain how Terraform works. If you are not familiar with Terraform, you can check the documentation.
In short, you provide Terraform with a set of files that describe the infrastructure or services that you want it to deploy in Google Cloud, which is called a configuration. Then you instruct Terraform to initialize and plan the deployment, before applying the changes.
terraform -version
Terraform v1.5.6
on linux_amd64
Note: You may safely ignore any warning about the version of Terraform being out of date.
nano spanner.tf
resource "google_spanner_instance" "banking-instance-3" {
name = "banking-instance-3"
config = "regional-us-east1"
display_name = "Banking Instance 3"
num_nodes = 2
labels = {
}
}
terraform init
terraform plan
terraform apply
The plan will be displayed again and Terraform will pause for approval to continue. Type yes and Terraform will create the new instance.
The apply will process and return the following output:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
gcloud spanner instances list
gcloud spanner instances delete banking-instance-2
gcloud spanner instances list
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP1048/lab.sh
source lab.sh
Script Alternative
export REGION=

curl -LO raw.githubusercontent.com/QUICK-GCP-LAB/2-Minutes-Labs-Solutions/main/Cloud%20Spanner%20-%20Database%20Fundamentals/gsp1048.sh
sudo chmod +x gsp1048.sh
./gsp1048.sh