Skip to content

Commit f820917

Browse files
committed
---
yaml --- r: 2523 b: refs/heads/update-datastore c: 2c90245 h: refs/heads/master i: 2521: 78cfcf9 2519: b4a731a
1 parent 2e6dc61 commit f820917

19 files changed

Lines changed: 513 additions & 82 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/gh-pages: 4e0561bb4504bf647db669a14417b2b2c87ba45d
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: 1a0e970f265af871e02274085b9662b3fe29058b
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: f87a394379e3ecb662370da5dc5e2b0ddfa4a91f
8+
refs/heads/update-datastore: 2c90245497df0c9674829bcfd02ec5adb294b295
99
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
1010
refs/tags/v0.0.10: 207ebd2a3472fddee69fe1298eb90429e3306efd
1111
refs/tags/v0.0.11: ffbfba48a6426ff63c08ff2117e58681f251fbf2

branches/update-datastore/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
7575
Authentication
7676
--------------
7777

78-
There are multiple ways to authenticate to use Google Cloud services.
78+
First, ensure that the necessary Google Cloud APIs are enabled for your project. To do this, follow the instructions on the [authentication document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/authentication/readme.md#authentication) shared by all the gcloud language libraries.
79+
80+
Next, choose a method for authenticating API requests from within your project:
7981

8082
1. When using `gcloud-java` libraries from within Compute/App Engine, no additional authentication steps are necessary.
8183
2. When using `gcloud-java` libraries elsewhere, there are two options:
@@ -180,6 +182,11 @@ if (blob == null) {
180182
}
181183
```
182184
185+
Troubleshooting
186+
---------------
187+
188+
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
189+
183190
Java Versions
184191
-------------
185192
@@ -206,7 +213,7 @@ Contributing
206213
207214
Contributions to this library are always welcome and highly encouraged.
208215
209-
See [CONTRIBUTING] for more information on how to get started.
216+
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
210217
211218
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][code-of-conduct] for more information.
212219
@@ -217,7 +224,7 @@ Apache 2.0 - See [LICENSE] for more information.
217224
218225
219226
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
220-
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md
227+
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
221228
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
222229
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md
223230
[cloud-platform]: https://cloud.google.com/

branches/update-datastore/gcloud-java-core/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ If you are using SBT, add this to your dependencies
2929
libraryDependencies += "com.google.gcloud" % "gcloud-java-core" % "0.0.12"
3030
```
3131

32+
Troubleshooting
33+
---------------
34+
35+
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
36+
3237
Java Versions
3338
-------------
3439

@@ -39,7 +44,9 @@ Contributing
3944

4045
Contributions to this library are always welcome and highly encouraged.
4146

42-
See [CONTRIBUTING] for more information on how to get started.
47+
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
48+
49+
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][code-of-conduct] for more information.
4350

4451
Versioning
4552
----------
@@ -57,5 +64,6 @@ Apache 2.0 - See [LICENSE] for more information.
5764

5865

5966
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
67+
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
6068
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
6169
[cloud-platform]: https://cloud.google.com/

branches/update-datastore/gcloud-java-datastore/README.md

Lines changed: 133 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,146 @@ Cloud Datastore for your project.
5656
See the ``gcloud-java`` API [datastore documentation][datastore-api] to learn how to interact
5757
with the Cloud Datastore using this Client Library.
5858

