Skip to content

Commit edc9db7

Browse files
committed
Refactor examples
- Create a package named after each service in gcloud-java-examples - Create a package snippet in each service's package to hold README's snippets - Split Datastore Storage and ResourceManager examples in two - Create a class for each snippet in the corresponding service package - Add reference for the READMEs to the corresponding snippet class - Update package-info.java accordingly
1 parent 2bb9cb0 commit edc9db7

24 files changed

Lines changed: 839 additions & 142 deletions

File tree

README.md

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ Google Cloud BigQuery (Alpha)
123123
124124
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you
125125
must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
126+
Complete source code can be found at
127+
[gcloud-java-examples:com.google.gcloud.examples.bigquery.snippets.CreateTableAndLoadData](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java).
126128
127129
```java
128130
import com.google.gcloud.bigquery.BigQuery;
129131
import com.google.gcloud.bigquery.BigQueryOptions;
130132
import com.google.gcloud.bigquery.Field;
133+
import com.google.gcloud.bigquery.FormatOptions;
131134
import com.google.gcloud.bigquery.Job;
132-
import com.google.gcloud.bigquery.JobStatus;
133-
import com.google.gcloud.bigquery.JobInfo;
134-
import com.google.gcloud.bigquery.LoadJobConfiguration;
135135
import com.google.gcloud.bigquery.Schema;
136136
import com.google.gcloud.bigquery.StandardTableDefinition;
137137
import com.google.gcloud.bigquery.Table;
@@ -145,19 +145,17 @@ if (table == null) {
145145
System.out.println("Creating table " + tableId);
146146
Field integerField = Field.of("fieldName", Field.Type.integer());
147147
Schema schema = Schema.of(integerField);
148-
bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
148+
table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
149+
}
150+
System.out.println("Loading data into table " + tableId);
151+
Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
152+
while (!loadJob.isDone()) {
153+
Thread.sleep(1000L);
154+
}
155+
if (loadJob.status().error() != null) {
156+
System.out.println("Job completed with errors");
149157
} else {
150-
System.out.println("Loading data into table " + tableId);
151-
LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
152-
Job loadJob = bigquery.create(JobInfo.of(configuration));
153-
while (!loadJob.isDone()) {
154-
Thread.sleep(1000L);
155-
}
156-
if (loadJob.status().error() != null) {
157-
System.out.println("Job completed with errors");
158-
} else {
159-
System.out.println("Job succeeded");
160-
}
158+
System.out.println("Job succeeded");
161159
}
162160
```
163161
@@ -171,7 +169,11 @@ Google Cloud Datastore
171169
172170
#### Preview
173171
174-
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
172+
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
173+
174+
The first snippet shows how to get a Datastore entity and create it if it does not exist. Complete
175+
source code can be found at
176+
[gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.GetOrCreateEntity](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/GetOrCreateEntity.java).
175177
176178
```java
177179
import com.google.gcloud.datastore.Datastore;
@@ -182,8 +184,8 @@ import com.google.gcloud.datastore.Key;
182184
import com.google.gcloud.datastore.KeyFactory;
183185
184186
Datastore datastore = DatastoreOptions.defaultInstance().service();
185-
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
186-
Key key = keyFactory.newKey(keyName);
187+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
188+
Key key = keyFactory.newKey("keyName");
187189
Entity entity = datastore.get(key);
188190
if (entity == null) {
189191
entity = Entity.builder(key)
@@ -192,7 +194,24 @@ if (entity == null) {
192194
.set("access_time", DateTime.now())
193195
.build();
194196
datastore.put(entity);
195-
} else {
197+
}
198+
```
199+
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
200+
found at
201+
[gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.UpdateEntity](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java).
202+
```java
203+
import com.google.gcloud.datastore.Datastore;
204+
import com.google.gcloud.datastore.DatastoreOptions;
205+
import com.google.gcloud.datastore.DateTime;
206+
import com.google.gcloud.datastore.Entity;
207+
import com.google.gcloud.datastore.Key;
208+
import com.google.gcloud.datastore.KeyFactory;
209+
210+
Datastore datastore = DatastoreOptions.defaultInstance().service();
211+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
212+
Key key = keyFactory.newKey("keyName");
213+
Entity entity = datastore.get(key);
214+
if (entity != null) {
196215
System.out.println("Updating access_time for " + entity.getString("name"));
197216
entity = Entity.builder(entity)
198217
.set("access_time", DateTime.now())
@@ -210,7 +229,8 @@ Google Cloud Resource Manager (Alpha)
210229
#### Preview
211230
212231
Here is a code snippet showing a simple usage example. Note that you must supply Google SDK credentials for this service, not other forms of authentication listed in the [Authentication section](#authentication).
213-
232+
Complete source code can be found at
233+
[gcloud-java-examples:com.google.gcloud.examples.resourcemanager.snippets.UpdateAndListProjects](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/UpdateAndListProjects.java).
214234
```java
215235
import com.google.gcloud.resourcemanager.Project;
216236
import com.google.gcloud.resourcemanager.ResourceManager;
@@ -244,13 +264,36 @@ Google Cloud Storage
244264
245265
#### Preview
246266
247-
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
267+
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
268+
269+
The first snippet shows how to get a Storage blob and create it if it does not exist. Complete
270+
source code can be found at
271+
[gcloud-java-examples:com.google.gcloud.examples.storage.snippets.GetOrCreateBlob](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/GetOrCreateBlob.java).
248272
249273
```java
250274
import static java.nio.charset.StandardCharsets.UTF_8;
251275
252276
import com.google.gcloud.storage.Blob;
277+
import com.google.gcloud.storage.BlobId;
253278
import com.google.gcloud.storage.BlobInfo;
279+
import com.google.gcloud.storage.Storage;
280+
import com.google.gcloud.storage.StorageOptions;
281+
282+
Storage storage = StorageOptions.defaultInstance().service();
283+
BlobId blobId = BlobId.of("bucket", "blob_name");
284+
Blob blob = storage.get(blobId);
285+
if (blob == null) {
286+
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
287+
blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
288+
}
289+
```
290+
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
291+
found at
292+
[gcloud-java-examples:com.google.gcloud.examples.storage.snippets.UpdateBlob](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/UpdateBlob.java).
293+
```java
294+
import static java.nio.charset.StandardCharsets.UTF_8;
295+
296+
import com.google.gcloud.storage.Blob;
254297
import com.google.gcloud.storage.BlobId;
255298
import com.google.gcloud.storage.Storage;
256299
import com.google.gcloud.storage.StorageOptions;
@@ -261,11 +304,7 @@ import java.nio.channels.WritableByteChannel;
261304
Storage storage = StorageOptions.defaultInstance().service();
262305
BlobId blobId = BlobId.of("bucket", "blob_name");
263306
Blob blob = storage.get(blobId);
264-
if (blob == null) {
265-
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
266-
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
267-
} else {
268-
System.out.println("Updating content for " + blobId.name());
307+
if (blob != null) {
269308
byte[] prevContent = blob.content();
270309
System.out.println(new String(prevContent, UTF_8));
271310
WritableByteChannel channel = blob.writer();

gcloud-java-bigquery/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ while (rowIterator.hasNext()) {
203203
Here we put together all the code shown above into one program. This program assumes that you are
204204
running on Compute Engine or from your own desktop. To run this example on App Engine, simply move
205205
the code from the main method to your application's servlet class and change the print statements to
206-
display on your webpage.
206+
display on your webpage. Complete source code can be found at
207+
[gcloud-java-examples:com.google.gcloud.examples.bigquery.snippets.InsertDataAndQueryTable](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java).
208+
207209

208210
```java
209211
import com.google.gcloud.bigquery.BigQuery;
@@ -217,7 +219,6 @@ import com.google.gcloud.bigquery.QueryRequest;
217219
import com.google.gcloud.bigquery.QueryResponse;
218220
import com.google.gcloud.bigquery.Schema;
219221
import com.google.gcloud.bigquery.StandardTableDefinition;
220-
import com.google.gcloud.bigquery.Table;
221222
import com.google.gcloud.bigquery.TableId;
222223
import com.google.gcloud.bigquery.TableInfo;
223224

