Skip to content

Commit d28baaf

Browse files
committed
Merge branch 'master' of https://github.com/GoogleCloudPlatform/gcloud-java into pubsub-alpha
2 parents 063d000 + 1ab48bb commit d28baaf

File tree

55 files changed

+2494
-1533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2494
-1533
lines changed

README.md

+82-48
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Java idiomatic client for [Google Cloud Platform][cloud-platform] services.
66
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
77
[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
88
[![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)
9+
[![Codacy Badge](https://api.codacy.com/project/badge/grade/9da006ad7c3a4fe1abd142e77c003917)](https://www.codacy.com/app/mziccard/gcloud-java)
910

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

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
46+
- [`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
4647
- 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).
4748
- [`Bookshelf`](https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/bookshelf) - An App Engine app that manages a virtual bookshelf.
4849
- 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
50+
- [`DatastoreExample`](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/DatastoreExample.java) - A simple command line interface for the Cloud Datastore
5051
- 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
52+
- [`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
5253
- 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).
53-
- [`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.
54-
- 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).
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
54+
- [`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.
55+
- 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).
56+
- [`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
5657
- 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).
5758

5859
Specifying a Project ID
@@ -123,15 +124,15 @@ Google Cloud BigQuery (Alpha)
123124
124125
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you
125126
must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
127+
Complete source code can be found at
128+
[CreateTableAndLoadData.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/CreateTableAndLoadData.java).
126129
127130
```java
128131
import com.google.gcloud.bigquery.BigQuery;
129132
import com.google.gcloud.bigquery.BigQueryOptions;
130133
import com.google.gcloud.bigquery.Field;
134+
import com.google.gcloud.bigquery.FormatOptions;
131135
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;
135136
import com.google.gcloud.bigquery.Schema;
136137
import com.google.gcloud.bigquery.StandardTableDefinition;
137138
import com.google.gcloud.bigquery.Table;
@@ -145,19 +146,17 @@ if (table == null) {
145146
System.out.println("Creating table " + tableId);
146147
Field integerField = Field.of("fieldName", Field.Type.integer());
147148
Schema schema = Schema.of(integerField);
148-
bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
149+
table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
150+
}
151+
System.out.println("Loading data into table " + tableId);
152+
Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
153+
while (!loadJob.isDone()) {
154+
Thread.sleep(1000L);
155+
}
156+
if (loadJob.status().error() != null) {
157+
System.out.println("Job completed with errors");
149158
} 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-
}
159+
System.out.println("Job succeeded");
161160
}
162161
```
163162
@@ -171,8 +170,32 @@ Google Cloud Datastore
171170
172171
#### Preview
173172
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.
173+
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.
174+
175+
The first snippet shows how to create a Datastore entity. Complete source code can be found at
176+
[CreateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/CreateEntity.java).
177+
178+
```java
179+
import com.google.gcloud.datastore.Datastore;
180+
import com.google.gcloud.datastore.DatastoreOptions;
181+
import com.google.gcloud.datastore.DateTime;
182+
import com.google.gcloud.datastore.Entity;
183+
import com.google.gcloud.datastore.Key;
184+
import com.google.gcloud.datastore.KeyFactory;
175185
186+
Datastore datastore = DatastoreOptions.defaultInstance().service();
187+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
188+
Key key = keyFactory.newKey("keyName");
189+
Entity entity = Entity.builder(key)
190+
.set("name", "John Doe")
191+
.set("age", 30)
192+
.set("access_time", DateTime.now())
193+
.build();
194+
datastore.put(entity);
195+
```
196+
The second snippet shows how to update a Datastore entity if it exists. Complete source code can be
197+
found at
198+
[UpdateEntity.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/datastore/snippets/UpdateEntity.java).
176199
```java
177200
import com.google.gcloud.datastore.Datastore;
178201
import com.google.gcloud.datastore.DatastoreOptions;
@@ -182,17 +205,10 @@ import com.google.gcloud.datastore.Key;
182205
import com.google.gcloud.datastore.KeyFactory;
183206
184207
Datastore datastore = DatastoreOptions.defaultInstance().service();
185-
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
186-
Key key = keyFactory.newKey(keyName);
208+
KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind");
209+
Key key = keyFactory.newKey("keyName");
187210
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 {
211+
if (entity != null) {
196212
System.out.println("Updating access_time for " + entity.getString("name"));
197213
entity = Entity.builder(entity)
198214
.set("access_time", DateTime.now())
@@ -210,7 +226,8 @@ Google Cloud Resource Manager (Alpha)
210226
#### Preview
211227
212228
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-
229+
Complete source code can be found at
230+
[UpdateAndListProjects.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/resourcemanager/snippets/UpdateAndListProjects.java).
214231
```java
215232
import com.google.gcloud.resourcemanager.Project;
216233
import com.google.gcloud.resourcemanager.ResourceManager;
@@ -219,14 +236,15 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
219236
import java.util.Iterator;
220237
221238
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.
239+
Project project = resourceManager.get("some-project-id"); // Use an existing project's ID
240+
if (project != null) {
241+
Project newProject = project.toBuilder()
242+
.addLabel("launch-status", "in-development")
243+
.build()
244+
.replace();
245+
System.out.println("Updated the labels of project " + newProject.projectId()
246+
+ " to be " + newProject.labels());
247+
}
230248
Iterator<Project> projectIterator = resourceManager.list().iterateAll();
231249
System.out.println("Projects I can view:");
232250
while (projectIterator.hasNext()) {
@@ -244,8 +262,28 @@ Google Cloud Storage
244262
245263
#### Preview
246264
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.
265+
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.
266+
267+
The first snippet shows how to create a Storage blob. Complete source code can be found at
268+
[CreateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/CreateBlob.java).
269+
270+
```java
271+
import static java.nio.charset.StandardCharsets.UTF_8;
272+
273+
import com.google.gcloud.storage.Blob;
274+
import com.google.gcloud.storage.BlobId;
275+
import com.google.gcloud.storage.BlobInfo;
276+
import com.google.gcloud.storage.Storage;
277+
import com.google.gcloud.storage.StorageOptions;
248278
279+
Storage storage = StorageOptions.defaultInstance().service();
280+
BlobId blobId = BlobId.of("bucket", "blob_name");
281+
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
282+
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
283+
```
284+
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
285+
found at
286+
[UpdateBlob.java](./gcloud-java-examples/src/main/java/com/google/gcloud/examples/storage/snippets/UpdateBlob.java).
249287
```java
250288
import static java.nio.charset.StandardCharsets.UTF_8;
251289
@@ -259,12 +297,8 @@ import java.nio.channels.WritableByteChannel;
259297
260298
Storage storage = StorageOptions.defaultInstance().service();
261299
BlobId blobId = BlobId.of("bucket", "blob_name");
262-
Blob blob = Blob.get(storage, blobId);
263-
if (blob == null) {
264-
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
265-
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
266-
} else {
267-
System.out.println("Updating content for " + blobId.name());
300+
Blob blob = storage.get(blobId);
301+
if (blob != null) {
268302
byte[] prevContent = blob.content();
269303
System.out.println(new String(prevContent, UTF_8));
270304
WritableByteChannel channel = blob.writer();

codacy-conf.json

+1
Large diffs are not rendered by default.

gcloud-java-bigquery/README.md

+6-83
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Java idiomatic client for [Google Cloud BigQuery] (https://cloud.google.com/bigq
66
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
77
[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
88
[![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)
9+
[![Codacy Badge](https://api.codacy.com/project/badge/grade/9da006ad7c3a4fe1abd142e77c003917)](https://www.codacy.com/app/mziccard/gcloud-java)
910

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

3536
Example Application
3637
-------------------
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.
38+
- [`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.
3839
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).
3940

4041
Authentication
@@ -200,91 +201,13 @@ while (rowIterator.hasNext()) {
200201
```
201202
#### Complete source code
202203

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
204+
In
205+
[InsertDataAndQueryTable.java](../gcloud-java-examples/src/main/java/com/google/gcloud/examples/bigquery/snippets/InsertDataAndQueryTable.java)
206+
we put together all the code shown above into one program. The program assumes that you are
207+
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
205208
the code from the main method to your application's servlet class and change the print statements to
206209
display on your webpage.
207210

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-
288211
Troubleshooting
289212
---------------
290213

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DatasetInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ public int hashCode() {
396396

397397
@Override
398398
public boolean equals(Object obj) {
399-
return obj.getClass().equals(DatasetInfo.class)
399+
return obj != null
400+
&& obj.getClass().equals(DatasetInfo.class)
400401
&& Objects.equals(toPb(), ((DatasetInfo) obj).toPb());
401402
}
402403

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import java.util.Objects;
2929

3030
/**
31-
* Google BigQuery external table type. BigQuery's external tables are tables whose data reside
32-
* outside of BigQuery but can be queried as normal BigQuery tables. External tables are
31+
* Google BigQuery external table definition. BigQuery's external tables are tables whose data
32+
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
3333
* experimental and might be subject to change or removed.
3434
*
3535
* @see <a href="https://cloud.google.com/bigquery/federated-data-sources">Federated Data Sources

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ public int hashCode() {
319319

320320
@Override
321321
public boolean equals(Object obj) {
322-
return obj.getClass().equals(JobInfo.class) && Objects.equals(toPb(), ((JobInfo) obj).toPb());
322+
return obj != null
323+
&& obj.getClass().equals(JobInfo.class)
324+
&& Objects.equals(toPb(), ((JobInfo) obj).toPb());
323325
}
324326

325327
JobInfo setProjectId(String projectId) {

0 commit comments

Comments
 (0)