
准备工作
- 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
- 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
- 在屏幕左上角,点击开始实验即可开始
Import sample stock trade data to the database
/ 100
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 build and deploy a Node.js application connected to a Cloud Spanner instance. The Node.js application is a stock price visualization tool named OmegaTrade. The OmegaTrade application stores stock prices in Cloud Spanner and renders visualizations using Google Charts.
The backend service uses the Node.js Express framework and connects to Cloud Spanner with default connection pooling, session, and timeout capabilities.
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.
In order to allow you to move more quickly through this lab the Cloud Spanner instance, database, and tables required for the OmegaTrade application were automatically created for you.
Here are some details for your reference:
Item | Name | Details |
---|---|---|
Cloud Spanner Instance | omegatrade-instance | This is the project-level instance |
Cloud Spanner Database | omegatrade-db | This is the instance specific database |
Table | Users | Contains user accounts |
Table | Companies | Contains company name and stock symbol |
Table | CompanyStocks | Contains stock values |
Table | Simulations | Tracks the state of each simulation |
First enable the Google Cloud APIs for Cloud Spanner, Container Registry, and Cloud Run.
The application relies on a deployment-specific file named .env to successfuly communicate with the Cloud Spanner instance. You will create this file in the next task.
With respect to the backend, some of the bindings and interactions with the Cloud Spanner tables are managed by Node.js models - three of these company.model.js, simulation.model.js, and user.model.js - reside in the models folder. Issue the following command to navigate to the models folder:
Press the spacebar to advance through the file until its end. If you wish to close the file early, type q to close the more command.
For instance, the company component contains the base application code to manage and update company information.
Issue the following command to inspect the manage-company.component.ts TypeScript configuration file. This file contains methods for deleting or editing a company, among other actions.
Press the spacebar to advance through the file until its end. If you wish to close the file early, type q to close the more command.
In the cloud shell enter the following command to invoke the Nano text editor and create a new .env file.
Paste the code block listed below.
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
Enter 'y' when prompted if you want to continue.
The backend Service URL will appear in the following format:
Press Ctrl+X to exit Nano, Y to confirm the update, and press Enter to save your changes.
Your updated environment.ts file should appear like the example below.
On the application launch page click the sign up link.
Use the following details to create a new account for a fictitious company named Spanner1.
Item | Value |
---|---|
Business email | admin@spanner1.com |
Full Name | Spanner1 Admin |
Password | Spanner1 |
Confirm your Password | Spanner1 |
Your account will be created and you will be logged in.
To examine the OmegaTrade application, navigate to the Dashboard and choose Foobar Inc from the selector to see the stock performance chart for Foobar Inc. You will see a range of simulated stock prices over time for Foobar Inc.
Navigate to Manage Company and add Spanner1 as a new company.
Click Add Company on the right side of the page. On the pop-up window, input the following values:
Item | Value |
---|---|
Company Name | Spanner1 |
Short Code | SPN |
Then click Save.
Spanner1 is now in the list of companies.
Navigate to the Dashboard and select Spanner1 if it is not already selected. You will see that no simulation exists for Spanner1. Click the link entitled here to generate a simiulation.
Under Simulate Data, provide the following details:
Item | Value |
---|---|
Select Company | Spanner1 |
Select Interval | 5 |
Number of Records | 50 |
Then click Simulate.
Navigate to the Dashboard which will immediately update the chart for Spanner1 as the simulation progresses. It will take between 3 and 6 minutes for the simulation to complete.
The OmegaTrade application also allows you to modify existing company information. On the Manage Company tab click the pencil icon under Action for Acme Corp.
Update the company name to Coyote Inc. Notice that the Short Code cannot be updated in the user interface. Click Update to close and accept the change.
The update to the company name is immediate. Navigate to the Dashboard and you will see that Acme Corp no longer appears and Coyote Inc has taken its place.
Occasionally data changes are required that exceed the capabilites of the application code. As an empowered user you have the ability to update data used in the OmegaTrade application by making direct changes in the Cloud Spanner database.
In the Cloud Console, click Navigation menu () > View All Products > Databases > Spanner.
Accept any acknowledgement or information window that may appear.
Click on the omegatrade-instance name and then omegatrade-db under Databases. From the list of tables on the bottom of the page, click companies.
Click Data on the left side pane to see the table contents.
You will update the name of the Bar Industries entity.
Click on the checkbox for the Bar Industries row. Then click Edit from the choices listed above the table rows.
You are taken to the Spanner Studio. Click on + New tab. Enter the following query to update the value for companyName from Bar Industries to Consolidated Enterprises Inc.
Click Run to process the update.
Return to the application page, refresh your browser, and navigate to Dashboard tab. You will see that Bar Industries no longer appears and Consolidated Enterprises Inc has taken its place.
You have now deployed a Node.js application connected to a Cloud Spanner instance and performed basic operations against the database using the application as well as editing data directly in the database.
...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 12, 2025
Lab Last Tested May 12, 2025
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.
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
一次一个实验
确认结束所有现有实验并开始此实验