Skip to content

Commit 1c69b05

Browse files
committed
Merge pull request #574 from GoogleCloudPlatform/master
Merge to bring the branch up to speed
2 parents 7493f1a + 3f7626d commit 1c69b05

73 files changed

Lines changed: 3284 additions & 1782 deletions

File tree

Some content is hidden

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

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static Entity fromPb(Access access) {
104104
}
105105
// Unreachable
106106
throw new BigQueryException(BigQueryException.UNKNOWN_CODE,
107-
"Unrecognized access configuration", false);
107+
"Unrecognized access configuration");
108108
}
109109
}
110110

@@ -370,22 +370,11 @@ Access toPb() {
370370
}
371371
}
372372

373-
/**
374-
* Build an ACL for an {@code entity} and a {@code role}.
375-
*/
376-
public Acl(Entity entity, Role role) {
373+
private Acl(Entity entity, Role role) {
377374
this.entity = checkNotNull(entity);
378375
this.role = role;
379376
}
380377

381-
/**
382-
* Build an ACL for a view entity.
383-
*/
384-
public Acl(View view) {
385-
this.entity = checkNotNull(view);
386-
this.role = null;
387-
}
388-
389378
/**
390379
* Returns the entity for this ACL.
391380
*/
@@ -400,6 +389,23 @@ public Role role() {
400389
return role;
401390
}
402391

392+
/**
393+
* Returns an Acl object.
394+
*
395+
* @param entity the entity for this ACL object
396+
* @param role the role to associate to the {@code entity} object
397+
*/
398+
public static Acl of(Entity entity, Role role) {
399+
return new Acl(entity, role);
400+
}
401+
402+
/**
403+
* Returns an Acl object for a view entity.
404+
*/
405+
public static Acl of(View view) {
406+
return new Acl(view, null);
407+
}
408+
403409
@Override
404410
public int hashCode() {
405411
return Objects.hash(entity, role);
@@ -432,7 +438,7 @@ Access toPb() {
432438
}
433439

434440
static Acl fromPb(Access access) {
435-
return new Acl(Entity.fromPb(access),
441+
return Acl.of(Entity.fromPb(access),
436442
access.getRole() != null ? Role.valueOf(access.getRole()) : null);
437443
}
438444
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public static QueryResultsOption startIndex(long startIndex) {
443443
/**
444444
* Returns an option that sets how long to wait for the query to complete, in milliseconds,
445445
* before returning. Default is 10 seconds. If the timeout passes before the job completes,
446-
* {@link QueryResponse#jobComplete()} will be {@code false}.
446+
* {@link QueryResponse#jobCompleted()} will be {@code false}.
447447
*/
448448
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
449449
checkArgument(maxWaitTime >= 0);
@@ -662,4 +662,12 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
662662
* @throws BigQueryException upon failure
663663
*/
664664
QueryResponse getQueryResults(JobId job, QueryResultsOption... options) throws BigQueryException;
665+
666+
/**
667+
* Returns a channel to write data to be inserted into a BigQuery table. Data format and other
668+
* options can be configured using the {@link LoadConfiguration} parameter.
669+
*
670+
* @throws BigQueryException upon failure
671+
*/
672+
TableDataWriteChannel writer(LoadConfiguration loadConfiguration);
665673
}

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
package com.google.gcloud.bigquery;
1818

19+
import com.google.api.client.googleapis.json.GoogleJsonError;
20+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
21+
import com.google.common.collect.ImmutableSet;
1922
import com.google.gcloud.BaseServiceException;
2023
import com.google.gcloud.RetryHelper.RetryHelperException;
2124
import com.google.gcloud.RetryHelper.RetryInterruptedException;
2225

26+
import java.io.IOException;
27+
import java.util.Set;
28+
2329
/**
2430
* BigQuery service exception.
2531
*
@@ -28,17 +34,31 @@
2834
*/
2935
public class BigQueryException extends BaseServiceException {
3036

31-
private static final long serialVersionUID = -5504832700512784654L;
32-
public static final int UNKNOWN_CODE = -1;
37+
// see: https://cloud.google.com/bigquery/troubleshooting-errors
38+
private static final Set<Error> RETRYABLE_ERRORS = ImmutableSet.of(
39+
new Error(500, null),
40+
new Error(502, null),
41+
new Error(503, null),
42+
new Error(504, null));
43+
private static final long serialVersionUID = -5006625989225438209L;
3344

3445
private final BigQueryError error;
3546

36-
public BigQueryException(int code, String message, boolean retryable) {
37-
this(code, message, retryable, null);
47+
public BigQueryException(int code, String message) {
48+
this(code, message, null);
3849
}
3950

40-
public BigQueryException(int code, String message, boolean retryable, BigQueryError error) {
41-
super(code, message, retryable);
51+
public BigQueryException(int code, String message, BigQueryError error) {
52+
super(code, message, error != null ? error.reason() : null, true);
53+
this.error = error;
54+
}
55+
56+
public BigQueryException(IOException exception) {
57+
super(exception, true);
58+
BigQueryError error = null;
59+
if (reason() != null) {
60+
error = new BigQueryError(reason(), location(), getMessage(), debugInfo());
61+
}
4262
this.error = error;
4363
}
4464

@@ -50,20 +70,20 @@ public BigQueryError error() {
5070
return error;
5171
}
5272

73+
@Override
74+
protected Set<Error> retryableErrors() {
75+
return RETRYABLE_ERRORS;
76+
}
77+
5378
/**
5479
* Translate RetryHelperException to the BigQueryException that caused the error. This method will
5580
* always throw an exception.
5681
*
5782
* @throws BigQueryException when {@code ex} was caused by a {@code BigQueryException}
5883
* @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException}
5984
*/
60-
static BigQueryException translateAndThrow(RetryHelperException ex) {
61-
if (ex.getCause() instanceof BigQueryException) {
62-
throw (BigQueryException) ex.getCause();
63-
}
64-
if (ex instanceof RetryInterruptedException) {
65-
RetryInterruptedException.propagate();
66-
}
67-
throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), false);
85+
static BaseServiceException translateAndThrow(RetryHelperException ex) {
86+
BaseServiceException.translateAndPropagateIfPossible(ex);
87+
throw new BigQueryException(UNKNOWN_CODE, ex.getMessage());
6888
}
6989
}

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
import com.google.common.collect.Lists;
3535
import com.google.common.collect.Maps;
3636
import com.google.gcloud.BaseService;
37-
import com.google.gcloud.ExceptionHandler;
38-
import com.google.gcloud.ExceptionHandler.Interceptor;
3937
import com.google.gcloud.Page;
4038
import com.google.gcloud.PageImpl;
4139
import com.google.gcloud.PageImpl.NextPageFetcher;
@@ -49,27 +47,6 @@
4947

5048
final class BigQueryImpl extends BaseService<BigQueryOptions> implements BigQuery {
5149

52-
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
53-
54-
private static final long serialVersionUID = -7478333733015750774L;
55-
56-
@Override
57-
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
58-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
59-
}
60-
61-
@Override
62-
public RetryResult beforeEval(Exception exception) {
63-
if (exception instanceof BigQueryException) {
64-
boolean retriable = ((BigQueryException) exception).retryable();
65-
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
66-
}
67-
return Interceptor.RetryResult.CONTINUE_EVALUATION;
68-
}
69-
};
70-
static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.builder()
71-
.abortOn(RuntimeException.class).interceptor(EXCEPTION_HANDLER_INTERCEPTOR).build();
72-
7350
private static class DatasetPageFetcher implements NextPageFetcher<DatasetInfo> {
7451

7552
private static final long serialVersionUID = -3057564042439021278L;
@@ -537,10 +514,10 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
537514
QueryResponse.Builder builder = QueryResponse.builder();
538515
JobId completeJobId = JobId.fromPb(results.getJobReference());
539516
builder.jobId(completeJobId);
540-
builder.jobComplete(results.getJobComplete());
517+
builder.jobCompleted(results.getJobComplete());
541518
List<TableRow> rowsPb = results.getRows();
542519
if (results.getJobComplete()) {
543-
builder.jobComplete(true);
520+
builder.jobCompleted(true);
544521
QueryResult.Builder resultBuilder = transformQueryResults(completeJobId, rowsPb,
545522
results.getPageToken(), options(), ImmutableMap.<BigQueryRpc.Option, Object>of());
546523
resultBuilder.totalBytesProcessed(results.getTotalBytesProcessed());
@@ -584,7 +561,7 @@ public GetQueryResultsResponse call() {
584561
JobId completeJobId = JobId.fromPb(results.getJobReference());
585562
builder.jobId(completeJobId);
586563
builder.etag(results.getEtag());
587-
builder.jobComplete(results.getJobComplete());
564+
builder.jobCompleted(results.getJobComplete());
588565
List<TableRow> rowsPb = results.getRows();
589566
if (results.getJobComplete()) {
590567
QueryResult.Builder resultBuilder = transformQueryResults(completeJobId, rowsPb,
@@ -619,6 +596,10 @@ private static QueryResult.Builder transformQueryResults(JobId jobId, List<Table
619596
.results(transformTableData(rowsPb));
620597
}
621598

599+
public TableDataWriteChannel writer(LoadConfiguration loadConfiguration) {
600+
return new TableDataWriteChannel(options(), setProjectId(loadConfiguration));
601+
}
602+
622603
private Map<BigQueryRpc.Option, ?> optionMap(Option... options) {
623604
Map<BigQueryRpc.Option, Object> optionMap = Maps.newEnumMap(BigQueryRpc.Option.class);
624605
for (Option option : options) {
@@ -640,7 +621,7 @@ private DatasetInfo setProjectId(DatasetInfo dataset) {
640621
if (viewReferencePb.getProjectId() == null) {
641622
viewReferencePb.setProjectId(options().projectId());
642623
}
643-
acls.add(new Acl(new Acl.View(TableId.fromPb(viewReferencePb))));
624+
acls.add(Acl.of(new Acl.View(TableId.fromPb(viewReferencePb))));
644625
} else {
645626
acls.add(acl);
646627
}
@@ -698,8 +679,7 @@ public TableId apply(TableId tableId) {
698679
if (job instanceof LoadJobInfo) {
699680
LoadJobInfo loadJob = (LoadJobInfo) job;
700681
LoadJobInfo.Builder loadBuilder = loadJob.toBuilder();
701-
loadBuilder.destinationTable(setProjectId(loadJob.destinationTable()));
702-
return loadBuilder.build();
682+
return loadBuilder.configuration(setProjectId(loadJob.configuration())).build();
703683
}
704684
return job;
705685
}
@@ -711,4 +691,10 @@ private QueryRequest setProjectId(QueryRequest request) {
711691
}
712692
return builder.build();
713693
}
694+
695+
private LoadConfiguration setProjectId(LoadConfiguration configuration) {
696+
LoadConfiguration.Builder builder = configuration.toBuilder();
697+
builder.destinationTable(setProjectId(configuration.destinationTable()));
698+
return builder.build();
699+
}
714700
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public Builder destinationTable(TableId destinationTable) {
9595
/**
9696
* Sets whether the job is allowed to create new tables.
9797
*
98-
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
99-
* Jobs: Link Configuration</a>
98+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
99+
* Create Disposition</a>
100100
*/
101101
public Builder createDisposition(CreateDisposition createDisposition) {
102102
this.createDisposition = createDisposition;
@@ -106,8 +106,8 @@ public Builder createDisposition(CreateDisposition createDisposition) {
106106
/**
107107
* Sets the action that should occur if the destination table already exists.
108108
*
109-
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
110-
* Jobs: Link Configuration</a>
109+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
110+
* Write Disposition</a>
111111
*/
112112
public Builder writeDisposition(WriteDisposition writeDisposition) {
113113
this.writeDisposition = writeDisposition;
@@ -145,8 +145,8 @@ public TableId destinationTable() {
145145
/**
146146
* Returns whether the job is allowed to create new tables.
147147
*
148-
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
149-
* Jobs: Copy Configuration</a>
148+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
149+
* Create Disposition</a>
150150
*/
151151
public CreateDisposition createDisposition() {
152152
return this.createDisposition;
@@ -155,8 +155,8 @@ public CreateDisposition createDisposition() {
155155
/**
156156
* Returns the action that should occur if the destination table already exists.
157157
*
158-
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
159-
* Jobs: Copy Configuration</a>
158+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
159+
* Write Disposition</a>
160160
*/
161161
public WriteDisposition writeDisposition() {
162162
return writeDisposition;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.api.services.bigquery.model.Table;
2222
import com.google.common.base.MoreObjects.ToStringHelper;
2323

24+
import java.util.Objects;
25+
2426
/**
2527
* Google BigQuery External Table information. BigQuery's external tables are tables whose data
2628
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
@@ -103,6 +105,17 @@ ToStringHelper toStringHelper() {
103105
return super.toStringHelper().add("configuration", configuration);
104106
}
105107

108+
@Override
109+
public boolean equals(Object obj) {
110+
return obj instanceof ExternalTableInfo
111+
&& Objects.equals(toPb(), ((ExternalTableInfo) obj).toPb());
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
return Objects.hash(super.hashCode(), configuration);
117+
}
118+
106119
@Override
107120
Table toPb() {
108121
Table tablePb = super.toPb();

0 commit comments

Comments
 (0)