
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
Google Cloud Datastore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. In this lab, you use Datastore to store application data for an online Quiz application. You also configure the application to retrieve from Datastore and display the data in the quiz.
The Quiz application skeleton has already been written. You clone the repository that contains the skeleton using Google Cloud Shell, review the code using the Cloud Shell editor, and view it using the Cloud Shell web preview feature. You then modify the code that stores data to use Cloud Datastore.
In this lab, you perform the following tasks:
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
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.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
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.
Accept the terms and skip the recovery resource page.
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.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
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:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Example output:
Output:
Example output:
In this section, you access Cloud Shell, clone the git repository that contains the Quiz application, and then run the application.
Navigate to the directory that contains the sample files for this lab:
Create the environment variable GCLOUD_PROJECT
that references the GCP Project ID:
Install the application dependencies:
The installation may take a couple of minutes. It's complete when you see output similar to the following:
Run the application:
The application is running when the last line output is similar to the following:
Click Create Question.
You should see a simple form that contains textboxes for the question and answers with radio buttons to select the correct answer. Quiz authors can add questions in this part of the application.
Click Take Test.
You are taken to a client application. Since you haven't made any questions, the Quite Interesting Quiz is blank.
In the top navigation bar, click GCP.
You should see a Dummy Title and Dummy Answers. The Dummy Title represents a sample question.
Quiz takers will answer questions in this part of the application.
In this section and throughout the lab you'll review the Quiz application code in a code editor. You can use the shell editors that are installed on Cloud Shell, such as nano or vim, or use the Cloud Shell code editor. This lab uses the Cloud Shell code editor.
/training-data-analyst/courses/developingapps/v1.3/java/datastore/start
folder using the file browser panel on the left side of the editor.This is the root folder for the application.
datastore
folder, notice the end
folder. The end
folder contains the same files as the start
folder, but each file in the end folder contains the complete code required to perform this lab.
From the start
folder, navigate to the src/main/java/com/google/training/appdev
folder using the file browser panel on the left side of the editor.
The paths for Java source code files are relative to the appdev
folder.
Select the .../QuizApplication.java
file.
In this file, the class contains the entrypoint for the Spring Boot application.
Select the .../services/gcp/domain/Question.java
file.
In this file, the domain class represents question data submitted in the question form and questions displayed when taking a quiz.
Select the .../web/QuestionsController.java
file.
This file contains the handlers that display the form and collect form data posted by quiz authors in the web application.
In the QuestionsController.java
file, find the handler that responds to HTTP POST requests for the /questions/add
route.
/training-data-analyst/courses/developingapps/v1.3/java/datastore/start/src/main/resources
folder using the file browser panel on the left side of the editor.templates
folder.This folder contains the template for the web application user interface using the Thyme templating engine.
Select the .../templates/new_question.html file.
This file contains the template for the Create Question form. Notice how there is a select list to pick a quiz, textboxes where an author can enter the question and answers, and radio buttons to select the correct answer.
Return to the folder containing Java source code using the file browser panel on the left side of the editor. (Do you remember? It's start/src/main/java/com/google/training/appdev
.)
Select the .../api/QuizEndpoint.java
file.
This file contains the handler that sends JSON data to students taking a test.
Notice that the handlers also make use of the questionService
object.
Select the .../services/gcp/datastore/QuestionService.java
file.
This is the file where you write Datastore code to save and load quiz questions to and from Cloud Datastore. The web application and API use this class.
In this section, you write code to save form data in Cloud Datastore.
// TODO
and // END TODO
comment lines. To maximize your learning, review the code, inline comments, and related API documentation.
From Cloud Shell, click on the Open Terminal icon and stop the application by pressing Ctrl+C.
To create an App Engine application in your project, execute the following command in Cloud Shell:
Open the .../services/gcp/datastore/QuestionService.java
file in the Cloud Shell editor.
Write a star import for the com.google.cloud.datastore.*
package:
Declare a Datastore
client object named datastore
and initialize it:
After the updates, the first part of QuestionService.java
is as follows:
Declare a static final string named ENTITY_KIND
, initialized with the value "Question"
:
Create a KeyFactory for Question entities:
Learn more about entities from the Entities, Properties, and Keys guide.
In the createQuestion(Question question)
method, modify the method's return type to Key
:
Declare a key with an allocated ID for the Question entity using the datastore client and Key Factory:
For more information view the KeyFactory (Google App Engine API for Java) guide.
Declare an entity named questionEntity
, and initialize it using an entity builder:
Use the Datastore client object (datastore
) to save the entity by calling its put(questionEntity)
method:
Modify the return statement to return the key for the entity:
Save the file.
The following is the QuestionService.java
content with all updates to this point:
From Cloud Shell, click on the Open Terminal icon and run the application:
The application is running when you see the last line output similar to the following:
In Cloud Shell, click Web preview > Preview on port 8080 to preview the quiz application.
Click Create Question, complete the form with the following values, and then click Save.
Form Field |
Value |
Author |
|
Quiz |
|
Title |
|
Answer 1 |
|
Answer 2 |
|
Answer 3 |
|
Answer 4 |
|
You are returned to the application home page.
In this section, you write code to retrieve entity data from Cloud Datastore.
From Cloud Shell, click on the Open Editor icon. Move to the getAllQuestions(String quiz)
method in the .../services/gcp/datastore/QuestionService.java
file, and remove the code for the existing Dummy questions:
Still in the getAllQuestions(String quiz)
method, create a query object and initialize it with a query that retrieves Question entities for a specific quiz from Cloud Datastore:
For more information view the Datastore Queries guide.
Call the Datastore client object's datastore.run(query)
method, and assign the result to entity iterator named entities
:
Return the transformed results, using buildQuestions(entities)
method to convert Datastore entities to domain objects:
Uncomment the buildQuestions(...)
and entityToQuestion(...)
helper methods provided in the QuestionService
class and use them to map the iterator to a list of question domain objects.
The following is the updated QuestionService.java
:
Save the .../services/gcp/datastore/QuestionService.java
file, and then return to the Cloud Shell command.
From Cloud Shell, click on the Open Terminal Stop the application by pressing Ctrl+C.
Start the application.
In Cloud Shell, click Web preview > Preview on port 8080 to preview the quiz application.
Replace the query string at the end of the application's URL with /api/quizzes/gcp
.
https://8080-####-####.cloudshell.dev/api/quizzes/gcp
You should see that JSON data has been returned to the client corresponding to the question you added in the web application!
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:
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.
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