Skip to content

Commit 38d257b

Browse files
committed
Minor refactoring of examples and READMEs
- Remove check for existence in datastore and storage create snippets - Rename GetOrCreateBlob to CreateBlob, GetOrCreateEntity to CreateEntity - Use relative links in READMEs - Remove full snippets in module's READMEs, replaced with link to class
1 parent edc9db7 commit 38d257b

File tree

13 files changed

+108
-360
lines changed

13 files changed

+108
-360
lines changed

README.md

Lines changed: 29 additions & 36 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
@@ -124,7 +124,7 @@ Google Cloud BigQuery (Alpha)
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.
126126
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).
127+
[CreateTableAndLoadData.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java).
128128
129129
```java
130130
import com.google.gcloud.bigquery.BigQuery;
@@ -171,9 +171,8 @@ Google Cloud Datastore
171171
172172
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.
173173
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).
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).
177176
178177
```java
179178
import com.google.gcloud.datastore.Datastore;
@@ -186,19 +185,16 @@ import com.google.gcloud.datastore.KeyFactory;
186185
Datastore datastore = DatastoreOptions.defaultInstance().service();
187186
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
188187
Key key = keyFactory.newKey("keyName");
189-
Entity entity = datastore.get(key);
190-
if (entity == null) {
191-
entity = Entity.builder(key)
192-
.set("name", "John Do")
193-
.set("age", 30)
194-
.set("access_time", DateTime.now())
195-
.build();
196-
datastore.put(entity);
197-
}
188+
Entity entity = Entity.builder(key)
189+
.set("name", "John Do")
190+
.set("age", 30)
191+
.set("access_time", DateTime.now())
192+
.build();
193+
datastore.put(entity);
198194
```
199195
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
200196
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).
197+
[UpdateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java).
202198
```java
203199
import com.google.gcloud.datastore.Datastore;
204200
import com.google.gcloud.datastore.DatastoreOptions;
@@ -230,7 +226,7 @@ Google Cloud Resource Manager (Alpha)
230226
231227
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).
232228
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).
229+
[UpdateAndListProjects.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/UpdateAndListProjects.java).
234230
```java
235231
import com.google.gcloud.resourcemanager.Project;
236232
import com.google.gcloud.resourcemanager.ResourceManager;
@@ -239,14 +235,15 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
239235
import java.util.Iterator;
240236
241237
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
242-
Project myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
243-
Project newProject = myProject.toBuilder()
244-
.addLabel("launch-status", "in-development")
245-
.build()
246-
.replace();
247-
System.out.println("Updated the labels of project " + newProject.projectId()
248-
+ " to be " + newProject.labels());
249-
// 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+
}
250247
Iterator<Project> projectIterator = resourceManager.list().iterateAll();
251248
System.out.println("Projects I can view:");
252249
while (projectIterator.hasNext()) {
@@ -266,9 +263,8 @@ Google Cloud Storage
266263
267264
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.
268265
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).
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).
272268
273269
```java
274270
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -281,15 +277,12 @@ import com.google.gcloud.storage.StorageOptions;
281277
282278
Storage storage = StorageOptions.defaultInstance().service();
283279
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-
}
280+
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
281+
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
289282
```
290283
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
291284
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).
285+
[UpdateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/UpdateBlob.java).
293286
```java
294287
import static java.nio.charset.StandardCharsets.UTF_8;
295288

gcloud-java-bigquery/README.md

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

289210
Troubleshooting
290211
---------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* <p>A simple usage example showing how to create a table if it does not exist and load data into
2121
* it. For the complete source code see
2222
* <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>.
23+
* CreateTableAndLoadData.java</a>.
2424
* <pre> {@code
2525
* BigQuery bigquery = BigQueryOptions.defaultInstance().service();
2626
* TableId tableId = TableId.of("dataset", "table");

gcloud-java-datastore/README.md

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -134,69 +134,12 @@ 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`.
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).
140-
141-
```java
142-
import com.google.gcloud.datastore.Datastore;
143-
import com.google.gcloud.datastore.DatastoreOptions;
144-
import com.google.gcloud.datastore.Entity;
145-
import com.google.gcloud.datastore.Key;
146-
import com.google.gcloud.datastore.KeyFactory;
147-
import com.google.gcloud.datastore.Query;
148-
import com.google.gcloud.datastore.QueryResults;
149-
import com.google.gcloud.datastore.StructuredQuery;
150-
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
151-
152-
public class AddEntitiesAndRunQuery {
153-
154-
public static void main(String... args) {
155-
// Create datastore service object.
156-
// By default, credentials are inferred from the runtime environment.
157-
Datastore datastore = DatastoreOptions.defaultInstance().service();
158-
159-
// Add an entity to Datastore
160-
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person");
161-
Key key = keyFactory.newKey("john.doe@gmail.com");
162-
Entity entity = Entity.builder(key)
163-
.set("name", "John Doe")
164-
.set("age", 51)
165-
.set("favorite_food", "pizza")
166-
.build();
167-
datastore.put(entity);
168-
169-
// Get an entity from Datastore
170-
Entity johnEntity = datastore.get(key);
171-
172-
// Add a couple more entities to make the query results more interesting
173-
Key janeKey = keyFactory.newKey("jane.doe@gmail.com");
174-
Entity janeEntity = Entity.builder(janeKey)
175-
.set("name", "Jane Doe")
176-
.set("age", 44)
177-
.set("favorite_food", "pizza")
178-
.build();
179-
Key joeKey = keyFactory.newKey("joe.shmoe@gmail.com");
180-
Entity joeEntity = Entity.builder(joeKey)
181-
.set("name", "Joe Shmoe")
182-
.set("age", 27)
183-
.set("favorite_food", "sushi")
184-
.build();
185-
datastore.put(janeEntity, joeEntity);
186-
187-
// Run a query
188-
Query<Entity> query = Query.entityQueryBuilder()
189-
.kind("Person")
190-
.filter(PropertyFilter.eq("favorite_food", "pizza"))
191-
.build();
192-
QueryResults<Entity> results = datastore.run(query);
193-
while (results.hasNext()) {
194-
Entity currentEntity = results.next();
195-
System.out.println(currentEntity.getString("name") + ", you're invited to a pizza party!");
196-
}
197-
}
198-
}
199-
```
137+
In
138+
[AddEntitiesAndRunQuery.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/AddEntitiesAndRunQuery.java).
139+
we put together all the code shown above into one program. The program assumes that you are
140+
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
141+
the code from the main method to your application's servlet class and change the print statements to
142+
display on your webpage.
200143

201144
Troubleshooting
202145
---------------

0 commit comments

Comments
 (0)