访问 700 多个实验和课程

App Dev - Processing Cloud Pub/Sub Data using Cloud Functions: Node.js

实验 2 个小时 universal_currency_alt 5 个积分 show_chart 入门级
info 此实验可能会提供 AI 工具来支持您学习。
访问 700 多个实验和课程

Overview

In this lab, you enhance the online Quiz application by creating a Cloud Function to process Cloud Pub/Sub messages.

This process harnesses several GCP products in a serverless environment: Cloud Pub/Sub, Cloud Natural Language API, and Cloud Spanner.

Objectives

In this lab, you learn how to perform the following tasks:

  • Create a Cloud Function that responds to Cloud Pub/Sub messages.
  • Deploy multiple files to a Cloud Function.

Setup and requirements

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Qwiklabs using an incognito window.

  2. Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
    There is no pause feature. You can restart if needed, but you have to start at the beginning.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. Click Use another account and copy/paste credentials for this lab into the prompts.
    If you use other credentials, you'll receive errors or incur charges.

  7. Accept the terms and skip the recovery resource page.

Activate Google Cloud Shell

Google 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.

Google Cloud Shell provides command-line access to your Google Cloud resources.

  1. In Cloud console, on the top right toolbar, click the Open Cloud Shell button.

    Highlighted Cloud Shell icon

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Project ID highlighted in the Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  • You can list the active account name with this command:
gcloud auth list

Output:

Credentialed accounts: - @.com (active)

Example output:

Credentialed accounts: - google1623327_student@qwiklabs.net
  • You can list the project ID with this command:
gcloud config list project

Output:

[core] project =

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: Full documentation of gcloud is available in the gcloud CLI overview guide .

Task 1. Preparing the case study application

In this section, you access Cloud Shell, clone the git repository containing the Quiz application, configure environment variables, and run the application.

Clone source code in Cloud Shell

  1. To clone the repository for the class, enter the following command:
git clone --depth=1 https://github.com/GoogleCloudPlatform/training-data-analyst
  1. Create a soft link as a shortcut to your working directory:
ln -s ~/training-data-analyst/courses/developingapps/v1.3/nodejs/cloudfunctions ~/cloudfunctions

Configure and run the case study application

  1. To change the working directory, enter the following command:
cd ~/cloudfunctions/start
  1. To replace the default region in a file with the lab-assigned region, you can use the following commands:
export APP_REGION={{{project_0.startup_script.app_gcp_region | APP_REGION}}} export REGION={{{project_0.default_region | REGION}}} sed -i 's/us-central1/'"$REGION"'/g' prepare_environment.sh sed -i 's/us-central/'"$APP_REGION"'/g' prepare_environment.sh
  1. To configure the Quiz application, enter the following command:
. prepare_environment.sh Note: This script file:
  • Creates an App Engine application.
  • Exports the environment variables GCLOUD_PROJECT and GCLOUD_BUCKET.
  • Runs npm install.
  • Creates entities in Cloud Datastore.
  • Creates a Cloud Pub/Sub topic.
  • Creates a Cloud Spanner Instance, Database, and Table.
  • Prints out the Google Cloud Project ID.
  1. To run the web application, enter the following command:
npm start

Click Check my progress to verify the objective. Configure and run the case study application

Task 2. Working with Cloud Functions

In this section, you create a Cloud Function in your Google Cloud project that is triggered by publishing a message to Cloud Pub/Sub, runs the application, and monitors the Cloud Function invocation.

Create a Cloud Function

  1. To access Cloud Run functions, on the Navigation menu (Navigation menu icon), click Cloud Run.

  2. Click Write a Function.

  3. For Service name, type process-feedback.

  4. For Region, select the region.

  5. Click + Add Triggers and select Pub/Sub trigger from the dropdown.

  6. If necessary, click Enable to enable the required APIs.

In the Pub/Sub trigger window, specify the following:

Property Value
Trigger type Google sources
Event provider Cloud Pub/Sub
Event type google.cloud.pubsub.topic.v1.messagePublished
Cloud Pub/Sub topic projects/PROJECT_ID/topics/feedback
Region

If you're notified that Pub/Sub needs the role roles/iam.serviceAccountTokenCreator granted to the Pub/Sub service account, click Grant.

  1. Click Save Trigger.

  2. Click Create.

  3. Review the provided function implementation.

