This page provides a short exercise in building a command-line TaskList application with the Firestore in Datastore mode API. The TaskList application stores, lists, updates, and removes tasks.
Prerequisites
- Ability to write and run a command line application in the programming languages used in this topic
In addition to a basic understanding of how to develop applications, you should be able to download and install additional libraries before attempting this tutorial. - A Google Cloud project with the Datastore mode API enabled
Applications that use Datastore mode are associated with a Google Cloud project with the Datastore mode API enabled. This project provides authentication credentials you use in your application to identify it to Google and authorize its use of the Datastore mode API.
Follow these instructions to create a project, enable the Datastore mode API for it, and set up your local development environment with authentication credentials using thegcloud auth login
command. Make a note of the project's ID, which you'll use later on.
Installation and setup
Install client libraries and configure any additional settings for your development environment.
C#
- Ensure you have Visual Studio (version 2013 or later) installed.
- Download the TaskList sample application from the samples repository.
- Extract the files from the zip into a directory in your Documents folder.
- In Visual Studio, open the file
dotnet-docs-samples-master\datastore\api\Datastore.sln
. - In Visual Studio's Solution Explorer window, right-click the TaskList project and choose Set as StartUp Project.
- Right-click the TaskList project again and choose Properties.
In the Properties window, click Debug and type the ID of your Google Cloud project into the Command line arguments: box.
Click File and then click Save to save your changes.
Run the application! Press F5 on your keyboard.
Go
Clone the TaskList sample application.
go get github.com/GoogleCloudPlatform/golang-samples/datastore/tasks
Change directories to where you cloned the sample:
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/datastore/tasks
At a command prompt, run the following, where
<project-id>
is the ID of your Google Cloud project.export DATASTORE_PROJECT_ID=<project-id>
(Windows users: use
set
instead ofexport
.)Run the application!
go run tasks.go
Java
Ensure you have Maven and Java (version 8 or later) installed.
Download the TaskList sample application from the samples repository.
At a command prompt, extract the download:
unzip main.zip
Change directories to the TaskList application:
cd java-datastore-main/samples/snippets
Run the following, where
<project-id>
is the ID of your Google Cloud project.gcloud config set project <project-id>
Compile and run the application!
mvn clean compile mvn exec:java -Dexec.mainClass="com.google.datastore.snippets.TaskList"
Node.js
Download the TaskList sample application from the samples repository.
Extract the download:
unzip master.zip
Change directories to the TaskList application:
cd nodejs-datastore-master/samples
Install the dependencies and link the application:
npm install
At a command prompt, run the following, where
<project-id>
is the ID of your Google Cloud project.export GCLOUD_PROJECT=<project-id>
(Windows users: use
set
instead ofexport
.)Run the application!
node tasks.js
PHP
- Ensure you have PHP (version 5.6 or later) and Composer installed.
- Download the TaskList sample application from the samples repository.
Extract the download:
unzip master.zip
Change directories to the TaskList application:
cd php-docs-samples-master/datastore/tutorial
Install dependencies.
composer install
Run the application!
php src/list_tasks.php
Python
- Ensure you have Python (version 2.7.9 or later), pip, and virtualenv installed.
Activate a
virtualenv
session.virtualenv venv source venv/bin/activate
Download the TaskList sample application from the samples repository.
Extract the download:
unzip master.zip
Change directories to the TaskList application:
cd python-docs-samples-master/datastore/cloud-client
Install dependencies:
pip install -r requirements.txt
Run the application! Use the ID of your Google Cloud project for
<project-id>
.python tasks.py new project-id
Ruby
Download the TaskList sample application from the samples repository.
Extract the download:
unzip master.zip
Change directories to the TaskList application:
cd google-cloud-ruby-master/google-cloud-datastore/samples
Install the dependencies:
bundle install
At a command prompt, run the following, where
<project-id>
is the ID of your Google Cloud project.export GOOGLE_CLOUD_PROJECT=<project-id>
(Windows users: use
set
instead ofexport
.)Run the application!
bundle exec ruby tasks.rb
Creating an Authorized Service Object
In order to make authenticated requests to Google Cloud APIs using the Google APIs Client libraries, you must:
- Fetch the credential to use for requests.
- Create a service object that uses that credential.
You can then make API calls by calling methods on the Datastore mode service object.
For this example, you'll fetch Application Default Credentials from the environment, and pass it as an argument to create the service object.
Here's the call to create the authorized Datastore mode service object:
C#
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Ruby API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Storing data
Objects in Datastore mode are known as entities, and each entity is of a
particular kind. The TaskList application will store entities of kind
Task
, with the following properties:
description
: a string specified by the user as the task descriptioncreated
: a date that shows when the task was initially createddone
: a boolean that indicates whether the task has been completed
When the user adds a new task, the TaskList application creates a Task
entity with values for the properties previously listed:
C#
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Ruby API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
For this application, we also provide a method to update the done
property, to indicate the task is complete:
C#
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Ruby API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Here's the method to delete a Task
entity, using the Task
entity's key:
C#
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Ruby API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Running a query
In addition to retrieving entities from Datastore mode directly by their keys, an application can perform a query to retrieve them by the values of their properties. A typical query includes the following:
- An entity kind to which the query applies
- Zero or more filters, for example to select kinds whose properties match a value
- Zero or more sort orders, to sequence the results
For this application, we'll query Datastore mode for Task
entities sorted by
creation time:
C#
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore C# API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Go API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Java API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Node.js API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore PHP API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Python API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
To learn how to install and use the client library for Cloud Datastore, see Cloud Datastore client libraries. For more information, see the Cloud Datastore Ruby API reference documentation.
To authenticate to Cloud Datastore, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Next Steps
This tutorial covers only the most basic steps necessary to make calls to the Datastore mode API from a command-line application. Datastore mode supports fast and highly scalable ACID transactions, SQL-like queries, indexes and more.
- For a deeper look into the Datastore mode capabilities, see What is Firestore in Datastore mode?.
- For information about using the Datastore mode emulator while you develop your application, see Datastore mode Emulator.