Skip to content

Commit 7864a93

Browse files
committed
Merge pull request #635 from mziccard/refactor-examples
Refactor examples
2 parents 307ddf3 + 8c9cb2e commit 7864a93

24 files changed

Lines changed: 885 additions & 382 deletions

File tree

README.md

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.3"
4242
Example Applications
4343
--------------------
4444

45-
- [`BigQueryExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java) - A simple command line interface providing some of Cloud BigQuery's functionality
45+
- [`BigQueryExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java) - A simple command line interface providing some of Cloud BigQuery's functionality
4646
- Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/BigQueryExample.html).
4747
- [`Bookshelf`](https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/bookshelf) - An App Engine app that manages a virtual bookshelf.
4848
- This app uses `gcloud-java` to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service.
49-
- [`DatastoreExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/DatastoreExample.java) - A simple command line interface for the Cloud Datastore
49+
- [`DatastoreExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/DatastoreExample.java) - A simple command line interface for the Cloud Datastore
5050
- Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/DatastoreExample.html).
51-
- [`ResourceManagerExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java) - A simple command line interface providing some of Cloud Resource Manager's functionality
51+
- [`ResourceManagerExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/ResourceManagerExample.java) - A simple command line interface providing some of Cloud Resource Manager's functionality
5252
- Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/ResourceManagerExample.html).
5353
- [`SparkDemo`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managed_vms/sparkjava) - An example of using gcloud-java-datastore from within the SparkJava and App Engine Managed VM frameworks.
5454
- Read about how it works on the example's [README page](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/managed_vms/sparkjava#how-does-it-work).
55-
- [`StorageExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java) - A simple command line interface providing some of Cloud Storage's functionality
55+
- [`StorageExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/StorageExample.java) - A simple command line interface providing some of Cloud Storage's functionality
5656
- Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/StorageExample.html).
5757

5858
Specifying a Project ID
@@ -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+
[CreateTableAndLoadData.java](./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,8 +169,32 @@ 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 create a Datastore entity. Complete source code can be found at
175+
[CreateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/CreateEntity.java).
176+
177+
```java
178+
import com.google.gcloud.datastore.Datastore;
179+
import com.google.gcloud.datastore.DatastoreOptions;
180+
import com.google.gcloud.datastore.DateTime;
181+
import com.google.gcloud.datastore.Entity;
182+
import com.google.gcloud.datastore.Key;
183+
import com.google.gcloud.datastore.KeyFactory;
175184
185+
Datastore datastore = DatastoreOptions.defaultInstance().service();
186+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
187+
Key key = keyFactory.newKey("keyName");
188+
Entity entity = Entity.builder(key)
189+
.set("name", "John Doe")
190+
.set("age", 30)
191+
.set("access_time", DateTime.now())
192+
.build();
193+
datastore.put(entity);
194+
```
195+
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
196+
found at
197+
[UpdateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java).
176198
```java
177199
import com.google.gcloud.datastore.Datastore;
178200
import com.google.gcloud.datastore.DatastoreOptions;
@@ -182,17 +204,10 @@ import com.google.gcloud.datastore.Key;
182204
import com.google.gcloud.datastore.KeyFactory;
183205
184206
Datastore datastore = DatastoreOptions.defaultInstance().service();
185-
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
186-
Key key = keyFactory.newKey(keyName);
207+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
208+
Key key = keyFactory.newKey("keyName");
187209
Entity entity = datastore.get(key);
188-
if (entity == null) {
189-
entity = Entity.builder(key)
190-
.set("name", "John Do")
191-
.set("age", 30)
192-
.set("access_time", DateTime.now())
193-
.build();
194-
datastore.put(entity);
195-
} else {
210+
if (entity != null) {
196211
System.out.println("Updating access_time for " + entity.getString("name"));
197212
entity = Entity.builder(entity)
198213
.set("access_time", DateTime.now())
@@ -210,7 +225,8 @@ Google Cloud Resource Manager (Alpha)
210225
#### Preview
211226
212227
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-
228+
Complete source code can be found at
229+
[UpdateAndListProjects.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/UpdateAndListProjects.java).
214230
```java
215231
import com.google.gcloud.resourcemanager.Project;
216232
import com.google.gcloud.resourcemanager.ResourceManager;
@@ -219,14 +235,15 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
219235
import java.util.Iterator;
220236
221237
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
222-
Project myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
223-
Project newProject = myProject.toBuilder()
224-
.addLabel("launch-status", "in-development")
225-
.build()
226-
.replace();
227-
System.out.println("Updated the labels of project " + newProject.projectId()
228-
+ " to be " + newProject.labels());
229-
// List all the projects you have permission to view.
238+
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
239+
if (project != null) {
240+
Project newProject = project.toBuilder()
241+
.addLabel("launch-status", "in-development")
242+
.build()
243+
.replace();
244+
System.out.println("Updated the labels of project " + newProject.projectId()
245+
+ " to be " + newProject.labels());
246+
}
230247
Iterator<Project> projectIterator = resourceManager.list().iterateAll();
231248
System.out.println("Projects I can view:");
232249
while (projectIterator.hasNext()) {
@@ -244,13 +261,32 @@ Google Cloud Storage
244261
245262
#### Preview
246263
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.
264+
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.
265+
266+
The first snippet shows how to create a Storage blob. Complete source code can be found at
267+
[CreateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/CreateBlob.java).
248268
249269
```java
250270
import static java.nio.charset.StandardCharsets.UTF_8;
251271
252272
import com.google.gcloud.storage.Blob;
273+
import com.google.gcloud.storage.BlobId;
253274
import com.google.gcloud.storage.BlobInfo;
275+
import com.google.gcloud.storage.Storage;
276+
import com.google.gcloud.storage.StorageOptions;
277+
278+
Storage storage = StorageOptions.defaultInstance().service();
279+
BlobId blobId = BlobId.of("bucket", "blob_name");
280+
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
281+
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
282+
```
283+
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
284+
found at
285+
[UpdateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/UpdateBlob.java).
286+
```java
287+
import static java.nio.charset.StandardCharsets.UTF_8;
288+
289+
import com.google.gcloud.storage.Blob;
254290
import com.google.gcloud.storage.BlobId;
255291
import com.google.gcloud.storage.Storage;
256292
import com.google.gcloud.storage.StorageOptions;
@@ -261,11 +297,7 @@ import java.nio.channels.WritableByteChannel;
261297
Storage storage = StorageOptions.defaultInstance().service();
262298
BlobId blobId = BlobId.of("bucket", "blob_name");
263299
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());
300+
if (blob != null) {
269301
byte[] prevContent = blob.content();
270302
System.out.println(new String(prevContent, UTF_8));
271303
WritableByteChannel channel = blob.writer();

gcloud-java-bigquery/README.md

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.3"
3434

3535
Example Application
3636
-------------------
37-
- [`BigQueryExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java) - A simple command line interface providing some of Cloud BigQuery's functionality.
37+
- [`BigQueryExample`](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/BigQueryExample.java) - A simple command line interface providing some of Cloud BigQuery's functionality.
3838
Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/BigQueryExample.html).
3939