59-
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) and a project ID if running this snippet elsewhere.
59+
Getting Started
60+
---------------
61+
#### Prerequisites
62+
For this tutorial, you will need a [Google Developers Console](https://console.developers.google.com/) project with the Datastore API enabled. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up. You will also need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.
63+
64+
#### Installation and setup
65+
You'll need to obtain the `gcloud-java-datastore` library. See the [Quickstart](#quickstart) section to add `gcloud-java-datastore` as a dependency in your code.
66+
67+
#### Creating an authorized service object
68+
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:
6069

6170
```java
6271
import com.google.gcloud.datastore.Datastore;
6372
import com.google.gcloud.datastore.DatastoreOptions;
64-
import com.google.gcloud.datastore.DateTime;
73+
74+
Datastore datastore = DatastoreOptions.defaultInstance().service();
75+
```
76+
77+
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page.
78+
79+
#### Storing data
80+
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 name. First, add the following imports at the top of your file:
81+
82+
```java
6583
import com.google.gcloud.datastore.Entity;
6684
import com.google.gcloud.datastore.Key;
6785
import com.google.gcloud.datastore.KeyFactory;
86+
```
6887

69-
Datastore datastore = DatastoreOptions.defaultInstance().service();
70-
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
71-
Key key = keyFactory.newKey(keyName);
72-
Entity entity = datastore.get(key);
73-
if (entity == null) {
74-
entity = Entity.builder(key)
75-
.set("name", "John Do")
76-
.set("age", 30)
77-
.set("access_time", DateTime.now())
78-
.build();
79-
datastore.put(entity);
80-
} else {
81-
System.out.println("Updating access_time for " + entity.getString("name"));
82-
entity = Entity.builder(entity)
83-
.set("access_time", DateTime.now())
84-
.build();
85-
datastore.update(entity);
88+
Then add the following code to put an entity in Datastore.
89+
90+
```java
91+
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
92+
Key key = keyFactory.newKey("John Doe");
93+
Entity entity = Entity.builder(key)
94+
.set("age", 51)
95+
.set("favorite_food", "pizza")
96+
.build();
97+
datastore.put(entity);
98+
```
99+
100+
Later, if you want to get this entity back, add the following to your code:
101+
102+
```java
103+
Entity johnEntity = datastore.get(key);
104+
```
105+
106+
#### Running a query
107+
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](https://cloud.google.com/datastore/docs/apis/gql/gql_reference)) in string format. In this tutorial, we will use a simple `StructuredQuery`.
108+
109+
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:
110+
111+
```java
112+
import com.google.gcloud.datastore.Query;
113+
import com.google.gcloud.datastore.QueryResults;
114+
import com.google.gcloud.datastore.StructuredQuery;
115+
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
116+
```
117+
118+
Then add the following code to your program:
119+
120+
```java
121+
Query<Entity> query = Query.entityQueryBuilder()
122+
.kind("Person")
123+
.filter(PropertyFilter.eq("favorite_food", "pizza"))
124+
.build();
125+
QueryResults<Entity> results = datastore.run(query);
126+
while (results.hasNext()) {
127+
Entity currentEntity = results.next();
128+
}
129+
```
130+
131+
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).
132+
133+
#### Complete source code
134+
135+
Here we put together all the code shown above into one program. This program assumes that you are running on Compute Engine or from your own desktop. To run this example on App Engine, simply move the code from the main method to your application's servlet class.
136+
137+
```java
138+
import com.google.gcloud.datastore.Datastore;
139+
import com.google.gcloud.datastore.DatastoreOptions;
140+
import com.google.gcloud.datastore.Entity;
141+
import com.google.gcloud.datastore.Key;
142+
import com.google.gcloud.datastore.KeyFactory;
143+
import com.google.gcloud.datastore.Query;
144+
import com.google.gcloud.datastore.QueryResults;
145+
import com.google.gcloud.datastore.StructuredQuery;
146+
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
147+
148+
public class GcloudJavaDatastoreExample {
149+
150+
public static void main(String[] args) {
151+
// Create datastore service object.
152+
// By default, credentials are inferred from the runtime environment.
153+
Datastore datastore = DatastoreOptions.defaultInstance().service();
154+
155+
// Add an entity to Datastore
156+
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
157+
Key key = keyFactory.newKey("John Doe");
158+
Entity entity = Entity.builder(key)
159+
.set("age", 51)
160+
.set("favorite_food", "pizza")
161+
.build();
162+
datastore.put(entity);
163+
164+
// Get an entity from Datastore
165+
Entity johnEntity = datastore.get(key);
166+
167+
// Add a couple more entities to make the query results more interesting
168+
Key janeKey = keyFactory.newKey("Jane Doe");
169+
Entity janeEntity = Entity.builder(janeKey)
170+
.set("age", 44)
171+
.set("favorite_food", "pizza")
172+
.build();
173+
Key joeKey = keyFactory.newKey("Joe Shmoe");
174+
Entity joeEntity = Entity.builder(joeKey)
175+
.set("age", 27)
176+
.set("favorite_food", "sushi")
177+
.build();
178+
datastore.put(janeEntity, joeEntity);
179+
180+
// Run a query
181+
Query<Entity> query = Query.entityQueryBuilder()
182+
.kind("Person")
183+
.filter(PropertyFilter.eq("favorite_food", "pizza"))
184+
.build();
185+
QueryResults<Entity> results = datastore.run(query);
186+
while (results.hasNext()) {
187+
Entity currentEntity = results.next();
188+
// Do something using the entity. (e.g. send an invite a pizza party)
189+
}
190+
}
86191
}
87192
```
88193

194+
Troubleshooting
195+
---------------
196+
197+
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
198+
89199
Java Versions
90200
-------------
91201

@@ -112,7 +222,9 @@ Contributing
112222

113223
Contributions to this library are always welcome and highly encouraged.
114224

115-
See [CONTRIBUTING] for more information on how to get started.
225+
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
226+
227+
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][code-of-conduct] for more information.
116228