Note: It might take a minute or two to create the Cloud Function.

Click Check my progress to verify the objective. Create a Cloud Function

Run the web application

  1. Return to the Cloud Shell window.
  2. Preview the web application by clicking on the Web Preview button and selecting Preview on port 8080.
  3. Click Take Test.
  4. Click Places.
  5. Complete the question.
  6. Rate the quiz, enter some feedback, and then click Send Feedback.

View Cloud Function monitoring and logs

  1. Return to the Cloud Platform Console, on the Cloud Run page.
  2. Click process-feedback.
Note: You should see a monitoring graph for Cloud Function invocations. It takes a few minutes for the graph to show the invocation of your function.
  1. Click the Logs tab to view your logs.
  2. You can view your logs in the logs explorer by clicking the link on the upper right of the page.
  3. If your Cloud Pub/Sub message isn't displayed, click Refresh.
Note: You should see the log entries collected from the Cloud Function. It takes a few minutes for the logs to show the invocation of your function.
  1. Go to Cloud Shell to also see your feedback.

Task 3. Examining the case study application code

In this section, you use the Cloud Shell text editor to review the case study application code.

Launch the Cloud Shell Editor

  1. In the Cloud Platform Console, click Open Editor.

Open Editor button highlighted

Review the Cloud Function application code structure

  1. Navigate to cloudfunctions/start.
  2. Select the index.js file in the ...function folder.
Note: This file contains the same code as the sample from the Cloud Functions window in the Cloud Console, with one change: because the function you will write returns a Promise, the callback argument has been omitted.
  1. Select the package.json file.
Note: This file contains the list of dependencies that this function needs to run. Cloud Functions automatically install the dependencies.
  1. Select the languageapi.js file.
Note: This file contains code to process feedback text and return the Natural Language ML API sentiment score.
  1. Select the spanner.js file.
Note: This file contains code to insert a record into a Cloud Spanner database.

Task 4. Coding a Cloud Function

In this section you write the code to create a Cloud Function that retrieves the Cloud Pub/Sub message data, invokes the Natural Language ML API to perform sentiment detection, and inserts a record into Cloud Spanner.

Note: Update code within the sections marked as follows:

// TODO

// END TODO

To maximize your learning, review the code, inline comments, and related API documentation.

Write code to modify a Cloud Function

  1. Return to the ...function/index.js file.
  2. Load the languageapi and spanner modules. These modules are in the same folder as the index.js file.
  3. In the subscribe() method, after the existing code that loads the Cloud Pub/Sub message into a buffer, convert the PubSub message into a feedback object by parsing it as JSON data.
  4. Return a promise that invokes the languageapi module's analyze method to analyze the feedback text.
  5. Chain a .then(...) method to the end of the return statement.
  6. Supply an arrow function as the value of the callback.
  7. In the body of the arrow function, log the Natural Language API sentiment score to the console.
  8. Add a new property called score to the feedback object.
  9. Complete the arrow function body by returning the feedback object.
  10. Chain a second .then(...) method to the end of the first one. This one uses the spanner module to save the feedback.
  11. Write a third .then(...) chained method, including an arrow function with no arguments and an empty body as the value of the callback.
  12. In the body of this callback, log a message to indicate that the feedback has been saved, and return a success message.
  13. Attach a .catch(...) handler to the end of the chain, which logs the error message to the console.
Note: Here's how your function should look when you're done.

function/index.js

// TODO: Load the ./languageapi module const languageAPI = require('./languageapi'); // END TODO // TODO: Load the ./spanner module const feedbackStorage = require('./spanner'); // END TODO exports.subscribe = function subscribe(event) { // The Cloud Pub/Sub Message object. // TODO: Decode the Cloud Pub/Sub message // extracting the feedbackObject data // The message received from Pub/Sub is base64 encoded, and // the data submitted by students is in a data property const pubsubMessage = Buffer.from(event.data, 'base64').toString(); let feedbackObject = JSON.parse(pubsubMessage); console.log('Feedback object data before Language API:' + JSON.stringify(feedbackObject)); // END TODO // TODO: Run Natural Language API sentiment analysis // The analyze(...) method expects to be passed the // feedback text from the feedbackObject as an argument, // and returns a Promise. return languageAPI.analyze(feedbackObject.feedback).then(score => { // TODO: Log the sentiment score console.log(`Score: ${score}`); // END TODO // TODO: Add new score property to feedbackObject feedbackObject.score = score; // END TODO // TODO: Pass feedback object to the next handler return feedbackObject; // END TODO }) // TODO: insert record .then(feedbackStorage.saveFeedback).then(() => { // TODO: Log and return success console.log('feedback saved...'); return 'success'; // END TODO }) // END TODO // TODO: Catch and Log error .catch(console.error); // End TODO };
  1. Save the file.

