What you'll need

How will you use this tutorial?

How would you rate your experience with using Google Cloud Platform services?

NoviceIntermediateProficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

Run the following command in Cloud Shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
- <myaccount>@<mydomain>.com (active)

Note: gcloud is the powerful and unified command-line tool for Google Cloud Platform. Full documentation is available from https://cloud.google.com/sdk/gcloud. It comes pre-installed on Cloud Shell. You will notice its support for tab-completion.

As you can see, this is a simple POJO. The class is annotated with @Entity to indicate that it can be stored in Datastore and provide the kind name (think of a kind as a table in SQL databases, see documentation for more details). The kind name is optional - if it's omitted, the kind name will be generated based on the class name.

Note that we annotated idproperty with @Id. That indicates that we want this field to be used as the identifier part of the Datastore Key. Every Datastore entity needs an identifier. Supported types are String and Long.

We override the toStringmethod to make the string representation of the objects more readable; this will be useful when we print them out.

The interface extends DatastoreRepository<Book, Long>where Bookis the domain class and Long is the Id type. We declare three query methods in our repository for which implementations are generated automatically behind the scenes.

The first one is findByAuthor. As you can guess, the implementation of this method will execute a query that will use a user-provided value in the condition filter for equality to author field.

findByYearGreaterThan method executes a query that filters for year field greater than user provided value.

findByAuthorAndYear executes a query that looks for entities where author and year fields match to user provided values.

Open the main application DemoApplication class and modify it to look like this:

Note how we annotated the class with @ShellComponent. That informs Spring that we want to use this class as a source for CLI commands. The methods annotated with @ShellMethod will be exposed as CLI commands in our application.

Here we use the methods we declared in the BookRepository interface: findByAuthor, findByYearGreaterThan, findByAuthorAndYear. Also we use three built-in methods: save, findAll and deleteAll.

Let's look at the saveBookmethod. We create a Book object using user-provided values for title, author and year. As you can see, we do not provide an id value, so it will be automatically allocated and assigned to the id field on save. The save method accepts an object of type Book and saves it to Cloud Datastore. It returns a Book object with all fields populated, including the id field. In the end we return a string representation of this object.

The rest of the methods work similarly: they accept passed in parameters to the appropriate repository methods and return stringified results.

To build and start the application, execute this command in Cloud Shell (from the root of the project datastore-example/ where the pom.xml is located) :

$ mvn spring-boot:run

After a successful build stage, the spring logo will show up and the shell prompt will appear:

Find books by specific author and year (find-by-author-year <author> <year>)

To see how the entities are stored in Cloud Datastore, go to GCP Console and navigate to Menu -> Datastore (in the Storage section) -> Entities (select "[default]" namespace, and "books" kind, if necessary).

To clean up, remove all books using the aptly named remove-all-books command from the application shell.

shell:> remove-all-books

To exit the application use the quit command, then Ctrl+C.

In this codelab, you've created an interactive CLI application that can store and retrieve objects from Cloud Datastore!