Java idiomatic client for [Google Cloud Datastore] (https://cloud.google.com/datastore/).
- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/datastore/package-summary.html)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-datastore</artifactId>
<version>0.2.6</version>
</dependency>
If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:gcloud-java-datastore:0.2.6'
If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "gcloud-java-datastore" % "0.2.6"
DatastoreExample
is a simple command line interface for the Cloud Datastore. Read more about using the application on the DatastoreExample
docs page.
See the Authentication section in the base directory's README.
Google Cloud Datastore is a fully managed, schemaless database for storing non-relational data. Cloud Datastore automatically scales with your users and supports ACID transactions, high availability of reads and writes, strong consistency for reads and ancestor queries, and eventual consistency for all other queries.
See the Google Cloud Datastore docs for more details on how to activate Cloud Datastore for your project.
See the gcloud-java
API datastore documentation to learn how to interact
with the Cloud Datastore using this Client Library.
For this tutorial, you will need a Google Developers Console project with the Datastore API enabled. Follow these instructions to get your project set up. You will also need to set up the local development environment by installing the Google Cloud SDK and running the following commands in command line: gcloud auth login
and gcloud config set project [YOUR PROJECT ID]
.
You'll need to obtain the gcloud-java-datastore
library. See the Quickstart section to add gcloud-java-datastore
as a dependency in your code.
To make authenticated requests to Google Cloud Datastore, you must create a service object with credentials. You can then make API calls by calling methods on the Datastore service object. The simplest way to authenticate is to use Application Default Credentials. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
Datastore datastore = DatastoreOptions.defaultInstance().service();
For other authentication options, see the Authentication page.
Objects in Datastore are known as entities. Entities are grouped by "kind" and have keys for easy access. In this code snippet, we will create a new entity representing a person and store that data by the person's email. First, add the following imports at the top of your file:
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
Then add the following code to put an entity in Datastore.
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
Key key = keyFactory.newKey("john.doe@gmail.com");
Entity entity = Entity.builder(key)
.set("name", "John Doe")
.set("age", 51)
.set("favorite_food", "pizza")
.build();
datastore.put(entity);
Later, if you want to get this entity back, add the following to your code:
Entity johnEntity = datastore.get(key);
In addition to retrieving entities by their keys, you can perform queries to retrieve entities by the values of their properties. A typical query includes an entity kind, filters to select entities with matching values, and sort orders to sequence the results. gcloud-java-datastore
supports two types of queries: StructuredQuery
(that allows you to construct query elements) and GqlQuery
(which operates using GQL syntax) in string format. In this tutorial, we will use a simple StructuredQuery
.
Suppose that you've added more people to Datastore, and now you want to find all people whose favorite food is pizza. Import the following:
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
Then add the following code to your program:
Query<Entity> query = Query.entityQueryBuilder()
.kind("Person")
.filter(PropertyFilter.eq("favorite_food", "pizza"))
.build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
Entity currentEntity = results.next();
System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!");
}
Cloud Datastore relies on indexing to run queries. Indexing is turned on by default for most types of properties. To read more about indexing, see the Cloud Datastore Index Configuration documentation.
In AddEntitiesAndRunQuery.java we put together all the code shown above into one program. The program assumes that you are running on Compute Engine or from your own desktop. To run the example on App Engine, simply move the code from the main method to your application's servlet class and change the print statements to display on your webpage.
To get help, follow the gcloud-java
links in the gcloud-*
shared Troubleshooting document.
Java 7 or above is required for using this client.
This library has tools to help write tests for code that uses the Datastore.
See TESTING to read more about testing.
This library follows [Semantic Versioning] (http://semver.org/).
It is currently in major version zero (0.y.z
), which means that anything
may change at any time and the public API should not be considered
stable.
Contributions to this library are always welcome and highly encouraged.
See gcloud-java
's CONTRIBUTING documentation and the gcloud-*
shared documentation for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
Apache 2.0 - See LICENSE for more information.