117229
License
118230
-------
@@ -121,6 +233,7 @@ Apache 2.0 - See [LICENSE] for more information.
121233

122234

123235
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
236+
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
124237
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
125238
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore
126239
[cloud-platform]: https://cloud.google.com/

branches/update-datastore/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/FullEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import com.google.api.services.datastore.DatastoreV1;
2020

2121
/**
22-
* A full entity is a {@link BaseEntity} that with a complete set of properties.
22+
* A full entity is a {@link BaseEntity} that holds all the properties associated with a
23+
* Datastore entity (as opposed to {@link ProjectionEntity}).
2324
*/
2425
public class FullEntity<K extends IncompleteKey> extends BaseEntity<K> {
2526

branches/update-datastore/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
* A Google Cloud Datastore query.
3636
* For usage examples see {@link GqlQuery} and {@link StructuredQuery}.
3737
*
38+
* Note that queries require proper indexing. See
39+
* <a href="https://cloud.google.com/datastore/docs/tools/indexconfig">
40+
* Cloud Datastore Index Configuration</a> for help configuring indexes.
41+
*
3842
* @param <V> the type of the values returned by this query.
3943
* @see <a href="https://cloud.google.com/datastore/docs/concepts/queries">Datastore Queries</a>
4044
*/

branches/update-datastore/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
import org.json.JSONTokener;
4343

4444
import java.net.InetAddress;
45-
import java.net.MalformedURLException;
4645
import java.net.URL;
47-
import java.net.UnknownHostException;
4846
import java.util.HashMap;
4947
import java.util.Map;
5048

@@ -95,7 +93,7 @@ private static boolean isLocalHost(String host) {
9593
}
9694
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
9795
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
98-
} catch (UnknownHostException | MalformedURLException e) {
96+
} catch (Exception e) {
9997
// ignore
10098
}
10199
}

branches/update-datastore/gcloud-java-examples/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ To run examples from your command line:
5858
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="delete <bucket_name> test.txt"
5959
```
6060

61+
Troubleshooting
62+
---------------
63+
64+
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
65+
6166
Java Versions
6267
-------------
6368

@@ -77,7 +82,9 @@ Contributing
7782

7883
Contributions to this library are always welcome and highly encouraged.
7984

80-
See [CONTRIBUTING] for more information on how to get started.
85+
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
86+
87+
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][code-of-conduct] for more information.
8188

8289
License
8390
-------
@@ -86,6 +93,7 @@ Apache 2.0 - See [LICENSE] for more information.
8693

8794

8895
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
96+
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
8997
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
9098
[cloud-platform]: https://cloud.google.com/
9199
[developers-console]:https://console.developers.google.com/

0 commit comments

Comments
 (0)