4040
Authentication
@@ -200,91 +200,13 @@ while (rowIterator.hasNext()) {
200200
```
201201
#### Complete source code
202202

203-
Here we put together all the code shown above into one program. This program assumes that you are
204-
running on Compute Engine or from your own desktop. To run this example on App Engine, simply move
203+
In
204+
[InsertDataAndQueryTable.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java)
205+
we put together all the code shown above into one program. The program assumes that you are
206+
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
205207
the code from the main method to your application's servlet class and change the print statements to
206208
display on your webpage.
207209

208-
```java
209-
import com.google.gcloud.bigquery.BigQuery;
210-
import com.google.gcloud.bigquery.BigQueryOptions;
211-
import com.google.gcloud.bigquery.DatasetInfo;
212-
import com.google.gcloud.bigquery.Field;
213-
import com.google.gcloud.bigquery.FieldValue;
214-
import com.google.gcloud.bigquery.InsertAllRequest;
215-
import com.google.gcloud.bigquery.InsertAllResponse;
216-
import com.google.gcloud.bigquery.QueryRequest;
217-
import com.google.gcloud.bigquery.QueryResponse;
218-
import com.google.gcloud.bigquery.Schema;
219-
import com.google.gcloud.bigquery.StandardTableDefinition;
220-
import com.google.gcloud.bigquery.Table;
221-
import com.google.gcloud.bigquery.TableId;
222-
import com.google.gcloud.bigquery.TableInfo;
223-
224-
import java.util.HashMap;
225-
import java.util.Iterator;
226-
import java.util.List;
227-
import java.util.Map;
228-
229-
public class GcloudBigQueryExample {
230-
231-
public static void main(String[] args) throws InterruptedException {
232-
233-
// Create a service instance
234-
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
235-
236-
// Create a dataset
237-
String datasetId = "my_dataset_id";
238-
bigquery.create(DatasetInfo.builder(datasetId).build());
239-
240-
TableId tableId = TableId.of(datasetId, "my_table_id");
241-
// Table field definition
242-
Field stringField = Field.of("StringField", Field.Type.string());
243-
// Table schema definition
244-
Schema schema = Schema.of(stringField);
245-
// Create a table
246-
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
247-
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
248-
249-
// Define rows to insert
250-
Map<String, Object> firstRow = new HashMap<>();
251-
Map<String, Object> secondRow = new HashMap<>();
252-
firstRow.put("StringField", "value1");
253-
secondRow.put("StringField", "value2");
254-
// Create an insert request
255-
InsertAllRequest insertRequest = InsertAllRequest.builder(tableId)
256-
.addRow(firstRow)
257-
.addRow(secondRow)
258-
.build();
259-
// Insert rows
260-
InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
261-
// Check if errors occurred
262-
if (insertResponse.hasErrors()) {
263-
System.out.println("Errors occurred while inserting rows");
264-
}
265-
266-
// Create a query request
267-
QueryRequest queryRequest =
268-
QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
269-
.maxWaitTime(60000L)
270-
.maxResults(1000L)
271-
.build();
272-
// Request query to be executed and wait for results
273-
QueryResponse queryResponse = bigquery.query(queryRequest);
274-
while (!queryResponse.jobCompleted()) {
275-
Thread.sleep(1000L);
276-
queryResponse = bigquery.getQueryResults(queryResponse.jobId());
277-
}
278-
// Read rows
279-
Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll();
280-
System.out.println("Table rows:");
281-
while (rowIterator.hasNext()) {
282-
System.out.println(rowIterator.next());
283-
}
284-
}
285-
}
286-
```
287-
288210
Troubleshooting
289211
---------------
290212

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+
* CreateTableAndLoadData.java</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>

0 commit comments

Comments
 (0)