Example usage of a query request: + *
{@code + * // Substitute "field", "table" and "dataset" with real field, table and dataset identifiers + * QueryRequest request = QueryRequest.builder("SELECT field FROM table") + * .defaultDataset(DatasetId.of("dataset")) + * .maxWaitTime(60000L) + * .maxResults(1000L) + * .build(); + * QueryResponse response = bigquery.query(request); + * while (!response.jobComplete()) { + * Thread.sleep(1000); + * response = bigquery.getQueryResults(response.jobId()); + * } + * List* * @see Query * @see Query Reference diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResponse.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResponse.java index b7bd5dc0efd0..8ef8351d9e1a 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResponse.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryResponse.java @@ -24,15 +24,16 @@ import java.util.Objects; /** - * Google Cloud BigQuery Query Response. This class contains the results of a Query Job or of a - * Query Request. + * Google Cloud BigQuery Query Response. This class contains the results of a Query Job + * ({@link BigQuery#getQueryResults(JobId, BigQuery.QueryResultsOption...)}) or of a + * Query Request ({@link BigQuery#query(QueryRequest)}). * *executionErrors = response.executionErrors(); + * // look for errors in executionErrors + * QueryResult result = response.result(); + * Iterator > rowIterator = result.iterateAll(); + * while(rowIterator.hasNext()) { + * List
row = rowIterator.next(); + * // do something with row + * } + * }
Example usage of a query response: *
{@code * QueryResponse response = bigquery.query(request); * while (!response.jobComplete()) { - * response = bigquery.getQueryResults(response.jobId()); * Thread.sleep(1000); + * response = bigquery.getQueryResults(response.jobId()); * } * ListexecutionErrors = response.executionErrors(); * // look for errors in executionErrors diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Schema.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Schema.java index 0ac6e1b84ade..787bb0d7f35f 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Schema.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Schema.java @@ -96,8 +96,8 @@ public Schema build() { } private Schema(Builder builder) { - this.fields = builder.fields != null ? ImmutableList.copyOf(builder.fields) : - ImmutableList. of(); + this.fields = builder.fields != null ? ImmutableList.copyOf(builder.fields) + : ImmutableList. of(); } /** diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableId.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableId.java index 6747dbf9923f..7a4e0bbb38b4 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableId.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableId.java @@ -50,7 +50,7 @@ public TableReference apply(TableId tableId) { private final String table; /** - * Returns project's user-defined id + * Returns project's user-defined id. */ public String project() { return project; diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java index 6594a3f25a67..7b47f4df8f19 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java @@ -16,8 +16,14 @@ package com.google.gcloud.bigquery; +import com.google.api.services.bigquery.model.Streamingbuffer; import com.google.api.services.bigquery.model.Table; import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; + +import java.io.Serializable; +import java.math.BigInteger; +import java.util.Objects; /** * A Google BigQuery Table information. A BigQuery table is a standard, two-dimensional table with @@ -34,6 +40,79 @@ public class TableInfo extends BaseTableInfo { private final String location; private final StreamingBuffer streamingBuffer; + /** + * Google BigQuery Table's Streaming Buffer information. This class contains information on a + * table's streaming buffer as the estimated size in number of rows/bytes. + */ + public static class StreamingBuffer implements Serializable { + + private static final long serialVersionUID = -6713971364725267597L; + private final long estimatedRows; + private final long estimatedBytes; + private final long oldestEntryTime; + + StreamingBuffer(long estimatedRows, long estimatedBytes, long oldestEntryTime) { + this.estimatedRows = estimatedRows; + this.estimatedBytes = estimatedBytes; + this.oldestEntryTime = oldestEntryTime; + } + + /** + * Returns a lower-bound estimate of the number of rows currently in the streaming buffer. + */ + public long estimatedRows() { + return estimatedRows; + } + + /** + * Returns a lower-bound estimate of the number of bytes currently in the streaming buffer. + */ + public long estimatedBytes() { + return estimatedBytes; + } + + /** + * Returns the timestamp of the oldest entry in the streaming buffer, in milliseconds since + * epoch. + */ + public long oldestEntryTime() { + return oldestEntryTime; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("estimatedRows", estimatedRows) + .add("estimatedBytes", estimatedBytes) + .add("oldestEntryTime", oldestEntryTime) + .toString(); + } + + @Override + public int hashCode() { + return Objects.hash(estimatedRows, estimatedBytes, oldestEntryTime); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof StreamingBuffer + && Objects.equals(toPb(), ((StreamingBuffer) obj).toPb()); + } + + Streamingbuffer toPb() { + return new Streamingbuffer() + .setEstimatedBytes(BigInteger.valueOf(estimatedBytes)) + .setEstimatedRows(BigInteger.valueOf(estimatedRows)) + .setOldestEntryTime(BigInteger.valueOf(oldestEntryTime)); + } + + static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { + return new StreamingBuffer(streamingBufferPb.getEstimatedRows().longValue(), + streamingBufferPb.getEstimatedBytes().longValue(), + streamingBufferPb.getOldestEntryTime().longValue()); + } + } + public static final class Builder extends BaseTableInfo.Builder { private String location; @@ -110,7 +189,7 @@ public static Builder builder(TableId tableId, Schema schema) { } /** - * Creates BigQuery table given its type + * Creates BigQuery table given its type. * * @param tableId table id * @param schema the schema of the table @@ -128,7 +207,7 @@ public Builder toBuilder() { } @Override - protected MoreObjects.ToStringHelper toStringHelper() { + ToStringHelper toStringHelper() { return super.toStringHelper() .add("location", location) .add("streamingBuffer", streamingBuffer); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/UserDefinedFunction.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/UserDefinedFunction.java index 931c1eaf024a..2135e0ddc941 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/UserDefinedFunction.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/UserDefinedFunction.java @@ -13,7 +13,8 @@ * and produces zero or more rows as output. The output can potentially have a different schema than * the input. * - * @see User-Defined Functions + * @see User-Defined Functions + * */ public abstract class UserDefinedFunction implements Serializable { @@ -137,7 +138,7 @@ public static UserDefinedFunction fromUri(String functionDefinition) { return new UriFunction(functionDefinition); } - public static UserDefinedFunction fromPb( + static UserDefinedFunction fromPb( com.google.api.services.bigquery.model.UserDefinedFunctionResource pb) { if (pb.getInlineCode() != null) { return new InlineFunction(pb.getInlineCode()); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java index 01e07663b363..771a7a679c11 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java @@ -20,7 +20,7 @@ import com.google.api.services.bigquery.model.Table; import com.google.api.services.bigquery.model.ViewDefinition; -import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; @@ -76,8 +76,8 @@ public Builder query(String query) { /** * Sets user defined functions that can be used by {@link #query()}. * - * @see User-Defined Functions - * + * @see User-Defined + * Functions */ public Builder userDefinedFunctions(List userDefinedFunctions) { this.userDefinedFunctions = ImmutableList.copyOf(checkNotNull(userDefinedFunctions)); @@ -87,8 +87,8 @@ public Builder userDefinedFunctions(List userDefinedFunctio /** * Sets user defined functions that can be used by {@link #query()}. * - * @see User-Defined Functions - * + * @see User-Defined + * Functions */ public Builder userDefinedFunctions(UserDefinedFunction... userDefinedFunctions) { this.userDefinedFunctions = ImmutableList.copyOf(userDefinedFunctions); @@ -115,7 +115,7 @@ private ViewInfo(Builder builder) { */ public String query() { return query; - }; + } /** * Returns user defined functions that can be used by {@link #query()}. Returns {@code null} if @@ -137,7 +137,7 @@ public Builder toBuilder() { } @Override - protected MoreObjects.ToStringHelper toStringHelper() { + ToStringHelper toStringHelper() { return super.toStringHelper() .add("query", query) .add("userDefinedFunctions", userDefinedFunctions); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java index ffaa787fb1d8..74d1c038a6bc 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/spi/DefaultBigQueryRpc.java @@ -122,8 +122,8 @@ public Tuple > listDatasets(Map