Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…d-java into pubsub-alpha
  • Loading branch information
aozarov committed Feb 12, 2016
2 parents 063d000 + 1ab48bb commit d28baaf
Show file tree
Hide file tree
Showing 55 changed files with 2,494 additions and 1,533 deletions.
130 changes: 82 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Java idiomatic client for [Google Cloud Platform][cloud-platform] services.
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
[![Maven](https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java.svg)]( https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java.svg)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/9da006ad7c3a4fe1abd142e77c003917)](https://www.codacy.com/app/mziccard/gcloud-java)

- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs)
Expand Down Expand Up @@ -42,17 +43,17 @@ libraryDependencies += "com.google.gcloud" % "gcloud-java" % "0.1.3"
Example Applications
--------------------

- [`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
- [`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
- 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).
- [`Bookshelf`](https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/bookshelf) - An App Engine app that manages a virtual bookshelf.
- This app uses `gcloud-java` to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service.
- [`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
- [`DatastoreExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/DatastoreExample.java) - A simple command line interface for the Cloud Datastore
- 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).
- [`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
- [`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
- 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).
- [`SparkDemo`](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/managedvms/sparkjava) - An example of using gcloud-java-datastore from within the SparkJava and App Engine Managed VM frameworks.
- Read about how it works on the example's [README page](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/managedvms/sparkjava#how-does-it-work).
- [`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
- [`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.
- 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).
- [`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
- 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).

Specifying a Project ID
Expand Down Expand Up @@ -123,15 +124,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
[CreateTableAndLoadData.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java).
```java
import com.google.gcloud.bigquery.BigQuery;
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 +146,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,8 +170,32 @@ 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 create a Datastore entity. Complete source code can be found at
[CreateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/CreateEntity.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 = Entity.builder(key)
.set("name", "John Doe")
.set("age", 30)
.set("access_time", DateTime.now())
.build();
datastore.put(entity);
```
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
found at
[UpdateEntity.java](./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;
Expand All @@ -182,17 +205,10 @@ 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");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity == null) {
entity = Entity.builder(key)
.set("name", "John Do")
.set("age", 30)
.set("access_time", DateTime.now())
.build();
datastore.put(entity);
} else {
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 +226,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
[UpdateAndListProjects.java](./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 All @@ -219,14 +236,15 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
import java.util.Iterator;
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
Project myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
Project newProject = myProject.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
System.out.println("Updated the labels of project " + newProject.projectId()
+ " to be " + newProject.labels());
// List all the projects you have permission to view.
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
if (project != null) {
Project newProject = project.toBuilder()
.addLabel("launch-status", "in-development")
.build()
.replace();
System.out.println("Updated the labels of project " + newProject.projectId()
+ " to be " + newProject.labels());
}
Iterator<Project> projectIterator = resourceManager.list().iterateAll();
System.out.println("Projects I can view:");
while (projectIterator.hasNext()) {
Expand All @@ -244,8 +262,28 @@ 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 create a Storage blob. Complete source code can be found at
[CreateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/CreateBlob.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");
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
Blob 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
[UpdateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/UpdateBlob.java).
```java
import static java.nio.charset.StandardCharsets.UTF_8;
Expand All @@ -259,12 +297,8 @@ import java.nio.channels.WritableByteChannel;
Storage storage = StorageOptions.defaultInstance().service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = Blob.get(storage, 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());
Blob blob = storage.get(blobId);
if (blob != null) {
byte[] prevContent = blob.content();
System.out.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
Expand Down
1 change: 1 addition & 0 deletions codacy-conf.json

Large diffs are not rendered by default.

89 changes: 6 additions & 83 deletions gcloud-java-bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Java idiomatic client for [Google Cloud BigQuery] (https://cloud.google.com/bigq
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
[![Maven](https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java-bigquery.svg)]( https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java-bigquery.svg)
[![Codacy Badge](https://api.codacy.com/project/badge/grade/9da006ad7c3a4fe1abd142e77c003917)](https://www.codacy.com/app/mziccard/gcloud-java)

- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/bigquery/package-summary.html)
Expand Down Expand Up @@ -34,7 +35,7 @@ libraryDependencies += "com.google.gcloud" % "gcloud-java-bigquery" % "0.1.3"

Example Application
-------------------
- [`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.
- [`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.
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).

Authentication
Expand Down Expand Up @@ -200,91 +201,13 @@ while (rowIterator.hasNext()) {
```
#### 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, simply move
In
[InsertDataAndQueryTable.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java)
we put together all the code shown above into one program. The program assumes that you are
running on Compute Engine or from your own desktop. To run the 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.

```java
import com.google.gcloud.bigquery.BigQuery;
import com.google.gcloud.bigquery.BigQueryOptions;
import com.google.gcloud.bigquery.DatasetInfo;
import com.google.gcloud.bigquery.Field;
import com.google.gcloud.bigquery.FieldValue;
import com.google.gcloud.bigquery.InsertAllRequest;
import com.google.gcloud.bigquery.InsertAllResponse;
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;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GcloudBigQueryExample {

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

// Create a service instance
BigQuery bigquery = BigQueryOptions.defaultInstance().service();

// Create a dataset
String datasetId = "my_dataset_id";
bigquery.create(DatasetInfo.builder(datasetId).build());

TableId tableId = TableId.of(datasetId, "my_table_id");
// Table field definition
Field stringField = Field.of("StringField", Field.Type.string());
// Table schema definition
Schema schema = Schema.of(stringField);
// Create a table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));

// Define rows to insert
Map<String, Object> firstRow = new HashMap<>();
Map<String, Object> secondRow = new HashMap<>();
firstRow.put("StringField", "value1");
secondRow.put("StringField", "value2");
// Create an insert request
InsertAllRequest insertRequest = InsertAllRequest.builder(tableId)
.addRow(firstRow)
.addRow(secondRow)
.build();
// Insert rows
InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
// Check if errors occurred
if (insertResponse.hasErrors()) {
System.out.println("Errors occurred while inserting rows");
}

// Create a query request
QueryRequest queryRequest =
QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
.maxWaitTime(60000L)
.maxResults(1000L)
.build();
// Request query to be executed and wait for results
QueryResponse queryResponse = bigquery.query(queryRequest);
while (!queryResponse.jobCompleted()) {
Thread.sleep(1000L);
queryResponse = bigquery.getQueryResults(queryResponse.jobId());
}
// Read rows
Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll();
System.out.println("Table rows:");
while (rowIterator.hasNext()) {
System.out.println(rowIterator.next());
}
}
}
```

Troubleshooting
---------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
return obj.getClass().equals(DatasetInfo.class)
return obj != null
&& obj.getClass().equals(DatasetInfo.class)
&& Objects.equals(toPb(), ((DatasetInfo) obj).toPb());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.util.Objects;

/**
* Google BigQuery external table type. BigQuery's external tables are tables whose data reside
* outside of BigQuery but can be queried as normal BigQuery tables. External tables are
* Google BigQuery external table definition. BigQuery's external tables are tables whose data
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
* experimental and might be subject to change or removed.
*
* @see <a href="https://cloud.google.com/bigquery/federated-data-sources">Federated Data Sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
return obj.getClass().equals(JobInfo.class) && Objects.equals(toPb(), ((JobInfo) obj).toPb());
return obj != null
&& obj.getClass().equals(JobInfo.class)
&& Objects.equals(toPb(), ((JobInfo) obj).toPb());
}

JobInfo setProjectId(String projectId) {
Expand Down
Loading

0 comments on commit d28baaf

Please sign in to comment.