Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor examples #635

Merged
merged 7 commits into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ Google Cloud BigQuery (Alpha)

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.
Complete source code can be found at

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

[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).

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


```java
import com.google.gcloud.bigquery.BigQuery;

This comment was marked as spam.

This comment was marked as spam.

import com.google.gcloud.bigquery.BigQueryOptions;
import com.google.gcloud.bigquery.Field;
import com.google.gcloud.bigquery.FormatOptions;
import com.google.gcloud.bigquery.Job;
import com.google.gcloud.bigquery.JobStatus;
import com.google.gcloud.bigquery.JobInfo;
import com.google.gcloud.bigquery.LoadJobConfiguration;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.StandardTableDefinition;
import com.google.gcloud.bigquery.Table;
Expand All @@ -145,19 +145,17 @@ if (table == null) {
System.out.println("Creating table " + tableId);
Field integerField = Field.of("fieldName", Field.Type.integer());
Schema schema = Schema.of(integerField);
bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
}
System.out.println("Loading data into table " + tableId);
Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
while (!loadJob.isDone()) {
Thread.sleep(1000L);
}
if (loadJob.status().error() != null) {
System.out.println("Job completed with errors");
} else {
System.out.println("Loading data into table " + tableId);
LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
Job loadJob = bigquery.create(JobInfo.of(configuration));
while (!loadJob.isDone()) {
Thread.sleep(1000L);
}
if (loadJob.status().error() != null) {
System.out.println("Job completed with errors");
} else {
System.out.println("Job succeeded");
}
System.out.println("Job succeeded");
}
```

Expand All @@ -171,7 +169,11 @@ Google Cloud Datastore

#### Preview

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.
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.

The first snippet shows how to get a Datastore entity and create it if it does not exist. Complete
source code can be found at
[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).

```java
import com.google.gcloud.datastore.Datastore;
Expand All @@ -182,8 +184,8 @@ import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
Key key = keyFactory.newKey(keyName);
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");

This comment was marked as spam.

Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity == null) {
entity = Entity.builder(key)
Expand All @@ -192,7 +194,24 @@ if (entity == null) {
.set("access_time", DateTime.now())
.build();
datastore.put(entity);
} else {
}
```
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
found at
[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).
```java
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.builder(entity)
.set("access_time", DateTime.now())
Expand All @@ -210,7 +229,8 @@ Google Cloud Resource Manager (Alpha)
#### Preview

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).

Complete source code can be found at

This comment was marked as spam.

[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).
```java
import com.google.gcloud.resourcemanager.Project;
import com.google.gcloud.resourcemanager.ResourceManager;
Expand Down Expand Up @@ -244,13 +264,36 @@ Google Cloud Storage

#### Preview

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.
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.

The first snippet shows how to get a Storage blob and create it if it does not exist. Complete
source code can be found at
[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).

```java
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;

Storage storage = StorageOptions.defaultInstance().service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = storage.get(blobId);
if (blob == null) {

This comment was marked as spam.

BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
}
```
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
found at
[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).
```java
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
Expand All @@ -261,11 +304,7 @@ import java.nio.channels.WritableByteChannel;
Storage storage = StorageOptions.defaultInstance().service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = storage.get(blobId);
if (blob == null) {
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
} else {
System.out.println("Updating content for " + blobId.name());
if (blob != null) {
byte[] prevContent = blob.content();
System.out.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
Expand Down
11 changes: 6 additions & 5 deletions gcloud-java-bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ while (rowIterator.hasNext()) {
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 and change the print statements to
display on your webpage.
display on your webpage. Complete source code can be found at

This comment was marked as spam.

This comment was marked as spam.

[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).


```java
import com.google.gcloud.bigquery.BigQuery;
Expand All @@ -217,7 +219,6 @@ import com.google.gcloud.bigquery.QueryRequest;
import com.google.gcloud.bigquery.QueryResponse;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.StandardTableDefinition;
import com.google.gcloud.bigquery.Table;
import com.google.gcloud.bigquery.TableId;
import com.google.gcloud.bigquery.TableInfo;