@@ -226,9 +227,9 @@ import java.util.Iterator;
226227
import java.util.List;
227228
import java.util.Map;
228229

229-
public class GcloudBigQueryExample {
230+
public class InsertDataAndQueryTable {
230231

231-
public static void main(String[] args) throws InterruptedException {
232+
public static void main(String... args) throws InterruptedException {
232233

233234
// Create a service instance
234235
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
@@ -244,7 +245,7 @@ public class GcloudBigQueryExample {
244245
Schema schema = Schema.of(stringField);
245246
// Create a table
246247
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
247-
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
248+
bigquery.create(TableInfo.of(tableId, tableDefinition));
248249

249250
// Define rows to insert
250251
Map<String, Object> firstRow = new HashMap<>();

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
/**
1818
* A client to Google Cloud BigQuery.
1919
*
20-
* <p>A simple usage example:
20+
* <p>A simple usage example showing how to create a table if it does not exist and load data into
21+
* it. For the complete source code see
22+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java">
23+
* gcloud-java-examples:com.google.gcloud.examples.bigquery.snippets.CreateTableAndLoadData</a>.
2124
* <pre> {@code
2225
* BigQuery bigquery = BigQueryOptions.defaultInstance().service();
2326
* TableId tableId = TableId.of("dataset", "table");
@@ -26,19 +29,17 @@
2629
* System.out.println("Creating table " + tableId);
2730
* Field integerField = Field.of("fieldName", Field.Type.integer());
2831
* Schema schema = Schema.of(integerField);
29-
* bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
32+
* table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
33+
* }
34+
* System.out.println("Loading data into table " + tableId);
35+
* Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
36+
* while (!loadJob.isDone()) {
37+
* Thread.sleep(1000L);
38+
* }
39+
* if (loadJob.status().error() != null) {
40+
* System.out.println("Job completed with errors");
3041
* } else {
31-
* System.out.println("Loading data into table " + tableId);
32-
* LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
33-
* Job loadJob = bigquery.create(JobInfo.of(configuration));
34-
* while (!loadJob.isDone()) {
35-
* Thread.sleep(1000L);
36-
* }
37-
* if (loadJob.status().error() != null) {
38-
* System.out.println("Job completed with errors");
39-
* } else {
40-
* System.out.println("Job succeeded");
41-
* }
42+
* System.out.println("Job succeeded");
4243
* }}</pre>
4344
*
4445
* @see <a href="https://cloud.google.com/bigquery/">Google Cloud BigQuery</a>

gcloud-java-datastore/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ Cloud Datastore relies on indexing to run queries. Indexing is turned on by defa
134134

135135
#### Complete source code
136136

137-
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, move this code to your application's servlet class and print the query output to the webpage instead of `System.out`.
137+
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, move this code to your application's servlet class and print the query output to the webpage instead of `System.out`.
138+
Complete source code can be found at
139+
[gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.AddEntitiesAndRunQuery](https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/AddEntitiesAndRunQuery.java).
138140

139141
```java
140142
import com.google.gcloud.datastore.Datastore;
@@ -147,9 +149,9 @@ import com.google.gcloud.datastore.QueryResults;
147149
import com.google.gcloud.datastore.StructuredQuery;
148150
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
149151

150-
public class GcloudDatastoreExample {
152+
public class AddEntitiesAndRunQuery {
151153

152-
public static void main(String[] args) {
154+
public static void main(String... args) {
153155
// Create datastore service object.
154156
// By default, credentials are inferred from the runtime environment.
155157
Datastore datastore = DatastoreOptions.defaultInstance().service();

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/package-info.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,41 @@
1717
/**
1818
* A client to the Google Cloud Datastore.
1919
*
20-
* <p>Here's a simple usage example for using gcloud-java from App/Compute Engine:
20+
* <p>Here's a simple usage example for using gcloud-java from App/Compute Engine. This example
21+
* shows how to get a Datastore entity and create it if it does not exist. For the complete source
22+
* code see
23+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/GetOrCreateEntity.java">
24+
* gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.GetOrCreateEntity</a>.
2125
* <pre> {@code
2226
* Datastore datastore = DatastoreOptions.defaultInstance().service();
23-
* KeyFactory keyFactory = datastore.newKeyFactory().kind(kind);
24-
* Key key = keyFactory.newKey(keyName);
27+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
28+
* Key key = keyFactory.newKey("keyName");
2529
* Entity entity = datastore.get(key);
2630
* if (entity == null) {
2731
* entity = Entity.builder(key)
2832
* .set("name", "John Do")
29-
* .set("age", LongValue.builder(100).indexed(false).build())
30-
* .set("updated", false)
33+
* .set("age", 30)
34+
* .set("access_time", DateTime.now())
3135
* .build();
3236
* datastore.put(entity);
33-
* } else {
34-
* boolean updated = entity.getBoolean("updated");
35-
* if (!updated) {
36-
* String[] name = entity.getString("name").split(" ");
37-
* entity = Entity.builder(entity)
38-
* .set("name", name[0])
39-
* .set("last_name", StringValue.builder(name[1]).indexed(false).build())
40-
* .set("updated", true)
41-
* .remove("old_property")
42-
* .set("new_property", 1.1)
43-
* .build();
44-
* datastore.update(entity);
45-
* }
46-
* }
47-
* } </pre>
48-
*
37+
* }} </pre>
38+
* <p>
39+
* This second example shows how to get and update a Datastore entity if it exists. For the complete
40+
* source code see
41+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java">
42+
* gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.UpdateEntity</a>.
43+
* <pre> {@code
44+
* Datastore datastore = DatastoreOptions.defaultInstance().service();
45+
* KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
46+
* Key key = keyFactory.newKey("keyName");
47+
* Entity entity = datastore.get(key);
48+
* if (entity != null) {
49+
* System.out.println("Updating access_time for " + entity.getString("name"));
50+
* entity = Entity.builder(entity)
51+
* .set("access_time", DateTime.now())
52+
* .build();
53+
* datastore.update(entity);
54+
* }} </pre>
4955
* <p>When using gcloud-java from outside of App/Compute Engine, you have to <a
5056
* href="https://github.com/GoogleCloudPlatform/gcloud-java#specifying-a-project-id">specify a
5157
* project ID</a> and

0 commit comments

Comments
 (0)