|
| 1 | +custom_content: | |
| 2 | + See the [Google Cloud Datastore docs][cloud-datastore-activation] for more details on how to activate |
| 3 | + Cloud Datastore for your project. |
| 4 | +
|
| 5 | + See the [Datastore client library docs][datastore-client-lib-docs] to learn how to interact |
| 6 | + with the Cloud Datastore using this Client Library. |
| 7 | +
|
| 8 | + #### Creating an authorized service object |
| 9 | + 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](https://developers.google.com/identity/protocols/application-default-credentials). These credentials are automatically inferred from your environment, so you only need the following code to create your service object: |
| 10 | +
|
| 11 | + ```java |
| 12 | + import com.google.cloud.datastore.Datastore; |
| 13 | + import com.google.cloud.datastore.DatastoreOptions; |
| 14 | +
|
| 15 | + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); |
| 16 | + ``` |
| 17 | +
|
| 18 | + For other authentication options, see the [Authentication](https://github.com/googleapis/google-cloud-java#authentication) page. |
| 19 | +
|
| 20 | + #### Storing data |
| 21 | + 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: |
| 22 | +
|
| 23 | + ```java |
| 24 | + import com.google.cloud.datastore.Entity; |
| 25 | + import com.google.cloud.datastore.Key; |
| 26 | + import com.google.cloud.datastore.KeyFactory; |
| 27 | + ``` |
| 28 | +
|
| 29 | + Then add the following code to put an entity in Datastore. |
| 30 | +
|
| 31 | + ```java |
| 32 | + KeyFactory keyFactory = datastore.newKeyFactory().setKind("Person"); |
| 33 | + Key key = keyFactory.newKey("[email protected]"); |
| 34 | + Entity entity = Entity.newBuilder(key) |
| 35 | + .set("name", "John Doe") |
| 36 | + .set("age", 51) |
| 37 | + .set("favorite_food", "pizza") |
| 38 | + .build(); |
| 39 | + datastore.put(entity); |
| 40 | + ``` |
| 41 | +
|
| 42 | + Later, if you want to get this entity back, add the following to your code: |
| 43 | +
|
| 44 | + ```java |
| 45 | + Entity johnEntity = datastore.get(key); |
| 46 | + ``` |
| 47 | +
|
| 48 | + #### Running a query |
| 49 | + In addition to retrieving entities by their keys, you can perform queries to retrieve entities by |
| 50 | + the values of their properties. A typical query includes an entity kind, filters to select entities |
| 51 | + with matching values, and sort orders to sequence the results. `google-cloud-datastore` supports two |
| 52 | + types of queries: `StructuredQuery` (that allows you to construct query elements) and `GqlQuery` |
| 53 | + (which operates using [GQL syntax](https://cloud.google.com/datastore/docs/apis/gql/gql_reference)) |
| 54 | + in string format. In this tutorial, we will use a simple `StructuredQuery`. |
| 55 | +
|
| 56 | + 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: |
| 57 | +
|
| 58 | + ```java |
| 59 | + import com.google.cloud.datastore.Query; |
| 60 | + import com.google.cloud.datastore.QueryResults; |
| 61 | + import com.google.cloud.datastore.StructuredQuery; |
| 62 | + import com.google.cloud.datastore.StructuredQuery.PropertyFilter; |
| 63 | + ``` |
| 64 | +
|
| 65 | + Then add the following code to your program: |
| 66 | +
|
| 67 | + ```java |
| 68 | + Query<Entity> query = Query.newEntityQueryBuilder() |
| 69 | + .setKind("Person") |
| 70 | + .setFilter(PropertyFilter.eq("favorite_food", "pizza")) |
| 71 | + .build(); |
| 72 | + QueryResults<Entity> results = datastore.run(query); |
| 73 | + while (results.hasNext()) { |
| 74 | + Entity currentEntity = results.next(); |
| 75 | + System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!"); |
| 76 | + } |
| 77 | + ``` |
| 78 | +
|
| 79 | + 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](https://cloud.google.com/datastore/docs/tools/indexconfig). |
| 80 | +
|
| 81 | + #### Updating data |
| 82 | + Another thing you'll probably want to do is update your data. The following snippet shows how to update a Datastore entity if it exists. |
| 83 | +
|
| 84 | + ``` java |
| 85 | + KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind"); |
| 86 | + Key key = keyFactory.newKey("keyName"); |
| 87 | + Entity entity = datastore.get(key); |
| 88 | + if (entity != null) { |
| 89 | + System.out.println("Updating access_time for " + entity.getString("name")); |
| 90 | + entity = Entity.newBuilder(entity) |
| 91 | + .set("access_time", DateTime.now()) |
| 92 | + .build(); |
| 93 | + datastore.update(entity); |
| 94 | + } |
| 95 | + ``` |
| 96 | +
|
| 97 | + The complete source code can be found at |
| 98 | + [UpdateEntity.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/UpdateEntity.java). |
| 99 | +
|
| 100 | + #### Complete source code |
| 101 | +
|
| 102 | + In |
| 103 | + [AddEntitiesAndRunQuery.java](../../google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/AddEntitiesAndRunQuery.java) |
| 104 | + we put together all the code to store data and run queries into one program. The program assumes that you are |
| 105 | + running on Compute Engine or from your own desktop. To run the example on App Engine, simply move |
| 106 | + the code from the main method to your application's servlet class and change the print statements to |
| 107 | + display on your webpage. |
| 108 | +
|
| 109 | + Testing |
| 110 | + ------- |
| 111 | +
|
| 112 | + This library has tools to help write tests for code that uses the Datastore. |
| 113 | +
|
| 114 | + See [TESTING.md](https://github.com/googleapis/google-cloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore) to read more about testing. |
| 115 | +
|
| 116 | + Example Applications |
| 117 | + -------------------- |
| 118 | + - [`Bookshelf`](https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/bookshelf) - An App Engine app that manages a virtual bookshelf. |
| 119 | + - This app uses `google-cloud` to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service. |
| 120 | + - [`Flexible Environment/Datastore example`](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/flexible/datastore) - A simple app that uses Cloud Datastore to list the last 10 IP addresses that visited your site. |
| 121 | + - [`SparkDemo`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/flexible/sparkjava) - An example of using `google-cloud-datastore` from within the SparkJava and App Engine Flexible Environment frameworks. |
| 122 | + - Read about how it works on the example's [README page](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/flexible/sparkjava#how-does-it-work). |
0 commit comments