Expand All @@ -226,9 +227,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GcloudBigQueryExample {
public class InsertDataAndQueryTable {

public static void main(String[] args) throws InterruptedException {
public static void main(String... args) throws InterruptedException {

// Create a service instance
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
Expand All @@ -244,7 +245,7 @@ public class GcloudBigQueryExample {
Schema schema = Schema.of(stringField);
// Create a table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
bigquery.create(TableInfo.of(tableId, tableDefinition));

// Define rows to insert
Map<String, Object> firstRow = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
/**
* A client to Google Cloud BigQuery.
*
* <p>A simple usage example:
* <p>A simple usage example showing how to create a table if it does not exist and load data into
* it. For the complete source code see
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java">
* gcloud-java-examples:com.google.gcloud.examples.bigquery.snippets.CreateTableAndLoadData</a>.

This comment was marked as spam.

* <pre> {@code
* BigQuery bigquery = BigQueryOptions.defaultInstance().service();
* TableId tableId = TableId.of("dataset", "table");
Expand All @@ -26,19 +29,17 @@
* System.out.println("Creating table " + tableId);
* Field integerField = Field.of("fieldName", Field.Type.integer());
* Schema schema = Schema.of(integerField);
* bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
* table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
* }
* System.out.println("Loading data into table " + tableId);
* Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
* while (!loadJob.isDone()) {
* Thread.sleep(1000L);
* }
* if (loadJob.status().error() != null) {
* System.out.println("Job completed with errors");
* } else {
* System.out.println("Loading data into table " + tableId);
* LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
* Job loadJob = bigquery.create(JobInfo.of(configuration));
* while (!loadJob.isDone()) {
* Thread.sleep(1000L);
* }
* if (loadJob.status().error() != null) {
* System.out.println("Job completed with errors");
* } else {
* System.out.println("Job succeeded");
* }
* System.out.println("Job succeeded");
* }}</pre>
*
* @see <a href="https://cloud.google.com/bigquery/">Google Cloud BigQuery</a>
Expand Down
8 changes: 5 additions & 3 deletions gcloud-java-datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ Cloud Datastore relies on indexing to run queries. Indexing is turned on by defa

#### Complete source code

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`.
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`.
Complete source code can be found at
[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).

```java
import com.google.gcloud.datastore.Datastore;
Expand All @@ -147,9 +149,9 @@ import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;

public class GcloudDatastoreExample {
public class AddEntitiesAndRunQuery {

public static void main(String[] args) {
public static void main(String... args) {
// Create datastore service object.
// By default, credentials are inferred from the runtime environment.
Datastore datastore = DatastoreOptions.defaultInstance().service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,41 @@
/**
* A client to the Google Cloud Datastore.
*
* <p>Here's a simple usage example for using gcloud-java from App/Compute Engine:
* <p>Here's a simple usage example for using gcloud-java from App/Compute Engine. This example
* shows how to get a Datastore entity and create it if it does not exist. For the complete source
* code see
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/GetOrCreateEntity.java">
* gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.GetOrCreateEntity</a>.
* <pre> {@code
* Datastore datastore = DatastoreOptions.defaultInstance().service();
* KeyFactory keyFactory = datastore.newKeyFactory().kind(kind);
* Key key = keyFactory.newKey(keyName);
* KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
* Key key = keyFactory.newKey("keyName");
* Entity entity = datastore.get(key);
* if (entity == null) {
* entity = Entity.builder(key)
* .set("name", "John Do")
* .set("age", LongValue.builder(100).indexed(false).build())
* .set("updated", false)
* .set("age", 30)
* .set("access_time", DateTime.now())
* .build();
* datastore.put(entity);
* } else {
* boolean updated = entity.getBoolean("updated");
* if (!updated) {
* String[] name = entity.getString("name").split(" ");
* entity = Entity.builder(entity)
* .set("name", name[0])
* .set("last_name", StringValue.builder(name[1]).indexed(false).build())
* .set("updated", true)
* .remove("old_property")
* .set("new_property", 1.1)
* .build();
* datastore.update(entity);
* }
* }
* } </pre>
*
* }} </pre>
* <p>
* This second example shows how to get and update a Datastore entity if it exists. For the complete
* source code see
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java">
* gcloud-java-examples:com.google.gcloud.examples.datastore.snippets.UpdateEntity</a>.
* <pre> {@code
* Datastore datastore = DatastoreOptions.defaultInstance().service();
* KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
* Key key = keyFactory.newKey("keyName");
* Entity entity = datastore.get(key);
* if (entity != null) {
* System.out.println("Updating access_time for " + entity.getString("name"));
* entity = Entity.builder(entity)
* .set("access_time", DateTime.now())
* .build();
* datastore.update(entity);
* }} </pre>
* <p>When using gcloud-java from outside of App/Compute Engine, you have to <a
* href="https://github.com/GoogleCloudPlatform/gcloud-java#specifying-a-project-id">specify a
* project ID</a> and
Expand Down
Loading