Package and deploy the Cloud Function code

  1. Return to Cloud Shell and stop the web application by pressing Ctrl+C.
  2. To change the working directory to the Cloud Function code, enter the following command:
cd function
  1. To zip up the files needed to deploy the function, enter the following command:
zip cf.zip *.js* Note: This generates a zip archive named cf.zip that includes all the JavaScript and JSON files in the folder.
  1. To stage the zip file into Cloud Storage, enter the following command:
gcloud storage cp cf.zip gs://$GCLOUD_BUCKET/ Note: This copies the zip archive into the Cloud Storage bucket named after your project ID with a -media suffix.
  1. To deploy the function from the Cloud Storage bucket, you can use the gcloud functions deploy command as follows.
gcloud functions deploy process-feedback1 \ --gen2 \ --region={{{project_0.default_region|set at lab start}}} \ --source=gs://$GCLOUD_BUCKET/cf.zip \ --entry-point=subscribe \ --runtime=nodejs22 \ --trigger-topic=feedback Note: It may take a minute or two to update the process-feedback1 function with your new code that performs sentiment analysis with the Cloud Natural Language Machine Learning API and inserts data into Cloud Spanner. Package and deploy the Cloud Function code

Task 5. Testing the case study application

Run the web application

  1. Return to the Cloud Shell window.
  2. Change the working folder back to the start folder for the cloudfunctions lab:
cd ..
  1. To start the web application, execute the following command:
npm start
  1. Preview the web application.
  2. Click Take Test.
  3. Click Places.
  4. Complete the question.
  5. Enter some feedback, and click Send Feedback.

View Cloud Function monitoring and logs

  1. Return to the Cloud Run section of the Cloud Platform Console.
  2. Click the function name, process-feedback1.
Note: You should see a monitoring graph for Cloud Function invocations. It takes a few minutes for the graph to show the invocation of your function.
  1. Click the Logs tab to view your logs.
  2. You can view your logs in the logs explorer by clicking the link on the upper right of the page.
  3. If your Cloud Pub/Sub message isn't displayed, click Refresh.
  4. Refresh the logs every few minutes until the log entries show that your function executed.
Note: You should see the log entries collected from the Cloud Function. You should now see a score from the Language API in the logs. It may takes a few minutes for the logs to show the invocation of your function.

View Cloud Spanner data

  1. On the Navigation menu, click on View All Products and click Spanner.
  2. Click Quiz instance, Under the Databeses click quiz-database and then on the left pane select Spanner Studio.
  3. Run the following query:
SELECT * FROM Feedback Note: You should see that a new record has been added to the Feedback table.

Task 6. Bonus: Storing student answers using a Cloud Function

When a student completes a quiz, their answers are submitted in an API call back to the server. Your job is to capture the student-submitted answers and the correct answers and save them into Cloud Spanner. You should see that a new record has been added to the Feedback table.

To do this:

  1. Create a Cloud Pub/Sub topic called answers.
  2. Create a Cloud Spanner table called Answers with appropriate column names and data types.
  3. Post the answer data to the answers topic.
  4. Subscribe to the answers topic in the console application and insert the answer data into the Answers table.

The details are left up to you! You can find the solution to the bonus in the lab's bonus folder.

Review:

Which triggers can be used with Cloud Functions?
  • Cloud Pub/Sub
  • Cloud Spanner
  • Cloud Storage
  • HTTP

With a Cloud Function triggered by Cloud Pub/Sub, how is the message delivered?
  • Base64 encoded
  • CSV encoded
  • Tarred and Gzipped
  • Zipped

What is the maximum execution time for a Cloud Function?
  • 60 seconds
  • 540 seconds
  • Unlimited

End your lab

When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

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 2022 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.

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

请使用无痕模式或无痕式浏览器窗口运行此实验。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。