content) {
@@ -241,7 +241,7 @@ public Builder ignoreUnknownValues(boolean ignoreUnknownValues) {
* is called use:
* {@code
* String suffixTableId = ...;
- * BaseTableInfo suffixTable = bigquery.getTable(DATASET, suffixTableId);
+ * TableInfo suffixTable = bigquery.getTable(DATASET, suffixTableId);
* while (suffixTable == null) {
* Thread.sleep(1000L);
* suffixTable = bigquery.getTable(DATASET, suffixTableId);
@@ -307,7 +307,7 @@ public Boolean skipInvalidRows() {
* called use:
* {@code
* String suffixTableId = ...;
- * BaseTableInfo suffixTable = bigquery.getTable(DATASET, suffixTableId);
+ * TableInfo suffixTable = bigquery.getTable(DATASET, suffixTableId);
* while (suffixTable == null) {
* Thread.sleep(1000L);
* suffixTable = bigquery.getTable(DATASET, suffixTableId);
@@ -371,7 +371,7 @@ public static Builder builder(String datasetId, String tableId, RowToInsert... r
* Returns a builder for an {@code InsertAllRequest} object given the destination table and the
* rows to insert.
*/
- public static Builder builder(BaseTableInfo tableInfo, Iterable rows) {
+ public static Builder builder(TableInfo tableInfo, Iterable rows) {
return builder(tableInfo.tableId(), rows);
}
@@ -379,7 +379,7 @@ public static Builder builder(BaseTableInfo tableInfo, Iterable row
* Returns a builder for an {@code InsertAllRequest} object given the destination table and the
* rows to insert.
*/
- public static Builder builder(BaseTableInfo tableInfo, RowToInsert... rows) {
+ public static Builder builder(TableInfo tableInfo, RowToInsert... rows) {
return builder(tableInfo.tableId(), rows);
}
@@ -414,14 +414,14 @@ public static InsertAllRequest of(String datasetId, String tableId, RowToInsert.
/**
* Returns a {@code InsertAllRequest} object given the destination table and the rows to insert.
*/
- public static InsertAllRequest of(BaseTableInfo tableInfo, Iterable rows) {
+ public static InsertAllRequest of(TableInfo tableInfo, Iterable rows) {
return builder(tableInfo.tableId(), rows).build();
}
/**
* Returns a {@code InsertAllRequest} object given the destination table and the rows to insert.
*/
- public static InsertAllRequest of(BaseTableInfo tableInfo, RowToInsert... rows) {
+ public static InsertAllRequest of(TableInfo tableInfo, RowToInsert... rows) {
return builder(tableInfo.tableId(), rows).build();
}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java
index c0d7ddc29c37..1e63344a600d 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Job.java
@@ -18,50 +18,102 @@
import static com.google.common.base.Preconditions.checkNotNull;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.util.Objects;
+
/**
* A Google BigQuery Job.
*
* Objects of this class are immutable. To get a {@code Job} object with the most recent
- * information use {@link #reload}.
+ * information use {@link #reload}. {@code Job} adds a layer of service-related functionality over
+ * {@link JobInfo}.
*
*/
-public final class Job {
+public final class Job extends JobInfo {
- private final BigQuery bigquery;
- private final JobInfo info;
+ private static final long serialVersionUID = -4324100991693024704L;
- /**
- * Constructs a {@code Job} object for the provided {@code JobInfo}. The BigQuery service
- * is used to issue requests.
- *
- * @param bigquery the BigQuery service used for issuing requests
- * @param info jobs's info
- */
- public Job(BigQuery bigquery, JobInfo info) {
- this.bigquery = checkNotNull(bigquery);
- this.info = checkNotNull(info);
- }
+ private final BigQueryOptions options;
+ private transient BigQuery bigquery;
/**
- * Creates a {@code Job} object for the provided job's user-defined id. Performs an RPC call to
- * get the latest job information.
- *
- * @param bigquery the BigQuery service used for issuing requests
- * @param job job's id, either user-defined or picked by the BigQuery service
- * @param options job options
- * @return the {@code Job} object or {@code null} if not found
- * @throws BigQueryException upon failure
+ * A builder for {@code Job} objects.
*/
- public static Job get(BigQuery bigquery, String job, BigQuery.JobOption... options) {
- JobInfo info = bigquery.getJob(job, options);
- return info != null ? new Job(bigquery, info) : null;
+ public static final class Builder extends JobInfo.Builder {
+
+ private final BigQuery bigquery;
+ private final JobInfo.BuilderImpl infoBuilder;
+
+ Builder(BigQuery bigquery, JobConfiguration configuration) {
+ this.bigquery = bigquery;
+ this.infoBuilder = new JobInfo.BuilderImpl();
+ this.infoBuilder.configuration(configuration);
+ }
+
+ Builder(Job job) {
+ this.bigquery = job.bigquery;
+ this.infoBuilder = new JobInfo.BuilderImpl(job);
+ }
+
+ @Override
+ Builder etag(String etag) {
+ infoBuilder.etag(etag);
+ return this;
+ }
+
+ @Override
+ Builder id(String id) {
+ infoBuilder.id(id);
+ return this;
+ }
+
+ @Override
+ public Builder jobId(JobId jobId) {
+ infoBuilder.jobId(jobId);
+ return this;
+ }
+
+ @Override
+ Builder selfLink(String selfLink) {
+ infoBuilder.selfLink(selfLink);
+ return this;
+ }
+
+ @Override
+ Builder status(JobStatus status) {
+ infoBuilder.status(status);
+ return this;
+ }
+
+ @Override
+ Builder statistics(JobStatistics statistics) {
+ infoBuilder.statistics(statistics);
+ return this;
+ }
+
+ @Override
+ Builder userEmail(String userEmail) {
+ infoBuilder.userEmail(userEmail);
+ return this;
+ }
+
+ @Override
+ public Builder configuration(JobConfiguration configuration) {
+ infoBuilder.configuration(configuration);
+ return this;
+ }
+
+ @Override
+ public Job build() {
+ return new Job(bigquery, infoBuilder);
+ }
}
- /**
- * Returns the job's information.
- */
- public JobInfo info() {
- return info;
+ Job(BigQuery bigquery, JobInfo.BuilderImpl infoBuilder) {
+ super(infoBuilder);
+ this.bigquery = checkNotNull(bigquery);
+ this.options = bigquery.options();
}
/**
@@ -71,7 +123,7 @@ public JobInfo info() {
* @throws BigQueryException upon failure
*/
public boolean exists() {
- return bigquery.getJob(info.jobId(), BigQuery.JobOption.fields()) != null;
+ return bigquery.getJob(jobId(), BigQuery.JobOption.fields()) != null;
}
/**
@@ -90,8 +142,7 @@ public boolean exists() {
* @throws BigQueryException upon failure
*/
public boolean isDone() {
- JobInfo job = bigquery.getJob(info.jobId(),
- BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
+ Job job = bigquery.getJob(jobId(), BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
return job != null && job.status().state() == JobStatus.State.DONE;
}
@@ -103,7 +154,7 @@ public boolean isDone() {
* @throws BigQueryException upon failure
*/
public Job reload(BigQuery.JobOption... options) {
- return Job.get(bigquery, info.jobId().job(), options);
+ return bigquery.getJob(jobId().job(), options);
}
/**
@@ -114,7 +165,7 @@ public Job reload(BigQuery.JobOption... options) {
* @throws BigQueryException upon failure
*/
public boolean cancel() {
- return bigquery.cancel(info.jobId());
+ return bigquery.cancel(jobId());
}
/**
@@ -123,4 +174,30 @@ public boolean cancel() {
public BigQuery bigquery() {
return bigquery;
}
+
+ @Override
+ public Builder toBuilder() {
+ return new Builder(this);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Job
+ && Objects.equals(toPb(), ((Job) obj).toPb())
+ && Objects.equals(options, ((Job) obj).options);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), options);
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+ this.bigquery = options.service();
+ }
+
+ static Job fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Job jobPb) {
+ return new Job(bigquery, new JobInfo.BuilderImpl(jobPb));
+ }
}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java
index 47135b6d97d0..1adf7fabafc1 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/JobInfo.java
@@ -32,7 +32,7 @@
*
* @see Jobs
*/
-public final class JobInfo implements Serializable {
+public class JobInfo implements Serializable {
static final Function FROM_PB_FUNCTION =
new Function() {
@@ -41,8 +41,18 @@ public JobInfo apply(Job pb) {
return JobInfo.fromPb(pb);
}
};
+
private static final long serialVersionUID = -3272941007234620265L;
+ private final String etag;
+ private final String id;
+ private final JobId jobId;
+ private final String selfLink;
+ private final JobStatus status;
+ private final JobStatistics statistics;
+ private final String userEmail;
+ private final JobConfiguration configuration;
+
/**
* Specifies whether the job is allowed to create new tables.
*/
@@ -78,16 +88,44 @@ public enum WriteDisposition {
WRITE_EMPTY
}
- private final String etag;
- private final String id;
- private final JobId jobId;
- private final String selfLink;
- private final JobStatus status;
- private final JobStatistics statistics;
- private final String userEmail;
- private final JobConfiguration configuration;
+ /**
+ * A builder for {@code JobInfo} objects.
+ */
+ public abstract static class Builder {
+
+ abstract Builder etag(String etag);
+
+ abstract Builder id(String id);
+
+ /**
+ * Sets the job identity.
+ */
+ public abstract Builder jobId(JobId jobId);
+
+ abstract Builder selfLink(String selfLink);
+
+ abstract Builder status(JobStatus status);
+
+ abstract Builder statistics(JobStatistics statistics);
- public static final class Builder {
+ abstract Builder userEmail(String userEmail);
+
+ /**
+ * Sets a configuration for the {@code JobInfo} object. Use {@link CopyJobConfiguration} for a
+ * job that copies an existing table. Use {@link ExtractJobConfiguration} for a job that exports
+ * a table to Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads data
+ * from Google Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that runs
+ * a query.
+ */
+ public abstract Builder configuration(JobConfiguration configuration);
+
+ /**
+ * Creates a {@code JobInfo} object.
+ */
+ public abstract JobInfo build();
+ }
+
+ static final class BuilderImpl extends Builder {
private String etag;
private String id;
@@ -98,9 +136,9 @@ public static final class Builder {
private String userEmail;
private JobConfiguration configuration;
- private Builder() {}
+ BuilderImpl() {}
- private Builder(JobInfo jobInfo) {
+ BuilderImpl(JobInfo jobInfo) {
this.etag = jobInfo.etag;
this.id = jobInfo.id;
this.jobId = jobInfo.jobId;
@@ -111,7 +149,7 @@ private Builder(JobInfo jobInfo) {
this.configuration = jobInfo.configuration;
}
- protected Builder(Job jobPb) {
+ BuilderImpl(Job jobPb) {
this.etag = jobPb.getEtag();
this.id = jobPb.getId();
if (jobPb.getJobReference() != null) {
@@ -128,55 +166,61 @@ protected Builder(Job jobPb) {
this.configuration = JobConfiguration.fromPb(jobPb.getConfiguration());
}
+ @Override
Builder etag(String etag) {
this.etag = etag;
return this;
}
+ @Override
Builder id(String id) {
this.id = id;
return this;
}
- /**
- * Sets the job identity.
- */
+ @Override
public Builder jobId(JobId jobId) {
this.jobId = jobId;
return this;
}
+ @Override
Builder selfLink(String selfLink) {
this.selfLink = selfLink;
return this;
}
+ @Override
Builder status(JobStatus status) {
this.status = status;
return this;
}
+ @Override
Builder statistics(JobStatistics statistics) {
this.statistics = statistics;
return this;
}
+ @Override
Builder userEmail(String userEmail) {
this.userEmail = userEmail;
return this;
}
+ @Override
public Builder configuration(JobConfiguration configuration) {
this.configuration = configuration;
return this;
}
+ @Override
public JobInfo build() {
return new JobInfo(this);
}
}
- private JobInfo(Builder builder) {
+ JobInfo(BuilderImpl builder) {
this.jobId = builder.jobId;
this.etag = builder.etag;
this.id = builder.id;
@@ -248,10 +292,10 @@ public C configuration() {
}
/**
- * Returns a builder for the job.
+ * Returns a builder for the job object.
*/
public Builder toBuilder() {
- return new Builder(this);
+ return new BuilderImpl(this);
}
@Override
@@ -275,7 +319,9 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
- return obj instanceof JobInfo && Objects.equals(toPb(), ((JobInfo) obj).toPb());
+ return obj != null
+ && obj.getClass().equals(JobInfo.class)
+ && Objects.equals(toPb(), ((JobInfo) obj).toPb());
}
JobInfo setProjectId(String projectId) {
@@ -301,19 +347,40 @@ Job toPb() {
return jobPb;
}
+ /**
+ * Returns a builder for a {@code JobInfo} object given the job configuration. Use
+ * {@link CopyJobConfiguration} for a job that copies an existing table. Use
+ * {@link ExtractJobConfiguration} for a job that exports a table to Google Cloud Storage. Use
+ * {@link LoadJobConfiguration} for a job that loads data from Google Cloud Storage into a table.
+ * Use {@link QueryJobConfiguration} for a job that runs a query.
+ */
public static Builder builder(JobConfiguration configuration) {
- return new Builder().configuration(configuration);
+ return new BuilderImpl().configuration(configuration);
}
+ /**
+ * Returns a {@code JobInfo} object given the job configuration. Use {@link CopyJobConfiguration}
+ * for a job that copies an existing table. Use {@link ExtractJobConfiguration} for a job that
+ * exports a table to Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads
+ * data from Google Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that
+ * runs a query.
+ */
public static JobInfo of(JobConfiguration configuration) {
return builder(configuration).build();
}
+ /**
+ * Returns a builder for a {@code JobInfo} object given the job identity and configuration. Use
+ * {@link CopyJobConfiguration} for a job that copies an existing table. Use
+ * {@link ExtractJobConfiguration} for a job that exports a table to Google Cloud Storage. Use
+ * {@link LoadJobConfiguration} for a job that loads data from Google Cloud Storage into a table.
+ * Use {@link QueryJobConfiguration} for a job that runs a query.
+ */
public static JobInfo of(JobId jobId, JobConfiguration configuration) {
return builder(configuration).jobId(jobId).build();
}
static JobInfo fromPb(Job jobPb) {
- return new Builder(jobPb).build();
+ return new BuilderImpl(jobPb).build();
}
}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/LoadJobConfiguration.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/LoadJobConfiguration.java
index 1f98a3dfaca1..9c9fa7a769b6 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/LoadJobConfiguration.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/LoadJobConfiguration.java
@@ -282,6 +282,7 @@ LoadJobConfiguration setProjectId(String projectId) {
return toBuilder().destinationTable(destinationTable().setProjectId(projectId)).build();
}
+ @Override
com.google.api.services.bigquery.model.JobConfiguration toPb() {
JobConfigurationLoad loadConfigurationPb = new JobConfigurationLoad();
loadConfigurationPb.setDestinationTable(destinationTable.toPb());
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java
index d88820fe5a29..3fdc27ecab99 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Option.java
@@ -19,7 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects;
-import com.google.gcloud.spi.BigQueryRpc;
+import com.google.gcloud.bigquery.spi.BigQueryRpc;
import java.io.Serializable;
import java.util.Objects;
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobConfiguration.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobConfiguration.java
index 630a3d5b9088..688611d07526 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobConfiguration.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobConfiguration.java
@@ -61,7 +61,7 @@ public enum Priority {
private final String query;
private final TableId destinationTable;
- private final Map tableDefinitions;
+ private final Map tableDefinitions;
private final List userDefinedFunctions;
private final CreateDisposition createDisposition;
private final WriteDisposition writeDisposition;
@@ -77,7 +77,7 @@ public static final class Builder
private String query;
private TableId destinationTable;
- private Map tableDefinitions;
+ private Map tableDefinitions;
private List userDefinedFunctions;
private CreateDisposition createDisposition;
private WriteDisposition writeDisposition;
@@ -127,7 +127,7 @@ private Builder(com.google.api.services.bigquery.model.JobConfiguration configur
}
if (queryConfigurationPb.getTableDefinitions() != null) {
tableDefinitions = Maps.transformValues(queryConfigurationPb.getTableDefinitions(),
- ExternalDataConfiguration.FROM_PB_FUNCTION);
+ ExternalTableDefinition.FROM_EXTERNAL_DATA_FUNCTION);
}
if (queryConfigurationPb.getUserDefinedFunctionResources() != null) {
userDefinedFunctions = Lists.transform(
@@ -167,7 +167,7 @@ public Builder destinationTable(TableId destinationTable) {
* sources. By defining these properties, the data sources can be queried as if they were
* standard BigQuery tables.
*/
- public Builder tableDefinitions(Map tableDefinitions) {
+ public Builder tableDefinitions(Map tableDefinitions) {
this.tableDefinitions = tableDefinitions != null ? Maps.newHashMap(tableDefinitions) : null;
return this;
}
@@ -179,7 +179,7 @@ public Builder tableDefinitions(Map tableDefi
* @param tableName name of the table
* @param tableDefinition external data configuration for the table used by this query
*/
- public Builder addTableDefinition(String tableName, ExternalDataConfiguration tableDefinition) {
+ public Builder addTableDefinition(String tableName, ExternalTableDefinition tableDefinition) {
if (this.tableDefinitions == null) {
this.tableDefinitions = Maps.newHashMap();
}
@@ -383,7 +383,7 @@ public String query() {
* sources. By defining these properties, the data sources can be queried as if they were
* standard BigQuery tables.
*/
- public Map tableDefinitions() {
+ public Map tableDefinitions() {
return tableDefinitions;
}
@@ -472,6 +472,7 @@ QueryJobConfiguration setProjectId(String projectId) {
return builder.build();
}
+ @Override
com.google.api.services.bigquery.model.JobConfiguration toPb() {
com.google.api.services.bigquery.model.JobConfiguration configurationPb =
new com.google.api.services.bigquery.model.JobConfiguration();
@@ -497,8 +498,8 @@ com.google.api.services.bigquery.model.JobConfiguration toPb() {
queryConfigurationPb.setPriority(priority.toString());
}
if (tableDefinitions != null) {
- queryConfigurationPb.setTableDefinitions(
- Maps.transformValues(tableDefinitions, ExternalDataConfiguration.TO_PB_FUNCTION));
+ queryConfigurationPb.setTableDefinitions(Maps.transformValues(tableDefinitions,
+ ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
}
if (useQueryCache != null) {
queryConfigurationPb.setUseQueryCache(useQueryCache);
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
index 0bcfb3d4a9ae..b3522a2a6ba3 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryRequest.java
@@ -35,26 +35,26 @@
* {@link QueryResponse#jobCompleted()} returns {@code true}.
*
* 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.jobCompleted()) {
- * Thread.sleep(1000);
- * response = bigquery.getQueryResults(response.jobId());
- * }
- * List 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
- * }
+ * {@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)
+ * .pageSize(1000L)
+ * .build();
+ * QueryResponse response = bigquery.query(request);
+ * while (!response.jobCompleted()) {
+ * Thread.sleep(1000);
+ * response = bigquery.getQueryResults(response.jobId());
+ * }
+ * List 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
+ * }
* }
*
* @see Query
@@ -65,7 +65,7 @@ public class QueryRequest implements Serializable {
private static final long serialVersionUID = -8727328332415880852L;
private final String query;
- private final Long maxResults;
+ private final Long pageSize;
private final DatasetId defaultDataset;
private final Long maxWaitTime;
private final Boolean dryRun;
@@ -74,7 +74,7 @@ public class QueryRequest implements Serializable {
public static final class Builder {
private String query;
- private Long maxResults;
+ private Long pageSize;
private DatasetId defaultDataset;
private Long maxWaitTime;
private Boolean dryRun;
@@ -96,8 +96,8 @@ public Builder query(String query) {
* query result set is large. In addition to this limit, responses are also limited to 10 MB.
* By default, there is no maximum row count, and only the byte limit applies.
*/
- public Builder maxResults(Long maxResults) {
- this.maxResults = maxResults;
+ public Builder pageSize(Long pageSize) {
+ this.pageSize = pageSize;
return this;
}
@@ -157,7 +157,7 @@ public QueryRequest build() {
private QueryRequest(Builder builder) {
query = builder.query;
- maxResults = builder.maxResults;
+ pageSize = builder.pageSize;
defaultDataset = builder.defaultDataset;
maxWaitTime = builder.maxWaitTime;
dryRun = builder.dryRun;
@@ -174,8 +174,8 @@ public String query() {
/**
* Returns the maximum number of rows of data to return per page of results.
*/
- public Long maxResults() {
- return maxResults;
+ public Long pageSize() {
+ return pageSize;
}
/**
@@ -224,7 +224,7 @@ public Boolean useQueryCache() {
public Builder toBuilder() {
return new Builder()
.query(query)
- .maxResults(maxResults)
+ .pageSize(pageSize)
.defaultDataset(defaultDataset)
.maxWaitTime(maxWaitTime)
.dryRun(dryRun)
@@ -235,7 +235,7 @@ public Builder toBuilder() {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("query", query)
- .add("maxResults", maxResults)
+ .add("pageSize", pageSize)
.add("defaultDataset", defaultDataset)
.add("maxWaitTime", maxWaitTime)
.add("dryRun", dryRun)
@@ -245,7 +245,7 @@ public String toString() {
@Override
public int hashCode() {
- return Objects.hash(query, maxResults, defaultDataset, maxWaitTime, dryRun, useQueryCache);
+ return Objects.hash(query, pageSize, defaultDataset, maxWaitTime, dryRun, useQueryCache);
}
@Override
@@ -264,8 +264,8 @@ QueryRequest setProjectId(String projectId) {
com.google.api.services.bigquery.model.QueryRequest toPb() {
com.google.api.services.bigquery.model.QueryRequest queryRequestPb =
new com.google.api.services.bigquery.model.QueryRequest().setQuery(query);
- if (maxResults != null) {
- queryRequestPb.setMaxResults(maxResults);
+ if (pageSize != null) {
+ queryRequestPb.setMaxResults(pageSize);
}
if (defaultDataset != null) {
queryRequestPb.setDefaultDataset(defaultDataset.toPb());
@@ -299,7 +299,7 @@ public static QueryRequest of(String query) {
static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
Builder builder = builder(queryRequestPb.getQuery());
if (queryRequestPb.getMaxResults() != null) {
- builder.maxResults(queryRequestPb.getMaxResults());
+ builder.pageSize(queryRequestPb.getMaxResults());
}
if (queryRequestPb.getDefaultDataset() != null) {
builder.defaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
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 77386747754f..12000cc1cbd2 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
@@ -29,20 +29,20 @@
* Query Request ({@link BigQuery#query(QueryRequest)}).
*
* Example usage of a query response:
- *
{@code
- * QueryResponse response = bigquery.query(request);
- * while (!response.jobCompleted()) {
- * Thread.sleep(1000);
- * response = bigquery.getQueryResults(response.jobId());
- * }
- * List 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
- * }
+ * {@code
+ * QueryResponse response = bigquery.query(request);
+ * while (!response.jobCompleted()) {
+ * Thread.sleep(1000);
+ * response = bigquery.getQueryResults(response.jobId());
+ * }
+ * List 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
+ * }
* }
*
* @see Get Query
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java
new file mode 100644
index 000000000000..d0e49157a99c
--- /dev/null
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java
@@ -0,0 +1,282 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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 default table definition. This definition is used for standard, two-dimensional
+ * tables with individual records organized in rows, and a data type assigned to each column (also
+ * called a field). Individual fields within a record may contain nested and repeated children
+ * fields. Every table is described by a schema that describes field names, types, and other
+ * information.
+ *
+ * @see Managing Tables
+ */
+public class StandardTableDefinition extends TableDefinition {
+
+ private static final long serialVersionUID = 2113445776046717900L;
+
+ private final Long numBytes;
+ private final Long numRows;
+ 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 = 822027055549277843L;
+ 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 TableDefinition.Builder {
+
+ private Long numBytes;
+ private Long numRows;
+ private String location;
+ private StreamingBuffer streamingBuffer;
+
+ private Builder() {
+ super(Type.TABLE);
+ }
+
+ private Builder(StandardTableDefinition tableDefinition) {
+ super(tableDefinition);
+ this.numBytes = tableDefinition.numBytes;
+ this.numRows = tableDefinition.numRows;
+ this.location = tableDefinition.location;
+ this.streamingBuffer = tableDefinition.streamingBuffer;
+ }
+
+ private Builder(Table tablePb) {
+ super(tablePb);
+ if (tablePb.getNumRows() != null) {
+ this.numRows(tablePb.getNumRows().longValue());
+ }
+ this.numBytes = tablePb.getNumBytes();
+ this.location = tablePb.getLocation();
+ if (tablePb.getStreamingBuffer() != null) {
+ this.streamingBuffer = StreamingBuffer.fromPb(tablePb.getStreamingBuffer());
+ }
+ }
+
+ Builder numBytes(Long numBytes) {
+ this.numBytes = numBytes;
+ return self();
+ }
+
+ Builder numRows(Long numRows) {
+ this.numRows = numRows;
+ return self();
+ }
+
+ Builder location(String location) {
+ this.location = location;
+ return self();
+ }
+
+ Builder streamingBuffer(StreamingBuffer streamingBuffer) {
+ this.streamingBuffer = streamingBuffer;
+ return self();
+ }
+
+ /**
+ * Creates a {@code StandardTableDefinition} object.
+ */
+ @Override
+ public StandardTableDefinition build() {
+ return new StandardTableDefinition(this);
+ }
+ }
+
+ private StandardTableDefinition(Builder builder) {
+ super(builder);
+ this.numBytes = builder.numBytes;
+ this.numRows = builder.numRows;
+ this.location = builder.location;
+ this.streamingBuffer = builder.streamingBuffer;
+ }
+
+ /**
+ * Returns the size of this table in bytes, excluding any data in the streaming buffer.
+ */
+ public Long numBytes() {
+ return numBytes;
+ }
+
+ /**
+ * Returns the number of rows in this table, excluding any data in the streaming buffer.
+ */
+ public Long numRows() {
+ return numRows;
+ }
+
+ /**
+ * Returns the geographic location where the table should reside. This value is inherited from the
+ * dataset.
+ *
+ * @see
+ * Dataset Location
+ */
+ public String location() {
+ return location;
+ }
+
+ /**
+ * Returns information on the table's streaming buffer if any exists. Returns {@code null} if no
+ * streaming buffer exists.
+ */
+ public StreamingBuffer streamingBuffer() {
+ return streamingBuffer;
+ }
+
+ /**
+ * Returns a builder for a BigQuery standard table definition.
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Creates a BigQuery standard table definition given its schema.
+ *
+ * @param schema the schema of the table
+ */
+ public static StandardTableDefinition of(Schema schema) {
+ return builder().schema(schema).build();
+ }
+
+ /**
+ * Returns a builder for the {@code StandardTableDefinition} object.
+ */
+ @Override
+ public Builder toBuilder() {
+ return new Builder(this);
+ }
+
+ @Override
+ ToStringHelper toStringHelper() {
+ return super.toStringHelper()
+ .add("numBytes", numBytes)
+ .add("numRows", numRows)
+ .add("location", location)
+ .add("streamingBuffer", streamingBuffer);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof StandardTableDefinition && baseEquals((StandardTableDefinition) obj);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(baseHashCode(), numBytes, numRows, location, streamingBuffer);
+ }
+
+ @Override
+ Table toPb() {
+ Table tablePb = super.toPb();
+ if (numRows != null) {
+ tablePb.setNumRows(BigInteger.valueOf(numRows));
+ }
+ tablePb.setNumBytes(numBytes);
+ tablePb.setLocation(location);
+ if (streamingBuffer != null) {
+ tablePb.setStreamingBuffer(streamingBuffer.toPb());
+ }
+ return tablePb;
+ }
+
+ @SuppressWarnings("unchecked")
+ static StandardTableDefinition fromPb(Table tablePb) {
+ return new Builder(tablePb).build();
+ }
+}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java
index 1344b31c9b68..3f902d2ff242 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Table.java
@@ -16,12 +16,13 @@
package com.google.gcloud.bigquery;
-import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.google.gcloud.Page;
+import java.io.IOException;
+import java.io.ObjectInputStream;
import java.util.List;
import java.util.Objects;
@@ -30,62 +31,106 @@
*
* Objects of this class are immutable. Operations that modify the table like {@link #update}
* return a new object. To get a {@code Table} object with the most recent information use
- * {@link #reload}.
+ * {@link #reload}. {@code Table} adds a layer of service-related functionality over
+ * {@link TableInfo}.
*
*/
-public final class Table {
+public final class Table extends TableInfo {
- private final BigQuery bigquery;
- private final BaseTableInfo info;
+ private static final long serialVersionUID = 5744556727066570096L;
- /**
- * Constructs a {@code Table} object for the provided {@code TableInfo}. The BigQuery service
- * is used to issue requests.
- *
- * @param bigquery the BigQuery service used for issuing requests
- * @param info table's info
- */
- public Table(BigQuery bigquery, BaseTableInfo info) {
- this.bigquery = checkNotNull(bigquery);
- this.info = checkNotNull(info);
- }
+ private final BigQueryOptions options;
+ private transient BigQuery bigquery;
/**
- * Creates a {@code Table} object for the provided dataset and table's user-defined ids. Performs
- * an RPC call to get the latest table information.
- *
- * @param bigquery the BigQuery service used for issuing requests
- * @param dataset the dataset's user-defined id
- * @param table the table's user-defined id
- * @param options table options
- * @return the {@code Table} object or {@code null} if not found
- * @throws BigQueryException upon failure
+ * A builder for {@code Table} objects.
*/
- public static Table get(BigQuery bigquery, String dataset, String table,
- BigQuery.TableOption... options) {
- return get(bigquery, TableId.of(dataset, table), options);
- }
+ public static class Builder extends TableInfo.Builder {
- /**
- * Creates a {@code Table} object for the provided table identity. Performs an RPC call to get the
- * latest table information.
- *
- * @param bigquery the BigQuery service used for issuing requests
- * @param table the table's identity
- * @param options table options
- * @return the {@code Table} object or {@code null} if not found
- * @throws BigQueryException upon failure
- */
- public static Table get(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
- BaseTableInfo info = bigquery.getTable(table, options);
- return info != null ? new Table(bigquery, info) : null;
+ private final BigQuery bigquery;
+ private final TableInfo.BuilderImpl infoBuilder;
+
+ Builder(BigQuery bigquery, TableId tableId, TableDefinition defintion) {
+ this.bigquery = bigquery;
+ this.infoBuilder = new TableInfo.BuilderImpl();
+ this.infoBuilder.tableId(tableId).definition(defintion);
+ }
+
+ Builder(Table table) {
+ this.bigquery = table.bigquery;
+ this.infoBuilder = new TableInfo.BuilderImpl(table);
+ }
+
+ @Override
+ Builder creationTime(Long creationTime) {
+ infoBuilder.creationTime(creationTime);
+ return this;
+ }
+
+ @Override
+ public Builder description(String description) {
+ infoBuilder.description(description);
+ return this;
+ }
+
+ @Override
+ Builder etag(String etag) {
+ infoBuilder.etag(etag);
+ return this;
+ }
+
+ @Override
+ public Builder expirationTime(Long expirationTime) {
+ infoBuilder.expirationTime(expirationTime);
+ return this;
+ }
+
+ @Override
+ public Builder friendlyName(String friendlyName) {
+ infoBuilder.friendlyName(friendlyName);
+ return this;
+ }
+
+ @Override
+ Builder id(String id) {
+ infoBuilder.id(id);
+ return this;
+ }
+
+ @Override
+ Builder lastModifiedTime(Long lastModifiedTime) {
+ infoBuilder.lastModifiedTime(lastModifiedTime);
+ return this;
+ }
+
+ @Override
+ Builder selfLink(String selfLink) {
+ infoBuilder.selfLink(selfLink);
+ return this;
+ }
+
+ @Override
+ public Builder tableId(TableId tableId) {
+ infoBuilder.tableId(tableId);
+ return this;
+ }
+
+ @Override
+ public Builder definition(TableDefinition definition) {
+ infoBuilder.definition(definition);
+ return this;
+ }
+
+ @Override
+ public Table build() {
+ return new Table(bigquery, infoBuilder);
+ }
}
- /**
- * Returns the table's information.
- */
- public BaseTableInfo info() {
- return info;
+ Table(BigQuery bigquery, TableInfo.BuilderImpl infoBuilder) {
+ super(infoBuilder);
+ this.bigquery = checkNotNull(bigquery);
+ this.options = bigquery.options();
}
/**
@@ -95,7 +140,7 @@ public BaseTableInfo info() {
* @throws BigQueryException upon failure
*/
public boolean exists() {
- return bigquery.getTable(info.tableId(), BigQuery.TableOption.fields()) != null;
+ return bigquery.getTable(tableId(), BigQuery.TableOption.fields()) != null;
}
/**
@@ -106,25 +151,19 @@ public boolean exists() {
* @throws BigQueryException upon failure
*/
public Table reload(BigQuery.TableOption... options) {
- return Table.get(bigquery, info.tableId(), options);
+ return bigquery.getTable(tableId(), options);
}
/**
- * Updates the table's information. Dataset's and table's user-defined ids cannot be changed. A
- * new {@code Table} object is returned.
+ * Updates the table's information with this table's information. Dataset's and table's
+ * user-defined ids cannot be changed. A new {@code Table} object is returned.
*
- * @param tableInfo new table's information. Dataset's and table's user-defined ids must match the
- * ones of the current table
* @param options dataset options
* @return a {@code Table} object with updated information
* @throws BigQueryException upon failure
*/
- public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
- checkArgument(Objects.equals(tableInfo.tableId().dataset(),
- info.tableId().dataset()), "Dataset's user-defined ids must match");
- checkArgument(Objects.equals(tableInfo.tableId().table(),
- info.tableId().table()), "Table's user-defined ids must match");
- return new Table(bigquery, bigquery.update(tableInfo, options));
+ public Table update(BigQuery.TableOption... options) {
+ return bigquery.update(this, options);
}
/**
@@ -134,7 +173,7 @@ public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
* @throws BigQueryException upon failure
*/
public boolean delete() {
- return bigquery.delete(info.tableId());
+ return bigquery.delete(tableId());
}
/**
@@ -143,8 +182,9 @@ public boolean delete() {
* @param rows rows to be inserted
* @throws BigQueryException upon failure
*/
- InsertAllResponse insert(Iterable rows) throws BigQueryException {
- return bigquery.insertAll(InsertAllRequest.of(info.tableId(), rows));
+ public InsertAllResponse insert(Iterable rows)
+ throws BigQueryException {
+ return bigquery.insertAll(InsertAllRequest.of(tableId(), rows));
}
/**
@@ -158,9 +198,9 @@ InsertAllResponse insert(Iterable rows) throws Big
* to be invalid
* @throws BigQueryException upon failure
*/
- InsertAllResponse insert(Iterable rows, boolean skipInvalidRows,
- boolean ignoreUnknownValues) throws BigQueryException {
- InsertAllRequest request = InsertAllRequest.builder(info.tableId(), rows)
+ public InsertAllResponse insert(Iterable rows,
+ boolean skipInvalidRows, boolean ignoreUnknownValues) throws BigQueryException {
+ InsertAllRequest request = InsertAllRequest.builder(tableId(), rows)
.skipInvalidRows(skipInvalidRows)
.ignoreUnknownValues(ignoreUnknownValues)
.build();
@@ -173,8 +213,9 @@ InsertAllResponse insert(Iterable rows, boolean sk
* @param options table data list options
* @throws BigQueryException upon failure
*/
- Page> list(BigQuery.TableDataListOption... options) throws BigQueryException {
- return bigquery.listTableData(info.tableId(), options);
+ public Page> list(BigQuery.TableDataListOption... options)
+ throws BigQueryException {
+ return bigquery.listTableData(tableId(), options);
}
/**
@@ -186,7 +227,7 @@ Page> list(BigQuery.TableDataListOption... options) throws BigQ
* @param options job options
* @throws BigQueryException upon failure
*/
- Job copy(String destinationDataset, String destinationTable, BigQuery.JobOption... options)
+ public Job copy(String destinationDataset, String destinationTable, BigQuery.JobOption... options)
throws BigQueryException {
return copy(TableId.of(destinationDataset, destinationTable), options);
}
@@ -199,9 +240,10 @@ Job copy(String destinationDataset, String destinationTable, BigQuery.JobOption.
* @param options job options
* @throws BigQueryException upon failure
*/
- Job copy(TableId destinationTable, BigQuery.JobOption... options) throws BigQueryException {
- CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, info.tableId());
- return new Job(bigquery, bigquery.create(JobInfo.of(configuration), options));
+ public Job copy(TableId destinationTable, BigQuery.JobOption... options)
+ throws BigQueryException {
+ CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, tableId());
+ return bigquery.create(JobInfo.of(configuration), options);
}
/**
@@ -214,7 +256,7 @@ Job copy(TableId destinationTable, BigQuery.JobOption... options) throws BigQuer
* @param options job options
* @throws BigQueryException upon failure
*/
- Job extract(String format, String destinationUri, BigQuery.JobOption... options)
+ public Job extract(String format, String destinationUri, BigQuery.JobOption... options)
throws BigQueryException {
return extract(format, ImmutableList.of(destinationUri), options);
}
@@ -229,11 +271,11 @@ Job extract(String format, String destinationUri, BigQuery.JobOption... options)
* @param options job options
* @throws BigQueryException upon failure
*/
- Job extract(String format, List destinationUris, BigQuery.JobOption... options)
+ public Job extract(String format, List destinationUris, BigQuery.JobOption... options)
throws BigQueryException {
ExtractJobConfiguration extractConfiguration =
- ExtractJobConfiguration.of(info.tableId(), destinationUris, format);
- return new Job(bigquery, bigquery.create(JobInfo.of(extractConfiguration), options));
+ ExtractJobConfiguration.of(tableId(), destinationUris, format);
+ return bigquery.create(JobInfo.of(extractConfiguration), options);
}
/**
@@ -246,7 +288,7 @@ Job extract(String format, List destinationUris, BigQuery.JobOption... o
* @param options job options
* @throws BigQueryException upon failure
*/
- Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options)
+ public Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options)
throws BigQueryException {
return load(format, ImmutableList.of(sourceUri), options);
}
@@ -261,10 +303,10 @@ Job load(FormatOptions format, String sourceUri, BigQuery.JobOption... options)
* @param options job options
* @throws BigQueryException upon failure
*/
- Job load(FormatOptions format, List sourceUris, BigQuery.JobOption... options)
+ public Job load(FormatOptions format, List sourceUris, BigQuery.JobOption... options)
throws BigQueryException {
- LoadJobConfiguration loadConfig = LoadJobConfiguration.of(info.tableId(), sourceUris, format);
- return new Job(bigquery, bigquery.create(JobInfo.of(loadConfig), options));
+ LoadJobConfiguration loadConfig = LoadJobConfiguration.of(tableId(), sourceUris, format);
+ return bigquery.create(JobInfo.of(loadConfig), options);
}
/**
@@ -273,4 +315,30 @@ Job load(FormatOptions format, List sourceUris, BigQuery.JobOption... op
public BigQuery bigquery() {
return bigquery;
}
+
+ @Override
+ public Builder toBuilder() {
+ return new Builder(this);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Table
+ && Objects.equals(toPb(), ((Table) obj).toPb())
+ && Objects.equals(options, ((Table) obj).options);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), options);
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+ this.bigquery = options.service();
+ }
+
+ static Table fromPb(BigQuery bigquery, com.google.api.services.bigquery.model.Table tablePb) {
+ return new Table(bigquery, new TableInfo.BuilderImpl(tablePb));
+ }
}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDataWriteChannel.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDataWriteChannel.java
index bee0340a29a8..9c6a950ca27f 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDataWriteChannel.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDataWriteChannel.java
@@ -53,6 +53,7 @@ public void run() {
}
}
+ @Override
protected StateImpl.Builder stateBuilder() {
return StateImpl.builder(options(), entity(), uploadId());
}
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java
new file mode 100644
index 000000000000..26e7bcc76f55
--- /dev/null
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.gcloud.bigquery;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.api.services.bigquery.model.Table;
+import com.google.common.base.MoreObjects;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * Base class for a Google BigQuery table definition.
+ */
+public abstract class TableDefinition implements Serializable {
+
+ private static final long serialVersionUID = -374760330662959529L;
+
+ private final Type type;
+ private final Schema schema;
+
+ /**
+ * The table type.
+ */
+ public enum Type {
+ /**
+ * A normal BigQuery table. Instances of {@code TableDefinition} for this type are implemented
+ * by {@link StandardTableDefinition}.
+ */
+ TABLE,
+
+ /**
+ * A virtual table defined by a SQL query. Instances of {@code TableDefinition} for this type
+ * are implemented by {@link ViewDefinition}.
+ *
+ * @see Views
+ */
+ VIEW,
+
+ /**
+ * A BigQuery table backed by external data. Instances of {@code TableDefinition} for this type
+ * are implemented by {@link ExternalTableDefinition}.
+ *
+ * @see Federated Data
+ * Sources
+ */
+ EXTERNAL
+ }
+
+ /**
+ * Base builder for table definitions.
+ *
+ * @param the table definition class
+ * @param the table definition builder
+ */
+ public abstract static class Builder> {
+
+ private Type type;
+ private Schema schema;
+
+ Builder(Type type) {
+ this.type = type;
+ }
+
+ Builder(TableDefinition tableDefinition) {
+ this.type = tableDefinition.type;
+ this.schema = tableDefinition.schema;
+ }
+
+ Builder(Table tablePb) {
+ this.type = Type.valueOf(tablePb.getType());
+ if (tablePb.getSchema() != null) {
+ this.schema(Schema.fromPb(tablePb.getSchema()));
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ B self() {
+ return (B) this;
+ }
+
+ B type(Type type) {
+ this.type = type;
+ return self();
+ }
+
+ /**
+ * Sets the table schema.
+ */
+ public B schema(Schema schema) {
+ this.schema = checkNotNull(schema);
+ return self();
+ }
+
+ /**
+ * Creates an object.
+ */
+ public abstract T build();
+ }
+
+ TableDefinition(Builder builder) {
+ this.type = builder.type;
+ this.schema = builder.schema;
+ }
+
+ /**
+ * Returns the table's type. If this table is simple table the method returns {@link Type#TABLE}.
+ * If this table is an external table this method returns {@link Type#EXTERNAL}. If this table is
+ * a view table this method returns {@link Type#VIEW}.
+ */
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Returns the table's schema.
+ */
+ public Schema schema() {
+ return schema;
+ }
+
+ /**
+ * Returns a builder for the object.
+ */
+ public abstract Builder toBuilder();
+
+ MoreObjects.ToStringHelper toStringHelper() {
+ return MoreObjects.toStringHelper(this).add("type", type).add("schema", schema);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper().toString();
+ }
+
+ final int baseHashCode() {
+ return Objects.hash(type);
+ }
+
+ final boolean baseEquals(TableDefinition tableDefinition) {
+ return Objects.equals(toPb(), tableDefinition.toPb());
+ }
+
+ Table toPb() {
+ Table tablePb = new Table();
+ if (schema != null) {
+ tablePb.setSchema(schema.toPb());
+ }
+ tablePb.setType(type.name());
+ return tablePb;
+ }
+
+ @SuppressWarnings("unchecked")
+ static