diff --git a/README.md b/README.md index df68cd18005d..67fbc4c8e337 100644 --- a/README.md +++ b/README.md @@ -125,23 +125,25 @@ Here is a code snippet showing a simple usage example from within Compute/App En must [supply credentials](#authentication) and a project ID if running this snippet elsewhere. ```java -import com.google.gcloud.bigquery.BaseTableInfo; import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.JobStatus; import com.google.gcloud.bigquery.JobInfo; +import com.google.gcloud.bigquery.LoadJobConfiguration; import com.google.gcloud.bigquery.Schema; +import com.google.gcloud.bigquery.StandardTableDefinition; import com.google.gcloud.bigquery.TableId; import com.google.gcloud.bigquery.TableInfo; BigQuery bigquery = BigQueryOptions.defaultInstance().service(); TableId tableId = TableId.of("dataset", "table"); -BaseTableInfo info = bigquery.getTable(tableId); +TableInfo info = bigquery.getTable(tableId); if (info == null) { System.out.println("Creating table " + tableId); Field integerField = Field.of("fieldName", Field.Type.integer()); - bigquery.create(TableInfo.of(tableId, Schema.of(integerField))); + Schema schema = Schema.of(integerField); + bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema))); } else { System.out.println("Loading data into table " + tableId); LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path"); diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md index eb347dfa0063..3f3678f41a04 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -111,9 +111,9 @@ are created from a BigQuery SQL query. In this code snippet we show how to creat with only one string field. Add the following imports at the top of your file: ```java -import com.google.gcloud.bigquery.BaseTableInfo; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.Schema; +import com.google.gcloud.bigquery.StandardTableDefinition; import com.google.gcloud.bigquery.TableId; import com.google.gcloud.bigquery.TableInfo; ``` @@ -126,7 +126,8 @@ Field stringField = Field.of("StringField", Field.Type.string()); // Table schema definition Schema schema = Schema.of(stringField); // Create a table -TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema)); +StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema); +TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); ``` #### Loading data into a table @@ -204,7 +205,6 @@ the code from the main method to your application's servlet class and change the display on your webpage. ```java -import com.google.gcloud.bigquery.BaseTableInfo; import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.DatasetInfo; @@ -215,6 +215,7 @@ import com.google.gcloud.bigquery.InsertAllResponse; import com.google.gcloud.bigquery.QueryRequest; import com.google.gcloud.bigquery.QueryResponse; import com.google.gcloud.bigquery.Schema; +import com.google.gcloud.bigquery.StandardTableDefinition; import com.google.gcloud.bigquery.TableId; import com.google.gcloud.bigquery.TableInfo; @@ -240,7 +241,8 @@ public class GcloudBigQueryExample { // Table schema definition Schema schema = Schema.of(stringField); // Create a table - TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema)); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); // Define rows to insert Map firstRow = new HashMap<>(); @@ -267,7 +269,7 @@ public class GcloudBigQueryExample { .build(); // Request query to be executed and wait for results QueryResponse queryResponse = bigquery.query(queryRequest); - while (!queryResponse.jobComplete()) { + while (!queryResponse.jobCompleted()) { Thread.sleep(1000L); queryResponse = bigquery.getQueryResults(queryResponse.jobId()); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableInfo.java deleted file mode 100644 index 8bb30f025c06..000000000000 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableInfo.java +++ /dev/null @@ -1,439 +0,0 @@ -/* - * Copyright 2015 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.MoreObjects.firstNonNull; -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.api.client.util.Data; -import com.google.api.services.bigquery.model.Table; -import com.google.common.base.Function; -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; - -/** - * Base class for Google BigQuery table information. Use {@link TableInfo} for a simple BigQuery - * Table. Use {@link ViewInfo} for a BigQuery View Table. Use {@link ExternalTableInfo} for a - * BigQuery Table backed by external data. - * - * @see Managing Tables - */ -public abstract class BaseTableInfo implements Serializable { - - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public BaseTableInfo apply(Table pb) { - return BaseTableInfo.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public Table apply(BaseTableInfo tableInfo) { - return tableInfo.toPb(); - } - }; - - private static final long serialVersionUID = -7679032506430816205L; - - /** - * The table type. - */ - public enum Type { - /** - * A normal BigQuery table. - */ - TABLE, - - /** - * A virtual table defined by a SQL query. - * - * @see Views - */ - VIEW, - - /** - * A BigQuery table backed by external data. - * - * @see Federated Data - * Sources - */ - EXTERNAL - } - - private final String etag; - private final String id; - private final String selfLink; - private final TableId tableId; - private final Type type; - private final Schema schema; - private final String friendlyName; - private final String description; - private final Long numBytes; - private final Long numRows; - private final Long creationTime; - private final Long expirationTime; - private final Long lastModifiedTime; - - /** - * Base builder for tables. - * - * @param the table type - * @param the table builder - */ - public abstract static class Builder> { - - private String etag; - private String id; - private String selfLink; - private TableId tableId; - private Type type; - private Schema schema; - private String friendlyName; - private String description; - private Long numBytes; - private Long numRows; - private Long creationTime; - private Long expirationTime; - private Long lastModifiedTime; - - protected Builder() {} - - protected Builder(BaseTableInfo tableInfo) { - this.etag = tableInfo.etag; - this.id = tableInfo.id; - this.selfLink = tableInfo.selfLink; - this.tableId = tableInfo.tableId; - this.type = tableInfo.type; - this.schema = tableInfo.schema; - this.friendlyName = tableInfo.friendlyName; - this.description = tableInfo.description; - this.numBytes = tableInfo.numBytes; - this.numRows = tableInfo.numRows; - this.creationTime = tableInfo.creationTime; - this.expirationTime = tableInfo.expirationTime; - this.lastModifiedTime = tableInfo.lastModifiedTime; - } - - protected Builder(Table tablePb) { - this.type = Type.valueOf(tablePb.getType()); - this.tableId = TableId.fromPb(tablePb.getTableReference()); - if (tablePb.getSchema() != null) { - this.schema(Schema.fromPb(tablePb.getSchema())); - } - if (tablePb.getLastModifiedTime() != null) { - this.lastModifiedTime(tablePb.getLastModifiedTime().longValue()); - } - if (tablePb.getNumRows() != null) { - this.numRows(tablePb.getNumRows().longValue()); - } - this.description = tablePb.getDescription(); - this.expirationTime = tablePb.getExpirationTime(); - this.friendlyName = tablePb.getFriendlyName(); - this.creationTime = tablePb.getCreationTime(); - this.etag = tablePb.getEtag(); - this.id = tablePb.getId(); - this.numBytes = tablePb.getNumBytes(); - this.selfLink = tablePb.getSelfLink(); - } - - @SuppressWarnings("unchecked") - protected B self() { - return (B) this; - } - - B creationTime(Long creationTime) { - this.creationTime = creationTime; - return self(); - } - - /** - * Sets a user-friendly description for the table. - */ - public B description(String description) { - this.description = firstNonNull(description, Data.nullOf(String.class)); - return self(); - } - - B etag(String etag) { - this.etag = etag; - return self(); - } - - /** - * Sets the time when this table expires, in milliseconds since the epoch. If not present, the - * table will persist indefinitely. Expired tables will be deleted and their storage reclaimed. - */ - public B expirationTime(Long expirationTime) { - this.expirationTime = firstNonNull(expirationTime, Data.nullOf(Long.class)); - return self(); - } - - /** - * Sets a user-friendly name for the table. - */ - public B friendlyName(String friendlyName) { - this.friendlyName = firstNonNull(friendlyName, Data.nullOf(String.class)); - return self(); - } - - B id(String id) { - this.id = id; - return self(); - } - - B lastModifiedTime(Long lastModifiedTime) { - this.lastModifiedTime = lastModifiedTime; - return self(); - } - - B numBytes(Long numBytes) { - this.numBytes = numBytes; - return self(); - } - - B numRows(Long numRows) { - this.numRows = numRows; - return self(); - } - - B selfLink(String selfLink) { - this.selfLink = selfLink; - return self(); - } - - /** - * Sets the table identity. - */ - public B tableId(TableId tableId) { - this.tableId = checkNotNull(tableId); - return self(); - } - - 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(); - } - - protected BaseTableInfo(Builder builder) { - this.tableId = checkNotNull(builder.tableId); - this.etag = builder.etag; - this.id = builder.id; - this.selfLink = builder.selfLink; - this.friendlyName = builder.friendlyName; - this.description = builder.description; - this.type = builder.type; - this.schema = builder.schema; - this.numBytes = builder.numBytes; - this.numRows = builder.numRows; - this.creationTime = builder.creationTime; - this.expirationTime = builder.expirationTime; - this.lastModifiedTime = builder.lastModifiedTime; - } - - /** - * Returns the hash of the table resource. - */ - public String etag() { - return etag; - } - - /** - * Returns an opaque id for the table. - */ - public String id() { - return id; - } - - /** - * 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 an URL that can be used to access the resource again. The returned URL can be used for - * get or update requests. - */ - public String selfLink() { - return selfLink; - } - - /** - * Returns the table identity. - */ - public TableId tableId() { - return tableId; - } - - /** - * Returns a user-friendly name for the table. - */ - public String friendlyName() { - return Data.isNull(friendlyName) ? null : friendlyName; - } - - /** - * Returns a user-friendly description for the table. - */ - public String description() { - return Data.isNull(description) ? null : description; - } - - /** - * 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 time when this table was created, in milliseconds since the epoch. - */ - public Long creationTime() { - return creationTime; - } - - /** - * Returns the time when this table expires, in milliseconds since the epoch. If not present, the - * table will persist indefinitely. Expired tables will be deleted and their storage reclaimed. - */ - public Long expirationTime() { - return Data.isNull(expirationTime) ? null : expirationTime; - } - - /** - * Returns the time when this table was last modified, in milliseconds since the epoch. - */ - public Long lastModifiedTime() { - return lastModifiedTime; - } - - /** - * Returns a builder for the object. - */ - public abstract Builder toBuilder(); - - ToStringHelper toStringHelper() { - return MoreObjects.toStringHelper(this) - .add("tableId", tableId) - .add("type", type) - .add("schema", schema) - .add("etag", etag) - .add("id", id) - .add("selfLink", selfLink) - .add("friendlyName", friendlyName) - .add("description", description) - .add("numBytes", numBytes) - .add("numRows", numRows) - .add("expirationTime", expirationTime) - .add("creationTime", creationTime) - .add("lastModifiedTime", lastModifiedTime); - } - - @Override - public String toString() { - return toStringHelper().toString(); - } - - protected final int baseHashCode() { - return Objects.hash(tableId); - } - - protected final boolean baseEquals(BaseTableInfo tableInfo) { - return Objects.equals(toPb(), tableInfo.toPb()); - } - - BaseTableInfo setProjectId(String projectId) { - return toBuilder().tableId(tableId().setProjectId(projectId)).build(); - } - - Table toPb() { - Table tablePb = new Table(); - tablePb.setTableReference(tableId.toPb()); - if (lastModifiedTime != null) { - tablePb.setLastModifiedTime(BigInteger.valueOf(lastModifiedTime)); - } - if (numRows != null) { - tablePb.setNumRows(BigInteger.valueOf(numRows)); - } - if (schema != null) { - tablePb.setSchema(schema.toPb()); - } - tablePb.setType(type.name()); - tablePb.setCreationTime(creationTime); - tablePb.setDescription(description); - tablePb.setEtag(etag); - tablePb.setExpirationTime(expirationTime); - tablePb.setFriendlyName(friendlyName); - tablePb.setId(id); - tablePb.setNumBytes(numBytes); - tablePb.setSelfLink(selfLink); - return tablePb; - } - - @SuppressWarnings("unchecked") - static T fromPb(Table tablePb) { - switch (Type.valueOf(tablePb.getType())) { - case TABLE: - return (T) TableInfo.fromPb(tablePb); - case VIEW: - return (T) ViewInfo.fromPb(tablePb); - case EXTERNAL: - return (T) ExternalTableInfo.fromPb(tablePb); - default: - // never reached - throw new IllegalArgumentException("Format " + tablePb.getType() + " is not supported"); - } - } -} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 6bc6a2ebabb5..a1b23aba4d5d 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java @@ -275,8 +275,8 @@ private TableOption(BigQueryRpc.Option option, Object value) { /** * Returns an option to specify the table's fields to be returned by the RPC call. If this * option is not provided all table's fields are returned. {@code TableOption.fields} can be - * used to specify only the fields of interest. {@link BaseTableInfo#tableId()} and - * {@link BaseTableInfo#type()} are always returned, even if not specified. + * used to specify only the fields of interest. {@link TableInfo#tableId()} and type (which is + * part of {@link TableInfo#definition()}) are always returned, even if not specified. */ public static TableOption fields(TableField... fields) { return new TableOption(BigQueryRpc.Option.FIELDS, TableField.selector(fields)); @@ -464,7 +464,7 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - T create(T table, TableOption... options) throws BigQueryException; + TableInfo create(TableInfo table, TableOption... options) throws BigQueryException; /** * Creates a new job. @@ -542,14 +542,14 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) { * * @throws BigQueryException upon failure */ - T update(T table, TableOption... options) throws BigQueryException; + TableInfo update(TableInfo table, TableOption... options) throws BigQueryException; /** * Returns the requested table or {@code null} if not found. * * @throws BigQueryException upon failure */ - T getTable(String datasetId, String tableId, TableOption... options) + TableInfo getTable(String datasetId, String tableId, TableOption... options) throws BigQueryException; /** @@ -557,31 +557,31 @@ T getTable(String datasetId, String tableId, TableOpti * * @throws BigQueryException upon failure */ - T getTable(TableId tableId, TableOption... options) + TableInfo getTable(TableId tableId, TableOption... options) throws BigQueryException; /** * Lists the tables in the dataset. This method returns partial information on each table - * ({@link BaseTableInfo#tableId()}, {@link BaseTableInfo#friendlyName()}, - * {@link BaseTableInfo#id()} and {@link BaseTableInfo#type()}). To get complete information use - * either {@link #getTable(TableId, TableOption...)} or + * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and + * type, which is part of {@link TableInfo#definition()}). To get complete information use either + * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * * @throws BigQueryException upon failure */ - Page listTables(String datasetId, TableListOption... options) + Page listTables(String datasetId, TableListOption... options) throws BigQueryException; /** * Lists the tables in the dataset. This method returns partial information on each table - * ({@link BaseTableInfo#tableId()}, {@link BaseTableInfo#friendlyName()}, - * {@link BaseTableInfo#id()} and {@link BaseTableInfo#type()}). To get complete information use - * either {@link #getTable(TableId, TableOption...)} or + * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and + * type, which is part of {@link TableInfo#definition()}). To get complete information use either + * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * * @throws BigQueryException upon failure */ - Page listTables(DatasetId datasetId, TableListOption... options) + Page listTables(DatasetId datasetId, TableListOption... options) throws BigQueryException; /** diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java index e521228d73bb..de74bdcac89c 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java @@ -65,7 +65,7 @@ public Page nextPage() { } } - private static class TablePageFetcher implements NextPageFetcher { + private static class TablePageFetcher implements NextPageFetcher { private static final long serialVersionUID = 8611248840504201187L; private final Map requestOptions; @@ -81,7 +81,7 @@ private static class TablePageFetcher implements NextPageFetcher } @Override - public Page nextPage() { + public Page nextPage() { return listTables(dataset, serviceOptions, requestOptions); } } @@ -173,12 +173,12 @@ public Dataset call() { } @Override - public T create(T table, TableOption... options) + public TableInfo create(TableInfo table, TableOption... options) throws BigQueryException { final Table tablePb = table.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); try { - return BaseTableInfo.fromPb(runWithRetries(new Callable() { + return TableInfo.fromPb(runWithRetries(new Callable
() { @Override public Table call() { return bigQueryRpc.create(tablePb, optionsMap); @@ -309,12 +309,12 @@ public Dataset call() { } @Override - public T update(T table, TableOption... options) + public TableInfo update(TableInfo table, TableOption... options) throws BigQueryException { final Table tablePb = table.setProjectId(options().projectId()).toPb(); final Map optionsMap = optionMap(options); try { - return BaseTableInfo.fromPb(runWithRetries(new Callable
() { + return TableInfo.fromPb(runWithRetries(new Callable
() { @Override public Table call() { return bigQueryRpc.patch(tablePb, optionsMap); @@ -326,13 +326,13 @@ public Table call() { } @Override - public T getTable(final String datasetId, final String tableId, + public TableInfo getTable(final String datasetId, final String tableId, TableOption... options) throws BigQueryException { return getTable(TableId.of(datasetId, tableId), options); } @Override - public T getTable(final TableId tableId, TableOption... options) + public TableInfo getTable(final TableId tableId, TableOption... options) throws BigQueryException { final Map optionsMap = optionMap(options); try { @@ -342,25 +342,25 @@ public Table call() { return bigQueryRpc.getTable(tableId.dataset(), tableId.table(), optionsMap); } }, options().retryParams(), EXCEPTION_HANDLER); - return answer == null ? null : BaseTableInfo.fromPb(answer); + return answer == null ? null : TableInfo.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } } @Override - public Page listTables(String datasetId, TableListOption... options) + public Page listTables(String datasetId, TableListOption... options) throws BigQueryException { return listTables(datasetId, options(), optionMap(options)); } @Override - public Page listTables(DatasetId datasetId, TableListOption... options) + public Page listTables(DatasetId datasetId, TableListOption... options) throws BigQueryException { return listTables(datasetId.dataset(), options(), optionMap(options)); } - private static Page listTables(final String datasetId, final BigQueryOptions + private static Page listTables(final String datasetId, final BigQueryOptions serviceOptions, final Map optionsMap) { try { BigQueryRpc.Tuple> result = @@ -371,8 +371,8 @@ public BigQueryRpc.Tuple> call() { } }, serviceOptions.retryParams(), EXCEPTION_HANDLER); String cursor = result.x(); - Iterable tables = Iterables.transform(result.y(), - BaseTableInfo.FROM_PB_FUNCTION); + Iterable tables = Iterables.transform(result.y(), + TableInfo.FROM_PB_FUNCTION); return new PageImpl<>(new TablePageFetcher(datasetId, serviceOptions, cursor, optionsMap), cursor, tables); } catch (RetryHelper.RetryHelperException e) { diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java index 274ef5678a8a..9576e7d75640 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java @@ -144,7 +144,7 @@ private CsvOptions(Builder builder) { * Returns whether BigQuery should accept rows that are missing trailing optional columns. If * {@code true}, BigQuery treats missing trailing columns as null values. If {@code false}, * records with missing trailing columns are treated as bad records, and if the number of bad - * records exceeds {@link ExternalDataConfiguration#maxBadRecords()}, an invalid error is returned + * records exceeds {@link ExternalTableDefinition#maxBadRecords()}, an invalid error is returned * in the job result. */ public Boolean allowJaggedRows() { diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java index facf5e659f99..a0914bb17409 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/Dataset.java @@ -28,7 +28,6 @@ import java.io.ObjectInputStream; import java.io.Serializable; import java.util.Iterator; -import java.util.List; import java.util.Objects; /** @@ -49,16 +48,16 @@ private static class TablePageFetcher implements PageImpl.NextPageFetcher
private static final long serialVersionUID = 6906197848579250598L; private final BigQueryOptions options; - private final Page infoPage; + private final Page infoPage; - TablePageFetcher(BigQueryOptions options, Page infoPage) { + TablePageFetcher(BigQueryOptions options, Page infoPage) { this.options = options; this.infoPage = infoPage; } @Override public Page
nextPage() { - Page nextInfoPage = infoPage.nextPage(); + Page nextInfoPage = infoPage.nextPage(); return new PageImpl<>(new TablePageFetcher(options, nextInfoPage), nextInfoPage.nextPageCursor(), new LazyTableIterable(options, nextInfoPage.values())); } @@ -69,10 +68,10 @@ private static class LazyTableIterable implements Iterable
, Serializable private static final long serialVersionUID = 3312744215731674032L; private final BigQueryOptions options; - private final Iterable infoIterable; + private final Iterable infoIterable; private transient BigQuery bigquery; - public LazyTableIterable(BigQueryOptions options, Iterable infoIterable) { + public LazyTableIterable(BigQueryOptions options, Iterable infoIterable) { this.options = options; this.infoIterable = infoIterable; this.bigquery = options.service(); @@ -85,9 +84,9 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE @Override public Iterator
iterator() { - return Iterators.transform(infoIterable.iterator(), new Function() { + return Iterators.transform(infoIterable.iterator(), new Function() { @Override - public Table apply(BaseTableInfo tableInfo) { + public Table apply(TableInfo tableInfo) { return new Table(bigquery, tableInfo); } }); @@ -198,7 +197,7 @@ public boolean delete() { * @throws BigQueryException upon failure */ public Page
list(BigQuery.TableListOption... options) { - Page infoPage = bigquery.listTables(info.datasetId(), options); + Page infoPage = bigquery.listTables(info.datasetId(), options); BigQueryOptions bigqueryOptions = bigquery.options(); return new PageImpl<>(new TablePageFetcher(bigqueryOptions, infoPage), infoPage.nextPageCursor(), new LazyTableIterable(bigqueryOptions, infoPage.values())); @@ -212,69 +211,21 @@ public Page
list(BigQuery.TableListOption... options) { * @throws BigQueryException upon failure */ public Table get(String table, BigQuery.TableOption... options) { - BaseTableInfo tableInfo = - bigquery.getTable(TableId.of(info.datasetId().dataset(), table), options); + TableInfo tableInfo = bigquery.getTable(TableId.of(info.datasetId().dataset(), table), options); return tableInfo != null ? new Table(bigquery, tableInfo) : null; } /** - * Creates a new simple table in this dataset. + * Creates a new table in this dataset. * * @param table the table's user-defined id - * @param schema the table's schema + * @param definition the table's definition * @param options options for table creation * @return a {@code Table} object for the created table * @throws BigQueryException upon failure */ - public Table create(String table, Schema schema, BigQuery.TableOption... options) { - BaseTableInfo tableInfo = TableInfo.of(TableId.of(info.datasetId().dataset(), table), schema); - return new Table(bigquery, bigquery.create(tableInfo, options)); - } - - /** - * Creates a new view table in this dataset. - * - * @param table the table's user-defined id - * @param query the query used to generate the table - * @param functions user-defined functions that can be used by the query - * @param options options for table creation - * @return a {@code Table} object for the created table - * @throws BigQueryException upon failure - */ - public Table create(String table, String query, List functions, - BigQuery.TableOption... options) { - BaseTableInfo tableInfo = - ViewInfo.of(TableId.of(info.datasetId().dataset(), table), query, functions); - return new Table(bigquery, bigquery.create(tableInfo, options)); - } - - /** - * Creates a new view table in this dataset. - * - * @param table the table's user-defined id - * @param query the query used to generate the table - * @param options options for table creation - * @return a {@code Table} object for the created table - * @throws BigQueryException upon failure - */ - public Table create(String table, String query, BigQuery.TableOption... options) { - BaseTableInfo tableInfo = ViewInfo.of(TableId.of(info.datasetId().dataset(), table), query); - return new Table(bigquery, bigquery.create(tableInfo, options)); - } - - /** - * Creates a new external table in this dataset. - * - * @param table the table's user-defined id - * @param configuration data format, location and other properties of an external table - * @param options options for table creation - * @return a {@code Table} object for the created table - * @throws BigQueryException upon failure - */ - public Table create(String table, ExternalDataConfiguration configuration, - BigQuery.TableOption... options) { - BaseTableInfo tableInfo = - ExternalTableInfo.of(TableId.of(info.datasetId().dataset(), table), configuration); + public Table create(String table, TableDefinition definition, BigQuery.TableOption... options) { + TableInfo tableInfo = TableInfo.of(TableId.of(info.datasetId().dataset(), table), definition); return new Table(bigquery, bigquery.create(tableInfo, options)); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalDataConfiguration.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java similarity index 71% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalDataConfiguration.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java index 4344aeba186b..882b1eb7065f 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalDataConfiguration.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google Inc. All Rights Reserved. + * 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. @@ -18,66 +18,91 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.services.bigquery.model.ExternalDataConfiguration; +import com.google.api.services.bigquery.model.Table; import com.google.common.base.Function; -import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.collect.ImmutableList; -import java.io.Serializable; import java.util.List; import java.util.Objects; /** - * Google BigQuery configuration for tables backed by external data. Objects of this class describe - * the data format, location, and other properties of a table stored outside of BigQuery. - * By defining these properties, the data source can then be queried as if it were a standard - * BigQuery table. Support for external tables is experimental and might be subject to changes or - * removed. + * Google BigQuery external table type. BigQuery's external tables are tables whose data reside + * outside of BigQuery but can be queried as normal BigQuery tables. External tables are + * experimental and might be subject to change or removed. * * @see Federated Data Sources * */ -public class ExternalDataConfiguration implements Serializable { +public class ExternalTableDefinition extends TableDefinition { - static final Function FROM_PB_FUNCTION = - new Function() { + static final Function + FROM_EXTERNAL_DATA_FUNCTION = + new Function() { @Override - public ExternalDataConfiguration apply( - com.google.api.services.bigquery.model.ExternalDataConfiguration configurationPb) { - return ExternalDataConfiguration.fromPb(configurationPb); + public ExternalTableDefinition apply(ExternalDataConfiguration pb) { + return ExternalTableDefinition.fromExternalDataConfiguration(pb); } }; - static final Function TO_PB_FUNCTION = - new Function() { + static final Function + TO_EXTERNAL_DATA_FUNCTION = + new Function() { @Override - public com.google.api.services.bigquery.model.ExternalDataConfiguration apply( - ExternalDataConfiguration configuration) { - return configuration.toPb(); + public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) { + return tableInfo.toExternalDataConfigurationPb(); } }; - private static final long serialVersionUID = -8004288831035566549L; + private static final long serialVersionUID = -5951580238459622025L; private final List sourceUris; - private final Schema schema; private final FormatOptions formatOptions; private final Integer maxBadRecords; private final Boolean ignoreUnknownValues; private final String compression; - public static final class Builder { + public static final class Builder + extends TableDefinition.Builder { private List sourceUris; - private Schema schema; private FormatOptions formatOptions; private Integer maxBadRecords; private Boolean ignoreUnknownValues; private String compression; - private Builder() {} + private Builder() { + super(Type.EXTERNAL); + } + + private Builder(ExternalTableDefinition tableDefinition) { + super(tableDefinition); + this.sourceUris = tableDefinition.sourceUris; + this.formatOptions = tableDefinition.formatOptions; + this.maxBadRecords = tableDefinition.maxBadRecords; + this.ignoreUnknownValues = tableDefinition.ignoreUnknownValues; + this.compression = tableDefinition.compression; + } + + private Builder(Table tablePb) { + super(tablePb); + com.google.api.services.bigquery.model.ExternalDataConfiguration externalDataConfiguration = + tablePb.getExternalDataConfiguration(); + if (externalDataConfiguration != null) { + if (externalDataConfiguration.getSourceUris() != null) { + this.sourceUris = ImmutableList.copyOf(externalDataConfiguration.getSourceUris()); + } + if (externalDataConfiguration.getSourceFormat() != null) { + this.formatOptions = FormatOptions.of(externalDataConfiguration.getSourceFormat()); + } + this.compression = externalDataConfiguration.getCompression(); + this.ignoreUnknownValues = externalDataConfiguration.getIgnoreUnknownValues(); + if (externalDataConfiguration.getCsvOptions() != null) { + this.formatOptions = CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()); + } + this.maxBadRecords = externalDataConfiguration.getMaxBadRecords(); + } + } /** * Sets the fully-qualified URIs that point to your data in Google Cloud Storage (e.g. @@ -92,14 +117,6 @@ public Builder sourceUris(List sourceUris) { return this; } - /** - * Sets the schema for the external data. - */ - public Builder schema(Schema schema) { - this.schema = checkNotNull(schema); - return this; - } - /** * Sets the source format, and possibly some parsing options, of the external data. Supported * formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}. @@ -149,18 +166,19 @@ public Builder compression(String compression) { } /** - * Creates an {@code ExternalDataConfiguration} object. + * Creates an {@code ExternalTableDefinition} object. */ - public ExternalDataConfiguration build() { - return new ExternalDataConfiguration(this); + @Override + public ExternalTableDefinition build() { + return new ExternalTableDefinition(this); } } - ExternalDataConfiguration(Builder builder) { + private ExternalTableDefinition(Builder builder) { + super(builder); this.compression = builder.compression; this.ignoreUnknownValues = builder.ignoreUnknownValues; this.maxBadRecords = builder.maxBadRecords; - this.schema = builder.schema; this.formatOptions = builder.formatOptions; this.sourceUris = builder.sourceUris; } @@ -197,13 +215,6 @@ public Integer maxBadRecords() { return maxBadRecords; } - /** - * Returns the schema for the external data. - */ - public Schema schema() { - return schema; - } - /** * Returns the fully-qualified URIs that point to your data in Google Cloud Storage. Each URI can * contain one '*' wildcard character that must come after the bucket's name. Size limits @@ -226,43 +237,42 @@ public F formatOptions() { } /** - * Returns a builder for the {@code ExternalDataConfiguration} object. + * Returns a builder for the {@code ExternalTableDefinition} object. */ + @Override public Builder toBuilder() { - return new Builder() - .compression(compression) - .ignoreUnknownValues(ignoreUnknownValues) - .maxBadRecords(maxBadRecords) - .schema(schema) - .formatOptions(formatOptions) - .sourceUris(sourceUris); + return new Builder(this); } @Override - public String toString() { - return MoreObjects.toStringHelper(this) + ToStringHelper toStringHelper() { + return super.toStringHelper() .add("sourceUris", sourceUris) .add("formatOptions", formatOptions) - .add("schema", schema) .add("compression", compression) .add("ignoreUnknownValues", ignoreUnknownValues) - .add("maxBadRecords", maxBadRecords) - .toString(); + .add("maxBadRecords", maxBadRecords); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ExternalTableDefinition && baseEquals((ExternalTableDefinition) obj); } @Override public int hashCode() { - return Objects.hash(compression, ignoreUnknownValues, maxBadRecords, schema, formatOptions, - sourceUris); + return Objects.hash(baseHashCode(), compression, ignoreUnknownValues, maxBadRecords, + formatOptions, sourceUris); } @Override - public boolean equals(Object obj) { - return obj instanceof ExternalDataConfiguration - && Objects.equals(toPb(), ((ExternalDataConfiguration) obj).toPb()); + com.google.api.services.bigquery.model.Table toPb() { + Table tablePb = super.toPb(); + tablePb.setExternalDataConfiguration(toExternalDataConfigurationPb()); + return tablePb; } - com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() { + com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataConfigurationPb() { com.google.api.services.bigquery.model.ExternalDataConfiguration externalConfigurationPb = new com.google.api.services.bigquery.model.ExternalDataConfiguration(); if (compression != null) { @@ -274,8 +284,8 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() { if (maxBadRecords != null) { externalConfigurationPb.setMaxBadRecords(maxBadRecords); } - if (schema != null) { - externalConfigurationPb.setSchema(schema.toPb()); + if (schema() != null) { + externalConfigurationPb.setSchema(schema().toPb()); } if (formatOptions != null) { externalConfigurationPb.setSourceFormat(formatOptions.type()); @@ -290,7 +300,7 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() { } /** - * Creates a builder for an ExternalDataConfiguration object. + * Creates a builder for an ExternalTableDefinition object. * * @param sourceUris the fully-qualified URIs that point to your data in Google Cloud Storage. * Each URI can contain one '*' wildcard character that must come after the bucket's name. @@ -298,7 +308,7 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() { * of 10 GB maximum size across all URIs. * @param schema the schema for the external data * @param format the source format of the external data - * @return a builder for an ExternalDataConfiguration object given source URIs, schema and format + * @return a builder for an ExternalTableDefinition object given source URIs, schema and format * * @see Quota * @see @@ -309,28 +319,25 @@ public static Builder builder(List sourceUris, Schema schema, FormatOpti } /** - * Creates a builder for an ExternalDataConfiguration object. + * Creates a builder for an ExternalTableDefinition object. * * @param sourceUri a fully-qualified URI that points to your data in Google Cloud Storage. The * URI can contain one '*' wildcard character that must come after the bucket's name. Size * limits related to load jobs apply to external data sources. * @param schema the schema for the external data * @param format the source format of the external data - * @return a builder for an ExternalDataConfiguration object given source URI, schema and format + * @return a builder for an ExternalTableDefinition object given source URI, schema and format * * @see Quota * @see * Source Format */ public static Builder builder(String sourceUri, Schema schema, FormatOptions format) { - return new Builder() - .sourceUris(ImmutableList.of(sourceUri)) - .schema(schema) - .formatOptions(format); + return builder(ImmutableList.of(sourceUri), schema, format); } /** - * Creates an ExternalDataConfiguration object. + * Creates an ExternalTableDefinition object. * * @param sourceUris the fully-qualified URIs that point to your data in Google Cloud Storage. * Each URI can contain one '*' wildcard character that must come after the bucket's name. @@ -338,38 +345,42 @@ public static Builder builder(String sourceUri, Schema schema, FormatOptions for * of 10 GB maximum size across all URIs. * @param schema the schema for the external data * @param format the source format of the external data - * @return an ExternalDataConfiguration object given source URIs, schema and format + * @return an ExternalTableDefinition object given source URIs, schema and format * * @see Quota * @see * Source Format */ - public static ExternalDataConfiguration of(List sourceUris, Schema schema, + public static ExternalTableDefinition of(List sourceUris, Schema schema, FormatOptions format) { return builder(sourceUris, schema, format).build(); } /** - * Creates an ExternalDataConfiguration object. + * Creates an ExternalTableDefinition object. * * @param sourceUri a fully-qualified URI that points to your data in Google Cloud Storage. The * URI can contain one '*' wildcard character that must come after the bucket's name. Size * limits related to load jobs apply to external data sources. * @param schema the schema for the external data * @param format the source format of the external data - * @return an ExternalDataConfiguration object given source URIs, schema and format + * @return an ExternalTableDefinition object given source URIs, schema and format * * @see Quota * @see * Source Format */ - public static ExternalDataConfiguration of(String sourceUri, Schema schema, - FormatOptions format) { + public static ExternalTableDefinition of(String sourceUri, Schema schema, FormatOptions format) { return builder(sourceUri, schema, format).build(); } - static ExternalDataConfiguration fromPb( - com.google.api.services.bigquery.model.ExternalDataConfiguration externalDataConfiguration) { + @SuppressWarnings("unchecked") + static ExternalTableDefinition fromPb(Table tablePb) { + return new Builder(tablePb).build(); + } + + static ExternalTableDefinition fromExternalDataConfiguration( + ExternalDataConfiguration externalDataConfiguration) { Builder builder = new Builder(); if (externalDataConfiguration.getSourceUris() != null) { builder.sourceUris(externalDataConfiguration.getSourceUris()); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableInfo.java deleted file mode 100644 index 80a094425484..000000000000 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableInfo.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 2015 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.ToStringHelper; - -import java.util.Objects; - -/** - * Google BigQuery External Table information. BigQuery's external tables are tables whose data - * reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are - * experimental and might be subject to change or removed. - * - * @see Federated Data Sources - * - */ -public class ExternalTableInfo extends BaseTableInfo { - - private static final long serialVersionUID = -5893406738246214865L; - - private final ExternalDataConfiguration configuration; - - public static final class Builder extends BaseTableInfo.Builder { - - private ExternalDataConfiguration configuration; - - private Builder() {} - - private Builder(ExternalTableInfo tableInfo) { - super(tableInfo); - this.configuration = tableInfo.configuration; - } - - protected Builder(Table tablePb) { - super(tablePb); - if (tablePb.getExternalDataConfiguration() != null) { - this.configuration = - ExternalDataConfiguration.fromPb(tablePb.getExternalDataConfiguration()); - } - } - - /** - * Sets the data format, location and other properties of a table stored outside of BigQuery. - * - * @see Federated Data - * Sources - */ - public Builder configuration(ExternalDataConfiguration configuration) { - this.configuration = checkNotNull(configuration); - return self(); - } - - /** - * Creates a {@code ExternalTableInfo} object. - */ - @Override - public ExternalTableInfo build() { - return new ExternalTableInfo(this); - } - } - - private ExternalTableInfo(Builder builder) { - super(builder); - this.configuration = builder.configuration; - } - - /** - * Returns the data format, location and other properties of a table stored outside of BigQuery. - * This property is experimental and might be subject to change or removed. - * - * @see Federated Data Sources - * - */ - public ExternalDataConfiguration configuration() { - return configuration; - } - - /** - * Returns a builder for the {@code ExternalTableInfo} object. - */ - @Override - public Builder toBuilder() { - return new Builder(this); - } - - @Override - ToStringHelper toStringHelper() { - return super.toStringHelper().add("configuration", configuration); - } - - @Override - public boolean equals(Object obj) { - return obj instanceof ExternalTableInfo && baseEquals((ExternalTableInfo) obj); - } - - @Override - public int hashCode() { - return Objects.hash(baseHashCode(), configuration); - } - - @Override - Table toPb() { - Table tablePb = super.toPb(); - tablePb.setExternalDataConfiguration(configuration.toPb()); - return tablePb; - } - - /** - * Returns a builder for a BigQuery External Table. - * - * @param tableId table id - * @param configuration data format, location and other properties of an External Table - */ - public static Builder builder(TableId tableId, ExternalDataConfiguration configuration) { - return new Builder().tableId(tableId).type(Type.EXTERNAL).configuration(configuration); - } - - /** - * Returns a BigQuery External Table. - * - * @param table table id - * @param configuration data format, location and other properties of an External Table - */ - public static ExternalTableInfo of(TableId table, ExternalDataConfiguration configuration) { - return builder(table, configuration).build(); - } - - @SuppressWarnings("unchecked") - static ExternalTableInfo fromPb(Table tablePb) { - return new Builder(tablePb).build(); - } -} diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java index bd86f208480f..6f39f20e498d 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/InsertAllRequest.java @@ -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/QueryJobConfiguration.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/QueryJobConfiguration.java
index 630a3d5b9088..94c16de149ed 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;
   }
 
@@ -497,8 +497,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/StandardTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java
new file mode 100644
index 000000000000..d6e8f0176609
--- /dev/null
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java
@@ -0,0 +1,281 @@
+/*
+ * 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 type. This type 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 default table type.
+   */
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  /**
+   * Creates a BigQuery default table type 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..cb45c52afd7e 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
@@ -36,7 +36,7 @@
 public final class Table {
 
   private final BigQuery bigquery;
-  private final BaseTableInfo info;
+  private final TableInfo info;
 
   /**
    * Constructs a {@code Table} object for the provided {@code TableInfo}. The BigQuery service
@@ -45,7 +45,7 @@ public final class Table {
    * @param bigquery the BigQuery service used for issuing requests
    * @param info table's info
    */
-  public Table(BigQuery bigquery, BaseTableInfo info) {
+  public Table(BigQuery bigquery, TableInfo info) {
     this.bigquery = checkNotNull(bigquery);
     this.info = checkNotNull(info);
   }
@@ -77,14 +77,14 @@ public static Table get(BigQuery bigquery, String dataset, String table,
    * @throws BigQueryException upon failure
    */
   public static Table get(BigQuery bigquery, TableId table, BigQuery.TableOption... options) {
-    BaseTableInfo info = bigquery.getTable(table, options);
+    TableInfo info = bigquery.getTable(table, options);
     return info != null ? new Table(bigquery, info) : null;
   }
 
   /**
    * Returns the table's information.
    */
-  public BaseTableInfo info() {
+  public TableInfo info() {
     return info;
   }
 
@@ -119,7 +119,7 @@ public Table reload(BigQuery.TableOption... options) {
    * @return a {@code Table} object with updated information
    * @throws BigQueryException upon failure
    */
-  public Table update(BaseTableInfo tableInfo, BigQuery.TableOption... options) {
+  public Table update(TableInfo 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(),
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..de858f54ac43
--- /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 type.
+ */
+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 types.
+   *
+   * @param  the table type class
+   * @param  the table type 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 jobConfiguration) {
+    return Objects.equals(toPb(), jobConfiguration.toPb());
+  }
+
+  Table toPb() {
+    Table tablePb = new Table();
+    if (schema != null) {
+      tablePb.setSchema(schema.toPb());
+    }
+    tablePb.setType(type.name());
+    return tablePb;
+  }
+
+  @SuppressWarnings("unchecked")
+  static  T fromPb(Table tablePb) {
+    switch (Type.valueOf(tablePb.getType())) {
+      case TABLE:
+        return (T) StandardTableDefinition.fromPb(tablePb);
+      case VIEW:
+        return (T) ViewDefinition.fromPb(tablePb);
+      case EXTERNAL:
+        return (T) ExternalTableDefinition.fromPb(tablePb);
+      default:
+        // never reached
+        throw new IllegalArgumentException("Format " + tablePb.getType() + " is not supported");
+    }
+  }
+}
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 aeb1eadd9771..814b8cac1e97 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,12 @@
 
 package com.google.gcloud.bigquery;
 
-import com.google.api.services.bigquery.model.Streamingbuffer;
+import static com.google.common.base.MoreObjects.firstNonNull;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.api.client.util.Data;
 import com.google.api.services.bigquery.model.Table;
+import com.google.common.base.Function;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
 
@@ -26,214 +30,320 @@
 import java.util.Objects;
 
 /**
- * A Google BigQuery Table information. A BigQuery table is a standard, two-dimensional table 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.
+ * Google BigQuery table information. Use {@link StandardTableDefinition} to create simple BigQuery
+ * table. Use {@link ViewDefinition} to create a BigQuery view. Use {@link ExternalTableDefinition}
+ * to create a BigQuery a table backed by external data.
  *
  * @see Managing Tables
  */
-public class TableInfo extends BaseTableInfo {
-
-  private static final long serialVersionUID = -5910575573063546949L;
-
-  private final String location;
-  private final StreamingBuffer streamingBuffer;
+public final class TableInfo implements Serializable {
+
+  static final Function FROM_PB_FUNCTION =
+      new Function() {
+        @Override
+        public TableInfo apply(Table pb) {
+          return TableInfo.fromPb(pb);
+        }
+      };
+  static final Function TO_PB_FUNCTION =
+      new Function() {
+        @Override
+        public Table apply(TableInfo tableInfo) {
+          return tableInfo.toPb();
+        }
+      };
+
+  private static final long serialVersionUID = -7679032506430816205L;
+
+  private final String etag;
+  private final String id;
+  private final String selfLink;
+  private final TableId tableId;
+  private final String friendlyName;
+  private final String description;
+  private final Long creationTime;
+  private final Long expirationTime;
+  private final Long lastModifiedTime;
+  private final TableDefinition definition;
 
   /**
-   * 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.
+   * Builder for tables.
    */
-  public static class StreamingBuffer implements Serializable {
+  public static class Builder {
+
+    private String etag;
+    private String id;
+    private String selfLink;
+    private TableId tableId;
+    private String friendlyName;
+    private String description;
+    private Long creationTime;
+    private Long expirationTime;
+    private Long lastModifiedTime;
+    private TableDefinition definition;
 
-    private static final long serialVersionUID = -6713971364725267597L;
-    private final long estimatedRows;
-    private final long estimatedBytes;
-    private final long oldestEntryTime;
+    private Builder() {}
 
-    StreamingBuffer(long estimatedRows, long estimatedBytes, long oldestEntryTime) {
-      this.estimatedRows = estimatedRows;
-      this.estimatedBytes = estimatedBytes;
-      this.oldestEntryTime = oldestEntryTime;
+    private Builder(TableInfo tableInfo) {
+      this.etag = tableInfo.etag;
+      this.id = tableInfo.id;
+      this.selfLink = tableInfo.selfLink;
+      this.tableId = tableInfo.tableId;
+      this.friendlyName = tableInfo.friendlyName;
+      this.description = tableInfo.description;
+      this.creationTime = tableInfo.creationTime;
+      this.expirationTime = tableInfo.expirationTime;
+      this.lastModifiedTime = tableInfo.lastModifiedTime;
+      this.definition = tableInfo.definition;
     }
 
-    /**
-     * Returns a lower-bound estimate of the number of rows currently in the streaming buffer.
-     */
-    public long estimatedRows() {
-      return estimatedRows;
+    private Builder(Table tablePb) {
+      this.tableId = TableId.fromPb(tablePb.getTableReference());
+      if (tablePb.getLastModifiedTime() != null) {
+        this.lastModifiedTime(tablePb.getLastModifiedTime().longValue());
+      }
+      this.description = tablePb.getDescription();
+      this.expirationTime = tablePb.getExpirationTime();
+      this.friendlyName = tablePb.getFriendlyName();
+      this.creationTime = tablePb.getCreationTime();
+      this.etag = tablePb.getEtag();
+      this.id = tablePb.getId();
+      this.selfLink = tablePb.getSelfLink();
+      this.definition = TableDefinition.fromPb(tablePb);
     }
 
-    /**
-     * Returns a lower-bound estimate of the number of bytes currently in the streaming buffer.
-     */
-    public long estimatedBytes() {
-      return estimatedBytes;
+    Builder creationTime(Long creationTime) {
+      this.creationTime = creationTime;
+      return this;
     }
 
     /**
-     * Returns the timestamp of the oldest entry in the streaming buffer, in milliseconds since
-     * epoch.
+     * Sets a user-friendly description for the table.
      */
-    public long oldestEntryTime() {
-      return oldestEntryTime;
+    public Builder description(String description) {
+      this.description = firstNonNull(description, Data.nullOf(String.class));
+      return this;
     }
 
-    @Override
-    public String toString() {
-      return MoreObjects.toStringHelper(this)
-          .add("estimatedRows", estimatedRows)
-          .add("estimatedBytes", estimatedBytes)
-          .add("oldestEntryTime", oldestEntryTime)
-          .toString();
+    Builder etag(String etag) {
+      this.etag = etag;
+      return this;
     }
 
-    @Override
-    public int hashCode() {
-      return Objects.hash(estimatedRows, estimatedBytes, oldestEntryTime);
+    /**
+     * Sets the time when this table expires, in milliseconds since the epoch. If not present, the
+     * table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.
+     */
+    public Builder expirationTime(Long expirationTime) {
+      this.expirationTime = firstNonNull(expirationTime, Data.nullOf(Long.class));
+      return this;
     }
 
-    @Override
-    public boolean equals(Object obj) {
-      return obj instanceof StreamingBuffer
-          && Objects.equals(toPb(), ((StreamingBuffer) obj).toPb());
+    /**
+     * Sets a user-friendly name for the table.
+     */
+    public Builder friendlyName(String friendlyName) {
+      this.friendlyName = firstNonNull(friendlyName, Data.nullOf(String.class));
+      return this;
     }
 
-    Streamingbuffer toPb() {
-      return new Streamingbuffer()
-          .setEstimatedBytes(BigInteger.valueOf(estimatedBytes))
-          .setEstimatedRows(BigInteger.valueOf(estimatedRows))
-          .setOldestEntryTime(BigInteger.valueOf(oldestEntryTime));
+    Builder id(String id) {
+      this.id = id;
+      return this;
     }
 
-    static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
-      return new StreamingBuffer(streamingBufferPb.getEstimatedRows().longValue(),
-          streamingBufferPb.getEstimatedBytes().longValue(),
-          streamingBufferPb.getOldestEntryTime().longValue());
+    Builder lastModifiedTime(Long lastModifiedTime) {
+      this.lastModifiedTime = lastModifiedTime;
+      return this;
     }
-  }
-
-  public static final class Builder extends BaseTableInfo.Builder {
 
-    private String location;
-    private StreamingBuffer streamingBuffer;
-
-    private Builder() {}
-
-    private Builder(TableInfo tableInfo) {
-      super(tableInfo);
-      this.location = tableInfo.location;
-      this.streamingBuffer = tableInfo.streamingBuffer;
-    }
-
-    protected Builder(Table tablePb) {
-      super(tablePb);
-      this.location = tablePb.getLocation();
-      if (tablePb.getStreamingBuffer() != null) {
-        this.streamingBuffer = StreamingBuffer.fromPb(tablePb.getStreamingBuffer());
-      }
+    Builder selfLink(String selfLink) {
+      this.selfLink = selfLink;
+      return this;
     }
 
-    Builder location(String location) {
-      this.location = location;
-      return self();
+    /**
+     * Sets the table identity.
+     */
+    public Builder tableId(TableId tableId) {
+      this.tableId = checkNotNull(tableId);
+      return this;
     }
 
-    Builder streamingBuffer(StreamingBuffer streamingBuffer) {
-      this.streamingBuffer = streamingBuffer;
-      return self();
+    /**
+     * Sets the table definition. Use {@link StandardTableDefinition} to create simple BigQuery
+     * table. Use {@link ViewDefinition} to create a BigQuery view. Use
+     * {@link ExternalTableDefinition} to create a BigQuery a table backed by external data.
+     */
+    public Builder definition(TableDefinition definition) {
+      this.definition = checkNotNull(definition);
+      return this;
     }
 
     /**
      * Creates a {@code TableInfo} object.
      */
-    @Override
     public TableInfo build() {
       return new TableInfo(this);
     }
   }
 
   private TableInfo(Builder builder) {
-    super(builder);
-    this.location = builder.location;
-    this.streamingBuffer = builder.streamingBuffer;
+    this.tableId = checkNotNull(builder.tableId);
+    this.etag = builder.etag;
+    this.id = builder.id;
+    this.selfLink = builder.selfLink;
+    this.friendlyName = builder.friendlyName;
+    this.description = builder.description;
+    this.creationTime = builder.creationTime;
+    this.expirationTime = builder.expirationTime;
+    this.lastModifiedTime = builder.lastModifiedTime;
+    this.definition = builder.definition;
   }
 
   /**
-   * Returns the geographic location where the table should reside. This value is inherited from the
-   * dataset.
-   *
-   * @see 
-   *     Dataset Location
+   * Returns the hash of the table resource.
    */
-  public String location() {
-    return location;
+  public String etag() {
+    return etag;
   }
 
   /**
-   * Returns information on the table's streaming buffer if any exists. Returns {@code null} if no
-   * streaming buffer exists.
+   * Returns an opaque id for the table.
    */
-  public StreamingBuffer streamingBuffer() {
-    return streamingBuffer;
+  public String id() {
+    return id;
   }
 
   /**
-   * Returns a builder for a BigQuery Table.
-   *
-   * @param tableId table id
-   * @param schema the schema of the table
+   * Returns an URL that can be used to access the resource again. The returned URL can be used for
+   * get or update requests.
    */
-  public static Builder builder(TableId tableId, Schema schema) {
-    return new Builder().tableId(tableId).type(Type.TABLE).schema(schema);
+  public String selfLink() {
+    return selfLink;
   }
 
   /**
-   * Creates BigQuery table given its type.
-   *
-   * @param tableId table id
-   * @param schema the schema of the table
+   * Returns the table identity.
    */
-  public static TableInfo of(TableId tableId, Schema schema) {
-    return builder(tableId, schema).build();
+  public TableId tableId() {
+    return tableId;
   }
 
   /**
-   * Returns a builder for the {@code TableInfo} object.
+   * Returns a user-friendly name for the table.
+   */
+  public String friendlyName() {
+    return Data.isNull(friendlyName) ? null : friendlyName;
+  }
+
+  /**
+   * Returns a user-friendly description for the table.
+   */
+  public String description() {
+    return Data.isNull(description) ? null : description;
+  }
+
+  /**
+   * Returns the time when this table was created, in milliseconds since the epoch.
+   */
+  public Long creationTime() {
+    return creationTime;
+  }
+
+  /**
+   * Returns the time when this table expires, in milliseconds since the epoch. If not present, the
+   * table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.
+   */
+  public Long expirationTime() {
+    return Data.isNull(expirationTime) ? null : expirationTime;
+  }
+
+  /**
+   * Returns the time when this table was last modified, in milliseconds since the epoch.
+   */
+  public Long lastModifiedTime() {
+    return lastModifiedTime;
+  }
+
+  /**
+   * Returns the table definition.
+   */
+  @SuppressWarnings("unchecked")
+  public  T definition() {
+    return (T) definition;
+  }
+
+  /**
+   * Returns a builder for the object.
    */
-  @Override
   public Builder toBuilder() {
     return new Builder(this);
   }
 
-  @Override
   ToStringHelper toStringHelper() {
-    return super.toStringHelper()
-        .add("location", location)
-        .add("streamingBuffer", streamingBuffer);
+    return MoreObjects.toStringHelper(this)
+        .add("tableId", tableId)
+        .add("etag", etag)
+        .add("id", id)
+        .add("selfLink", selfLink)
+        .add("friendlyName", friendlyName)
+        .add("description", description)
+        .add("expirationTime", expirationTime)
+        .add("creationTime", creationTime)
+        .add("lastModifiedTime", lastModifiedTime)
+        .add("definition", definition);
   }
 
   @Override
-  public boolean equals(Object obj) {
-    return obj instanceof TableInfo && baseEquals((TableInfo) obj);
+  public String toString() {
+    return toStringHelper().toString();
   }
 
   @Override
   public int hashCode() {
-    return Objects.hash(baseHashCode(), location, streamingBuffer);
+    return Objects.hash(tableId);
   }
 
   @Override
+  public boolean equals(Object obj) {
+    return obj instanceof TableInfo && Objects.equals(toPb(), ((TableInfo) obj).toPb());
+  }
+
+  /**
+   * Returns a builder for a {@code TableInfo} object given table identity and definition.
+   */
+  public static Builder builder(TableId tableId, TableDefinition definition) {
+    return new Builder().tableId(tableId).definition(definition);
+  }
+
+  /**
+   * Returns a {@code TableInfo} object given table identity and definition.
+   */
+  public static TableInfo of(TableId tableId, TableDefinition definition) {
+    return builder(tableId, definition).build();
+  }
+
+  TableInfo setProjectId(String projectId) {
+    return toBuilder().tableId(tableId().setProjectId(projectId)).build();
+  }
+
   Table toPb() {
-    Table tablePb = super.toPb();
-    tablePb.setLocation(location);
-    if (streamingBuffer != null) {
-      tablePb.setStreamingBuffer(streamingBuffer.toPb());
+    Table tablePb = definition.toPb();
+    tablePb.setTableReference(tableId.toPb());
+    if (lastModifiedTime != null) {
+      tablePb.setLastModifiedTime(BigInteger.valueOf(lastModifiedTime));
     }
+    tablePb.setCreationTime(creationTime);
+    tablePb.setDescription(description);
+    tablePb.setEtag(etag);
+    tablePb.setExpirationTime(expirationTime);
+    tablePb.setFriendlyName(friendlyName);
+    tablePb.setId(id);
+    tablePb.setSelfLink(selfLink);
     return tablePb;
   }
 
-  @SuppressWarnings("unchecked")
   static TableInfo fromPb(Table tablePb) {
     return new Builder(tablePb).build();
   }
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/ViewDefinition.java
similarity index 63%
rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java
rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java
index 2698921bc034..7065bcc155d2 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/ViewDefinition.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Google Inc. All Rights Reserved.
+ * 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.
@@ -19,7 +19,6 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import com.google.api.services.bigquery.model.Table;
-import com.google.api.services.bigquery.model.ViewDefinition;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
@@ -28,35 +27,36 @@
 import java.util.Objects;
 
 /**
- * Google BigQuery View Table information. BigQuery's views are logical views, not materialized
- * views, which means that the query that defines the view is re-executed every time the view is
- * queried.
+ * Google BigQuery view table type. BigQuery's views are logical views, not materialized views,
+ * which means that the query that defines the view is re-executed every time the view is queried.
  *
  * @see Views
  */
-public class ViewInfo extends BaseTableInfo {
+public final class ViewDefinition extends TableDefinition {
 
-  private static final long serialVersionUID = 7567772157817454901L;
+  private static final long serialVersionUID = -8789311196910794545L;
 
   private final String query;
   private final List userDefinedFunctions;
 
-  public static final class Builder extends BaseTableInfo.Builder {
+  public static final class Builder extends TableDefinition.Builder {
 
     private String query;
     private List userDefinedFunctions;
 
-    private Builder() {}
+    private Builder() {
+      super(Type.VIEW);
+    }
 
-    private Builder(ViewInfo viewInfo) {
-      super(viewInfo);
-      this.query = viewInfo.query;
-      this.userDefinedFunctions = viewInfo.userDefinedFunctions;
+    private Builder(ViewDefinition viewDefinition) {
+      super(viewDefinition);
+      this.query = viewDefinition.query;
+      this.userDefinedFunctions = viewDefinition.userDefinedFunctions;
     }
 
-    protected Builder(Table tablePb) {
+    private Builder(Table tablePb) {
       super(tablePb);
-      ViewDefinition viewPb = tablePb.getView();
+      com.google.api.services.bigquery.model.ViewDefinition viewPb = tablePb.getView();
       if (viewPb != null) {
         this.query = viewPb.getQuery();
         if (viewPb.getUserDefinedFunctionResources() != null) {
@@ -97,15 +97,15 @@ public Builder userDefinedFunctions(UserDefinedFunction... userDefinedFunctions)
     }
 
     /**
-     * Creates a {@code ViewInfo} object.
+     * Creates a {@code ViewDefinition} object.
      */
     @Override
-    public ViewInfo build() {
-      return new ViewInfo(this);
+    public ViewDefinition build() {
+      return new ViewDefinition(this);
     }
   }
 
-  private ViewInfo(Builder builder) {
+  private ViewDefinition(Builder builder) {
     super(builder);
     this.query = builder.query;
     this.userDefinedFunctions = builder.userDefinedFunctions;
@@ -146,7 +146,7 @@ ToStringHelper toStringHelper() {
 
   @Override
   public boolean equals(Object obj) {
-    return obj instanceof ViewInfo && baseEquals((ViewInfo) obj);
+    return obj instanceof ViewDefinition && baseEquals((ViewDefinition) obj);
   }
 
   @Override
@@ -157,7 +157,8 @@ public int hashCode() {
   @Override
   Table toPb() {
     Table tablePb = super.toPb();
-    ViewDefinition viewDefinition = new ViewDefinition().setQuery(query);
+    com.google.api.services.bigquery.model.ViewDefinition viewDefinition =
+        new com.google.api.services.bigquery.model.ViewDefinition().setQuery(query);
     if (userDefinedFunctions != null) {
       viewDefinition.setUserDefinedFunctionResources(Lists.transform(userDefinedFunctions,
           UserDefinedFunction.TO_PB_FUNCTION));
@@ -167,79 +168,65 @@ Table toPb() {
   }
 
   /**
-   * Returns a builder for a BigQuery View Table.
+   * Returns a builder for a BigQuery view type.
    *
-   * @param tableId table id
-   * @param query the query used to generate the table
+   * @param query the query used to generate the view
    */
-  public static Builder builder(TableId tableId, String query) {
-    return new Builder().tableId(tableId).type(Type.VIEW).query(query);
+  public static Builder builder(String query) {
+    return new Builder().query(query);
   }
 
   /**
-   * Returns a builder for a BigQuery View Table.
+   * Returns a builder for a BigQuery view type.
    *
-   * @param table table id
    * @param query the query used to generate the table
    * @param functions user-defined functions that can be used by the query
    */
-  public static Builder builder(TableId table, String query, List functions) {
-    return new Builder()
-        .tableId(table)
-        .type(Type.VIEW)
-        .userDefinedFunctions(functions)
-        .query(query);
+  public static Builder builder(String query, List functions) {
+    return new Builder().type(Type.VIEW).userDefinedFunctions(functions).query(query);
   }
 
   /**
-   * Returns a builder for a BigQuery View Table.
+   * Returns a builder for a BigQuery view type.
    *
-   * @param table table id
    * @param query the query used to generate the table
    * @param functions user-defined functions that can be used by the query
    */
-  public static Builder builder(TableId table, String query, UserDefinedFunction... functions) {
-    return new Builder()
-        .tableId(table)
-        .type(Type.VIEW)
-        .userDefinedFunctions(functions)
-        .query(query);
+  public static Builder builder(String query, UserDefinedFunction... functions) {
+    return new Builder().type(Type.VIEW).userDefinedFunctions(functions).query(query);
   }
 
   /**
-   * Creates a BigQuery View given table identity and query.
+   * Creates a BigQuery view type given the query used to generate the table.
    *
-   * @param tableId table id
    * @param query the query used to generate the table
    */
-  public static ViewInfo of(TableId tableId, String query) {
-    return builder(tableId, query).build();
+  public static ViewDefinition of(String query) {
+    return builder(query).build();
   }
 
   /**
-   * Creates a BigQuery View given table identity, a query and some user-defined functions.
+   * Creates a BigQuery view type given a query and some user-defined functions.
    *
-   * @param table table id
    * @param query the query used to generate the table
    * @param functions user-defined functions that can be used by the query
    */
-  public static ViewInfo of(TableId table, String query, List functions) {
-    return builder(table, query, functions).build();
+  public static ViewDefinition of(String query, List functions) {
+    return builder(query, functions).build();
   }
 
   /**
-   * Creates a BigQuery View given table identity, a query and some user-defined functions.
+   * Creates a BigQuery view type given a query and some user-defined functions.
    *
-   * @param table table id
    * @param query the query used to generate the table
    * @param functions user-defined functions that can be used by the query
    */
-  public static ViewInfo of(TableId table, String query, UserDefinedFunction... functions) {
-    return builder(table, query, functions).build();
+  public static ViewDefinition of(String query, UserDefinedFunction... functions) {
+    return builder(query, functions).build();
   }
 
   @SuppressWarnings("unchecked")
-  static ViewInfo fromPb(Table tablePb) {
+  static ViewDefinition fromPb(Table tablePb) {
     return new Builder(tablePb).build();
   }
 }
diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java
index dd57da2b606a..a249768f9d8d 100644
--- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java
+++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/package-info.java
@@ -21,11 +21,12 @@
  * 
 {@code
  * BigQuery bigquery = BigQueryOptions.defaultInstance().service();
  * TableId tableId = TableId.of("dataset", "table");
- * BaseTableInfo info = bigquery.getTable(tableId);
+ * TableInfo info = bigquery.getTable(tableId);
  * if (info == null) {
  *   System.out.println("Creating table " + tableId);
  *   Field integerField = Field.of("fieldName", Field.Type.integer());
- *   bigquery.create(TableInfo.of(tableId, Schema.of(integerField)));
+ *   Schema schema = Schema.of(integerField);
+ *   bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
  * } else {
  *   System.out.println("Loading data into table " + tableId);
  *   LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
index 8af8c700cd8c..afad9041e802 100644
--- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
+++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java
@@ -104,10 +104,12 @@ public class BigQueryImplTest {
           .description("FieldDescription3")
           .build();
   private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3);
-  private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_SCHEMA);
-  private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_SCHEMA);
+  private static final StandardTableDefinition TABLE_DEFINITION =
+      StandardTableDefinition.of(TABLE_SCHEMA);
+  private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_DEFINITION);
+  private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_DEFINITION);
   private static final TableInfo TABLE_INFO_WITH_PROJECT =
-      TableInfo.of(TABLE_ID_WITH_PROJECT, TABLE_SCHEMA);
+      TableInfo.of(TABLE_ID_WITH_PROJECT, TABLE_DEFINITION);
   private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION =
       LoadJobConfiguration.of(TABLE_ID, "URI");
   private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION_WITH_PROJECT =
@@ -511,47 +513,47 @@ public void testGetTableWithSelectedFields() {
   @Test
   public void testListTables() {
     String cursor = "cursor";
-    ImmutableList tableList =
-        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
+    ImmutableList tableList =
+        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
     Tuple> result =
-        Tuple.of(cursor, Iterables.transform(tableList, BaseTableInfo.TO_PB_FUNCTION));
+        Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
     EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result);
     EasyMock.replay(bigqueryRpcMock);
     bigquery = options.service();
-    Page page = bigquery.listTables(DATASET);
+    Page page = bigquery.listTables(DATASET);
     assertEquals(cursor, page.nextPageCursor());
-    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), BaseTableInfo.class));
+    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), TableInfo.class));
   }
 
   @Test
   public void testListTablesFromDatasetId() {
     String cursor = "cursor";
-    ImmutableList tableList =
-        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
+    ImmutableList tableList =
+        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
     Tuple> result =
-        Tuple.of(cursor, Iterables.transform(tableList, BaseTableInfo.TO_PB_FUNCTION));
+        Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
     EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result);
     EasyMock.replay(bigqueryRpcMock);
     bigquery = options.service();
-    Page page = bigquery.listTables(DatasetId.of(PROJECT, DATASET));
+    Page page = bigquery.listTables(DatasetId.of(PROJECT, DATASET));
     assertEquals(cursor, page.nextPageCursor());
-    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), BaseTableInfo.class));
+    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), TableInfo.class));
   }
 
   @Test
   public void testListTablesWithOptions() {
     String cursor = "cursor";
-    ImmutableList tableList =
-        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
+    ImmutableList tableList =
+        ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO);
     Tuple> result =
-        Tuple.of(cursor, Iterables.transform(tableList, BaseTableInfo.TO_PB_FUNCTION));
+        Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION));
     EasyMock.expect(bigqueryRpcMock.listTables(DATASET, TABLE_LIST_OPTIONS)).andReturn(result);
     EasyMock.replay(bigqueryRpcMock);
     bigquery = options.service();
-    Page page = bigquery.listTables(DATASET, TABLE_LIST_MAX_RESULTS,
+    Page page = bigquery.listTables(DATASET, TABLE_LIST_MAX_RESULTS,
         TABLE_LIST_PAGE_TOKEN);
     assertEquals(cursor, page.nextPageCursor());
-    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), BaseTableInfo.class));
+    assertArrayEquals(tableList.toArray(), Iterables.toArray(page.values(), TableInfo.class));
   }
 
   @Test
diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java
index 544fc2378b23..455212e16d3a 100644
--- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java
+++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DatasetTest.java
@@ -38,22 +38,21 @@
 import org.junit.rules.ExpectedException;
 
 import java.util.Iterator;
-import java.util.List;
 
 public class DatasetTest {
 
   private static final DatasetId DATASET_ID = DatasetId.of("dataset");
   private static final DatasetInfo DATASET_INFO = DatasetInfo.builder(DATASET_ID).build();
   private static final Field FIELD = Field.of("FieldName", Field.Type.integer());
-  private static final Iterable TABLE_INFO_RESULTS = ImmutableList.of(
-      TableInfo.builder(TableId.of("dataset", "table1"), Schema.of(FIELD)).build(),
-      ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY").build(),
-      ExternalTableInfo.builder(TableId.of("dataset", "table2"),
-          ExternalDataConfiguration.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv()))
-          .build());
-  private static final UserDefinedFunction FUNCTION1 = UserDefinedFunction.inline("inline");
-  private static final UserDefinedFunction FUNCTION2 = UserDefinedFunction.inline("gs://b/f");
-  private static final List FUNCTIONS = ImmutableList.of(FUNCTION1, FUNCTION2);
+  private static final StandardTableDefinition TABLE_DEFINITION =
+      StandardTableDefinition.of(Schema.of(FIELD));
+  private static final ViewDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY");
+  private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION =
+      ExternalTableDefinition.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv());
+  private static final Iterable TABLE_INFO_RESULTS = ImmutableList.of(
+      TableInfo.builder(TableId.of("dataset", "table1"), TABLE_DEFINITION).build(),
+      TableInfo.builder(TableId.of("dataset", "table2"), VIEW_DEFINITION).build(),
+      TableInfo.builder(TableId.of("dataset", "table2"), EXTERNAL_TABLE_DEFINITION).build());
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
@@ -168,13 +167,13 @@ public void testDelete() throws Exception {
   @Test
   public void testList() throws Exception {
     BigQueryOptions bigqueryOptions = createStrictMock(BigQueryOptions.class);
-    PageImpl tableInfoPage = new PageImpl<>(null, "c", TABLE_INFO_RESULTS);
+    PageImpl tableInfoPage = new PageImpl<>(null, "c", TABLE_INFO_RESULTS);
     expect(bigquery.listTables(DATASET_INFO.datasetId())).andReturn(tableInfoPage);
     expect(bigquery.options()).andReturn(bigqueryOptions);
     expect(bigqueryOptions.service()).andReturn(bigquery);
     replay(bigquery, bigqueryOptions);
     Page
tablePage = dataset.list(); - Iterator tableInfoIterator = tableInfoPage.values().iterator(); + Iterator tableInfoIterator = tableInfoPage.values().iterator(); Iterator
tableIterator = tablePage.values().iterator(); while (tableInfoIterator.hasNext() && tableIterator.hasNext()) { assertEquals(tableInfoIterator.next(), tableIterator.next().info()); @@ -188,14 +187,14 @@ public void testList() throws Exception { @Test public void testListWithOptions() throws Exception { BigQueryOptions bigqueryOptions = createStrictMock(BigQueryOptions.class); - PageImpl tableInfoPage = new PageImpl<>(null, "c", TABLE_INFO_RESULTS); + PageImpl tableInfoPage = new PageImpl<>(null, "c", TABLE_INFO_RESULTS); expect(bigquery.listTables(DATASET_INFO.datasetId(), BigQuery.TableListOption.maxResults(10L))) .andReturn(tableInfoPage); expect(bigquery.options()).andReturn(bigqueryOptions); expect(bigqueryOptions.service()).andReturn(bigquery); replay(bigquery, bigqueryOptions); Page
tablePage = dataset.list(BigQuery.TableListOption.maxResults(10L)); - Iterator tableInfoIterator = tableInfoPage.values().iterator(); + Iterator tableInfoIterator = tableInfoPage.values().iterator(); Iterator
tableIterator = tablePage.values().iterator(); while (tableInfoIterator.hasNext() && tableIterator.hasNext()) { assertEquals(tableInfoIterator.next(), tableIterator.next().info()); @@ -208,7 +207,7 @@ public void testListWithOptions() throws Exception { @Test public void testGet() throws Exception { - BaseTableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of()).build(); + TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_DEFINITION).build(); expect(bigquery.getTable(TableId.of("dataset", "table1"))).andReturn(info); replay(bigquery); Table table = dataset.get("table1"); @@ -225,7 +224,7 @@ public void testGetNull() throws Exception { @Test public void testGetWithOptions() throws Exception { - BaseTableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of()).build(); + TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_DEFINITION).build(); expect(bigquery.getTable(TableId.of("dataset", "table1"), BigQuery.TableOption.fields())) .andReturn(info); replay(bigquery); @@ -236,70 +235,19 @@ public void testGetWithOptions() throws Exception { @Test public void testCreateTable() throws Exception { - TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of(FIELD)).build(); + TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_DEFINITION).build(); expect(bigquery.create(info)).andReturn(info); replay(bigquery); - Table table = dataset.create("table1", Schema.of(FIELD)); + Table table = dataset.create("table1", TABLE_DEFINITION); assertEquals(info, table.info()); } @Test public void testCreateTableWithOptions() throws Exception { - TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), Schema.of(FIELD)).build(); + TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_DEFINITION).build(); expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); replay(bigquery); - Table table = dataset.create("table1", Schema.of(FIELD), BigQuery.TableOption.fields()); - assertEquals(info, table.info()); - } - - @Test - public void testCreateView() throws Exception { - ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY").build(); - expect(bigquery.create(info)).andReturn(info); - replay(bigquery); - Table table = dataset.create("table2", "QUERY"); - assertEquals(info, table.info()); - } - - @Test - public void testCreateViewWithUserDefinedFunctions() throws Exception { - ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY", FUNCTIONS).build(); - expect(bigquery.create(info)).andReturn(info); - replay(bigquery); - Table table = dataset.create("table2", "QUERY", FUNCTIONS); - assertEquals(info, table.info()); - } - - @Test - public void testCreateViewWithOptions() throws Exception { - ViewInfo info = ViewInfo.builder(TableId.of("dataset", "table2"), "QUERY").build(); - expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); - replay(bigquery); - Table table = dataset.create("table2", "QUERY", BigQuery.TableOption.fields()); - assertEquals(info, table.info()); - } - - @Test - public void testCreateExternalTable() throws Exception { - ExternalTableInfo info = ExternalTableInfo.builder(TableId.of("dataset", "table3"), - ExternalDataConfiguration.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv())) - .build(); - expect(bigquery.create(info)).andReturn(info); - replay(bigquery); - Table table = dataset.create("table3", ExternalDataConfiguration.of( - ImmutableList.of("URI"), Schema.of(), FormatOptions.csv())); - assertEquals(info, table.info()); - } - - @Test - public void testCreateExternalTableWithOptions() throws Exception { - ExternalTableInfo info = ExternalTableInfo.builder(TableId.of("dataset", "table3"), - ExternalDataConfiguration.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv())) - .build(); - expect(bigquery.create(info, BigQuery.TableOption.fields())).andReturn(info); - replay(bigquery); - Table table = dataset.create("table3", ExternalDataConfiguration.of( - ImmutableList.of("URI"), Schema.of(), FormatOptions.csv()), BigQuery.TableOption.fields()); + Table table = dataset.create("table1", TABLE_DEFINITION, BigQuery.TableOption.fields()); assertEquals(info, table.info()); } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalDataConfigurationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java similarity index 52% rename from gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalDataConfigurationTest.java rename to gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java index f9b7c31e1071..247032dff890 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalDataConfigurationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google Inc. All Rights Reserved. + * 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. @@ -24,7 +24,7 @@ import java.util.List; -public class ExternalDataConfigurationTest { +public class ExternalTableDefinitionTest { private static final List SOURCE_URIS = ImmutableList.of("uri1", "uri2"); private static final Field FIELD_SCHEMA1 = @@ -47,51 +47,56 @@ public class ExternalDataConfigurationTest { private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final String COMPRESSION = "GZIP"; private static final CsvOptions CSV_OPTIONS = CsvOptions.builder().build(); - private static final ExternalDataConfiguration CONFIGURATION = ExternalDataConfiguration - .builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) - .compression(COMPRESSION) - .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) - .maxBadRecords(MAX_BAD_RECORDS) - .build(); + private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + .compression(COMPRESSION) + .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) + .maxBadRecords(MAX_BAD_RECORDS) + .build(); @Test public void testToBuilder() { - compareConfiguration(CONFIGURATION, CONFIGURATION.toBuilder().build()); - ExternalDataConfiguration configuration = CONFIGURATION.toBuilder().compression("NONE").build(); - assertEquals("NONE", configuration.compression()); - configuration = configuration.toBuilder() + compareExternalTableDefinition(EXTERNAL_TABLE_DEFINITION, + EXTERNAL_TABLE_DEFINITION.toBuilder().build()); + ExternalTableDefinition externalTableDefinition = + EXTERNAL_TABLE_DEFINITION.toBuilder().compression("NONE").build(); + assertEquals("NONE", externalTableDefinition.compression()); + externalTableDefinition = externalTableDefinition.toBuilder() .compression(COMPRESSION) .build(); - compareConfiguration(CONFIGURATION, configuration); + compareExternalTableDefinition(EXTERNAL_TABLE_DEFINITION, externalTableDefinition); } @Test public void testToBuilderIncomplete() { - ExternalDataConfiguration configuration = - ExternalDataConfiguration.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); - assertEquals(configuration, configuration.toBuilder().build()); + ExternalTableDefinition externalTableDefinition = + ExternalTableDefinition.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); + assertEquals(externalTableDefinition, externalTableDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(COMPRESSION, CONFIGURATION.compression()); - assertEquals(CSV_OPTIONS, CONFIGURATION.formatOptions()); - assertEquals(IGNORE_UNKNOWN_VALUES, CONFIGURATION.ignoreUnknownValues()); - assertEquals(MAX_BAD_RECORDS, CONFIGURATION.maxBadRecords()); - assertEquals(TABLE_SCHEMA, CONFIGURATION.schema()); - assertEquals(SOURCE_URIS, CONFIGURATION.sourceUris()); + assertEquals(TableDefinition.Type.EXTERNAL, EXTERNAL_TABLE_DEFINITION.type()); + assertEquals(COMPRESSION, EXTERNAL_TABLE_DEFINITION.compression()); + assertEquals(CSV_OPTIONS, EXTERNAL_TABLE_DEFINITION.formatOptions()); + assertEquals(IGNORE_UNKNOWN_VALUES, EXTERNAL_TABLE_DEFINITION.ignoreUnknownValues()); + assertEquals(MAX_BAD_RECORDS, EXTERNAL_TABLE_DEFINITION.maxBadRecords()); + assertEquals(TABLE_SCHEMA, EXTERNAL_TABLE_DEFINITION.schema()); + assertEquals(SOURCE_URIS, EXTERNAL_TABLE_DEFINITION.sourceUris()); } @Test public void testToAndFromPb() { - compareConfiguration(CONFIGURATION, ExternalDataConfiguration.fromPb(CONFIGURATION.toPb())); - ExternalDataConfiguration configuration = - ExternalDataConfiguration.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS).build(); - compareConfiguration(configuration, ExternalDataConfiguration.fromPb(configuration.toPb())); + compareExternalTableDefinition(EXTERNAL_TABLE_DEFINITION, + ExternalTableDefinition.fromPb(EXTERNAL_TABLE_DEFINITION.toPb())); + ExternalTableDefinition externalTableDefinition = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS).build(); + compareExternalTableDefinition(externalTableDefinition, + ExternalTableDefinition.fromPb(externalTableDefinition.toPb())); } - private void compareConfiguration(ExternalDataConfiguration expected, - ExternalDataConfiguration value) { + private void compareExternalTableDefinition(ExternalTableDefinition expected, + ExternalTableDefinition value) { assertEquals(expected, value); assertEquals(expected.compression(), value.compression()); assertEquals(expected.formatOptions(), value.formatOptions()); @@ -99,5 +104,6 @@ private void compareConfiguration(ExternalDataConfiguration expected, assertEquals(expected.maxBadRecords(), value.maxBadRecords()); assertEquals(expected.schema(), value.schema()); assertEquals(expected.sourceUris(), value.sourceUris()); + assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java index e083d3682d8c..0928c04ea6d2 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ITBigQueryTest.java @@ -257,20 +257,21 @@ public void testGetNonExistingTable() { public void testCreateAndGetTable() { String tableName = "test_create_and_get_table"; TableId tableId = TableId.of(DATASET, tableName); - BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, TABLE_SCHEMA)); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); - BaseTableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); + TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo instanceof TableInfo); + assertTrue(remoteTableInfo.definition() instanceof StandardTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableInfo.Type.TABLE, remoteTableInfo.type()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.schema()); + assertEquals(TableDefinition.Type.TABLE, remoteTableInfo.definition().type()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); assertNotNull(remoteTableInfo.creationTime()); assertNotNull(remoteTableInfo.lastModifiedTime()); - assertNotNull(remoteTableInfo.numBytes()); - assertNotNull(remoteTableInfo.numRows()); + assertNotNull(remoteTableInfo.definition().numBytes()); + assertNotNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -278,21 +279,22 @@ public void testCreateAndGetTable() { public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); - BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, TABLE_SCHEMA)); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); - BaseTableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName, + TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName, TableOption.fields(TableField.CREATION_TIME)); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo instanceof TableInfo); + assertTrue(remoteTableInfo.definition() instanceof StandardTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableInfo.Type.TABLE, remoteTableInfo.type()); + assertEquals(TableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertNotNull(remoteTableInfo.creationTime()); - assertNull(remoteTableInfo.schema()); + assertNull(remoteTableInfo.definition().schema()); assertNull(remoteTableInfo.lastModifiedTime()); - assertNull(remoteTableInfo.numBytes()); - assertNull(remoteTableInfo.numRows()); + assertNull(remoteTableInfo.definition().numBytes()); + assertNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -300,18 +302,18 @@ public void testCreateAndGetTableWithSelectedField() { public void testCreateExternalTable() throws InterruptedException { String tableName = "test_create_external_table"; TableId tableId = TableId.of(DATASET, tableName); - ExternalDataConfiguration externalDataConfiguration = ExternalDataConfiguration.of( + ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of( "gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json()); - BaseTableInfo tableInfo = ExternalTableInfo.of(tableId, externalDataConfiguration); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); - BaseTableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); + TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo instanceof ExternalTableInfo); + assertTrue(remoteTableInfo.definition() instanceof ExternalTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.schema()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); QueryRequest request = QueryRequest.builder( "SELECT TimestampField, StringField, IntegerField, BooleanField FROM " + DATASET + "." + tableName) @@ -350,17 +352,18 @@ public void testCreateExternalTable() throws InterruptedException { public void testCreateViewTable() throws InterruptedException { String tableName = "test_create_view_table"; TableId tableId = TableId.of(DATASET, tableName); - BaseTableInfo tableInfo = ViewInfo.of(tableId, - "SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + ViewDefinition viewDefinition = + ViewDefinition.of("SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + TABLE_ID.table()); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + TableInfo tableInfo = TableInfo.of(tableId, viewDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); - BaseTableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); + TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertTrue(remoteTableInfo instanceof ViewInfo); + assertTrue(remoteTableInfo.definition() instanceof ViewDefinition); Schema expectedSchema = Schema.builder() .addField( Field.builder("TimestampField", Field.Type.timestamp()) @@ -375,7 +378,7 @@ public void testCreateViewTable() throws InterruptedException { .mode(Field.Mode.NULLABLE) .build()) .build(); - assertEquals(expectedSchema, remoteTableInfo.schema()); + assertEquals(expectedSchema, remoteTableInfo.definition().schema()); QueryRequest request = QueryRequest.builder("SELECT * FROM " + tableName) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) @@ -406,12 +409,13 @@ public void testCreateViewTable() throws InterruptedException { @Test public void testListTables() { String tableName = "test_list_tables"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); - Page tables = bigquery.listTables(DATASET); + Page tables = bigquery.listTables(DATASET); boolean found = false; - Iterator tableIterator = tables.values().iterator(); + Iterator tableIterator = tables.values().iterator(); while (tableIterator.hasNext() && !found) { if (tableIterator.next().tableId().equals(createdTableInfo.tableId())) { found = true; @@ -424,14 +428,15 @@ public void testListTables() { @Test public void testUpdateTable() { String tableName = "test_update_table"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); - BaseTableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder() + TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder() .description("newDescription").build()); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); - assertEquals(TABLE_SCHEMA, updatedTableInfo.schema()); + assertEquals(TABLE_SCHEMA, updatedTableInfo.definition().schema()); assertEquals("newDescription", updatedTableInfo.description()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -439,26 +444,27 @@ public void testUpdateTable() { @Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); - BaseTableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder().description("newDescr") + TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder().description("newDescr") .build(), TableOption.fields(TableField.DESCRIPTION)); - assertTrue(updatedTableInfo instanceof TableInfo); + assertTrue(updatedTableInfo.definition() instanceof StandardTableDefinition); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); assertEquals("newDescr", updatedTableInfo.description()); - assertNull(updatedTableInfo.schema()); + assertNull(updatedTableInfo.definition().schema()); assertNull(updatedTableInfo.lastModifiedTime()); - assertNull(updatedTableInfo.numBytes()); - assertNull(updatedTableInfo.numRows()); + assertNull(updatedTableInfo.definition().numBytes()); + assertNull(updatedTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @Test public void testUpdateNonExistingTable() { - TableInfo tableInfo = - TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), SIMPLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), + StandardTableDefinition.of(SIMPLE_SCHEMA)); try { bigquery.update(tableInfo); fail("BigQueryException was expected"); @@ -478,7 +484,8 @@ public void testDeleteNonExistingTable() { @Test public void testInsertAll() { String tableName = "test_insert_all_table"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) .addRow(ImmutableMap.of( @@ -509,7 +516,8 @@ public void testInsertAll() { @Test public void testInsertAllWithSuffix() throws InterruptedException { String tableName = "test_insert_all_with_suffix_table"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) .addRow(ImmutableMap.of( @@ -536,7 +544,7 @@ public void testInsertAllWithSuffix() throws InterruptedException { assertFalse(response.hasErrors()); assertEquals(0, response.insertErrors().size()); String newTableName = tableName + "_suffix"; - BaseTableInfo suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields()); + TableInfo suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields()); // wait until the new table is created. If the table is never created the test will time-out while (suffixTable == null) { Thread.sleep(1000L); @@ -549,7 +557,8 @@ public void testInsertAllWithSuffix() throws InterruptedException { @Test public void testInsertAllWithErrors() { String tableName = "test_insert_all_with_errors_table"; - BaseTableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) .addRow(ImmutableMap.of( @@ -680,8 +689,9 @@ public void testCreateAndGetJob() throws InterruptedException { String sourceTableName = "test_create_and_get_job_source_table"; String destinationTableName = "test_create_and_get_job_destination_table"; TableId sourceTable = TableId.of(DATASET, sourceTableName); - BaseTableInfo tableInfo = TableInfo.of(sourceTable, SIMPLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(sourceTableName, createdTableInfo.tableId().table()); @@ -712,8 +722,9 @@ public void testCreateAndGetJobWithSelectedFields() throws InterruptedException String sourceTableName = "test_create_and_get_job_with_selected_fields_source_table"; String destinationTableName = "test_create_and_get_job_with_selected_fields_destination_table"; TableId sourceTable = TableId.of(DATASET, sourceTableName); - BaseTableInfo tableInfo = TableInfo.of(sourceTable, SIMPLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(sourceTableName, createdTableInfo.tableId().table()); @@ -751,8 +762,9 @@ public void testCopyJob() throws InterruptedException { String sourceTableName = "test_copy_job_source_table"; String destinationTableName = "test_copy_job_destination_table"; TableId sourceTable = TableId.of(DATASET, sourceTableName); - BaseTableInfo tableInfo = TableInfo.of(sourceTable, SIMPLE_SCHEMA); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); + TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(sourceTableName, createdTableInfo.tableId().table()); @@ -764,11 +776,11 @@ public void testCopyJob() throws InterruptedException { remoteJob = bigquery.getJob(remoteJob.jobId()); } assertNull(remoteJob.status().error()); - BaseTableInfo remoteTableInfo = bigquery.getTable(DATASET, destinationTableName); + TableInfo remoteTableInfo = bigquery.getTable(DATASET, destinationTableName); assertNotNull(remoteTableInfo); assertEquals(destinationTable.dataset(), remoteTableInfo.tableId().dataset()); assertEquals(destinationTableName, remoteTableInfo.tableId().table()); - assertEquals(SIMPLE_SCHEMA, remoteTableInfo.schema()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); assertTrue(bigquery.delete(DATASET, sourceTableName)); assertTrue(bigquery.delete(DATASET, destinationTableName)); } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/InsertAllRequestTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/InsertAllRequestTest.java index d2e1de14a571..0866f0b9349e 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/InsertAllRequestTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/InsertAllRequestTest.java @@ -43,7 +43,8 @@ public class InsertAllRequestTest { InsertAllRequest.RowToInsert.of("id2", CONTENT2)); private static final TableId TABLE_ID = TableId.of("dataset", "table"); private static final Schema TABLE_SCHEMA = Schema.of(); - private static final BaseTableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_SCHEMA); + private static final TableDefinition TABLE_DEFINITION = StandardTableDefinition.of(TABLE_SCHEMA); + private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_DEFINITION); private static final boolean SKIP_INVALID_ROWS = true; private static final boolean IGNORE_UNKNOWN_VALUES = false; private static final String TEMPLATE_SUFFIX = "templateSuffix"; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java index 96bf8d1838c4..260088470aff 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/JobInfoTest.java @@ -118,12 +118,12 @@ public class JobInfoTest { private static final Integer MAX_BAD_RECORDS = 42; private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final CsvOptions CSV_OPTIONS = CsvOptions.builder().build(); - private static final ExternalDataConfiguration TABLE_CONFIGURATION = ExternalDataConfiguration - .builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) - .compression(COMPRESSION) - .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) - .maxBadRecords(MAX_BAD_RECORDS) - .build(); + private static final ExternalTableDefinition TABLE_CONFIGURATION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + .compression(COMPRESSION) + .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) + .maxBadRecords(MAX_BAD_RECORDS) + .build(); private static final LoadJobConfiguration LOAD_CONFIGURATION = LoadJobConfiguration.builder(TABLE_ID, SOURCE_URIS) .createDisposition(CREATE_DISPOSITION) @@ -135,7 +135,7 @@ public class JobInfoTest { .schema(TABLE_SCHEMA) .build(); private static final String QUERY = "BigQuery SQL"; - private static final Map TABLE_DEFINITIONS = + private static final Map TABLE_DEFINITIONS = ImmutableMap.of("tableName", TABLE_CONFIGURATION); private static final QueryJobConfiguration.Priority PRIORITY = QueryJobConfiguration.Priority.BATCH; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryJobConfigurationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryJobConfigurationTest.java index 69b2f992fe22..1ef270ee69cf 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryJobConfigurationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/QueryJobConfigurationTest.java @@ -58,13 +58,13 @@ public class QueryJobConfigurationTest { private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final String COMPRESSION = "GZIP"; private static final CsvOptions CSV_OPTIONS = CsvOptions.builder().build(); - private static final ExternalDataConfiguration TABLE_CONFIGURATION = ExternalDataConfiguration - .builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) - .compression(COMPRESSION) - .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) - .maxBadRecords(MAX_BAD_RECORDS) - .build(); - private static final Map TABLE_DEFINITIONS = + private static final ExternalTableDefinition TABLE_CONFIGURATION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + .compression(COMPRESSION) + .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) + .maxBadRecords(MAX_BAD_RECORDS) + .build(); + private static final Map TABLE_DEFINITIONS = ImmutableMap.of("tableName", TABLE_CONFIGURATION); private static final CreateDisposition CREATE_DISPOSITION = CreateDisposition.CREATE_IF_NEEDED; private static final WriteDisposition WRITE_DISPOSITION = WriteDisposition.WRITE_APPEND; diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 19b281f073b3..0c54ea627a67 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -25,7 +25,7 @@ import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; -import com.google.gcloud.bigquery.TableInfo.StreamingBuffer; +import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; import org.junit.Test; @@ -99,8 +99,8 @@ public class SerializationTest { private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); private static final StreamingBuffer STREAMING_BUFFER = new StreamingBuffer(1L, 2L, 3L); private static final List SOURCE_URIS = ImmutableList.of("uri1", "uri2"); - private static final ExternalDataConfiguration EXTERNAL_DATA_CONFIGURATION = - ExternalDataConfiguration.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .ignoreUnknownValues(true) .maxBadRecords(42) .build(); @@ -108,24 +108,26 @@ public class SerializationTest { new UserDefinedFunction.InlineFunction("inline"); private static final UserDefinedFunction URI_FUNCTION = new UserDefinedFunction.UriFunction("URI"); - private static final BaseTableInfo TABLE_INFO = - TableInfo.builder(TABLE_ID, TABLE_SCHEMA) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .id(ID) - .location(LOCATION) - .streamingBuffer(STREAMING_BUFFER) - .build(); - private static final ViewInfo VIEW_INFO = - ViewInfo.builder(TABLE_ID, "QUERY") - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .id(ID) - .build(); - private static final ExternalTableInfo EXTERNAL_TABLE_INFO = - ExternalTableInfo.builder(TABLE_ID, EXTERNAL_DATA_CONFIGURATION) + private static final TableDefinition TABLE_DEFINITION = StandardTableDefinition.builder() + .schema(TABLE_SCHEMA) + .location(LOCATION) + .streamingBuffer(STREAMING_BUFFER) + .build(); + private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_DEFINITION) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .id(ID) + .build(); + private static final TableDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY"); + private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_DEFINITION) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .id(ID) + .build(); + private static final TableInfo EXTERNAL_TABLE_INFO = + TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -244,10 +246,11 @@ public void testServiceOptions() throws Exception { @Test public void testModelAndRequests() throws Exception { Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, - DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, EXTERNAL_DATA_CONFIGURATION, - TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, - JOB_STATISTICS, EXTRACT_STATISTICS, LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, - JOB_STATUS, JOB_ID, COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION, + DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION, + EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, + EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, JOB_STATISTICS, EXTRACT_STATISTICS, + LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, JOB_STATUS, JOB_ID, + COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION, LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST, INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java new file mode 100644 index 000000000000..d1e3635d00cb --- /dev/null +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java @@ -0,0 +1,103 @@ +/* + * 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 org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; + +import org.junit.Test; + +public class TableDefinitionTest { + + private static final Field FIELD_SCHEMA1 = + Field.builder("StringField", Field.Type.string()) + .mode(Field.Mode.NULLABLE) + .description("FieldDescription1") + .build(); + private static final Field FIELD_SCHEMA2 = + Field.builder("IntegerField", Field.Type.integer()) + .mode(Field.Mode.REPEATED) + .description("FieldDescription2") + .build(); + private static final Field FIELD_SCHEMA3 = + Field.builder("RecordField", Field.Type.record(FIELD_SCHEMA1, FIELD_SCHEMA2)) + .mode(Field.Mode.REQUIRED) + .description("FieldDescription3") + .build(); + private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); + private static final Long NUM_BYTES = 42L; + private static final Long NUM_ROWS = 43L; + private static final String LOCATION = "US"; + private static final StreamingBuffer STREAMING_BUFFER = new StreamingBuffer(1L, 2L, 3L); + private static final StandardTableDefinition TABLE_DEFINITION = + StandardTableDefinition.builder() + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .streamingBuffer(STREAMING_BUFFER) + .schema(TABLE_SCHEMA) + .build(); + + @Test + public void testToBuilder() { + compareTableDefinition(TABLE_DEFINITION, TABLE_DEFINITION.toBuilder().build()); + StandardTableDefinition tableDefinition = TABLE_DEFINITION.toBuilder().location("EU").build(); + assertEquals("EU", tableDefinition.location()); + tableDefinition = tableDefinition.toBuilder() + .location(LOCATION) + .build(); + compareTableDefinition(TABLE_DEFINITION, tableDefinition); + } + + @Test + public void testToBuilderIncomplete() { + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); + assertEquals(tableDefinition, tableDefinition.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.type()); + assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.schema()); + assertEquals(LOCATION, TABLE_DEFINITION.location()); + assertEquals(NUM_BYTES, TABLE_DEFINITION.numBytes()); + assertEquals(NUM_ROWS, TABLE_DEFINITION.numRows()); + assertEquals(STREAMING_BUFFER, TABLE_DEFINITION.streamingBuffer()); + } + + @Test + public void testToAndFromPb() { + assertTrue(TableDefinition.fromPb(TABLE_DEFINITION.toPb()) instanceof StandardTableDefinition); + compareTableDefinition(TABLE_DEFINITION, + TableDefinition.fromPb(TABLE_DEFINITION.toPb())); + } + + private void compareTableDefinition(StandardTableDefinition expected, + StandardTableDefinition value) { + assertEquals(expected, value); + assertEquals(expected.schema(), value.schema()); + assertEquals(expected.type(), value.type()); + assertEquals(expected.numBytes(), value.numBytes()); + assertEquals(expected.numRows(), value.numRows()); + assertEquals(expected.location(), value.location()); + assertEquals(expected.streamingBuffer(), value.streamingBuffer()); + assertEquals(expected.type(), value.type()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java index 7326f6c51b95..18b8be10d71e 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableInfoTest.java @@ -17,10 +17,8 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; -import com.google.gcloud.bigquery.TableInfo.StreamingBuffer; import org.junit.Test; @@ -28,6 +26,16 @@ public class TableInfoTest { + private static final String ETAG = "etag"; + private static final String ID = "project:dataset:table"; + private static final String SELF_LINK = "selfLink"; + private static final TableId TABLE_ID = TableId.of("dataset", "table"); + private static final String FRIENDLY_NAME = "friendlyName"; + private static final String DESCRIPTION = "description"; + private static final Long CREATION_TIME = 10L; + private static final Long EXPIRATION_TIME = 100L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final Field FIELD_SCHEMA1 = Field.builder("StringField", Field.Type.string()) .mode(Field.Mode.NULLABLE) @@ -44,62 +52,59 @@ public class TableInfoTest { .description("FieldDescription3") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); - private static final String VIEW_QUERY = "VIEW QUERY"; + private static final Long NUM_BYTES = 42L; + private static final Long NUM_ROWS = 43L; + private static final String LOCATION = "US"; + private static final StandardTableDefinition.StreamingBuffer STREAMING_BUFFER = + new StandardTableDefinition.StreamingBuffer(1L, 2L, 3L); + private static final StandardTableDefinition TABLE_DEFINITION = StandardTableDefinition.builder() + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .streamingBuffer(STREAMING_BUFFER) + .schema(TABLE_SCHEMA) + .build(); + private static final List SOURCE_URIS = ImmutableList.of("uri1", "uri2"); private static final Integer MAX_BAD_RECORDS = 42; private static final Boolean IGNORE_UNKNOWN_VALUES = true; private static final String COMPRESSION = "GZIP"; - private static final ExternalDataConfiguration CONFIGURATION = ExternalDataConfiguration - .builder(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.datastoreBackup()) - .compression(COMPRESSION) - .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) - .maxBadRecords(MAX_BAD_RECORDS) - .build(); - private static final String ETAG = "etag"; - private static final String ID = "project:dataset:table"; - private static final String SELF_LINK = "selfLink"; - private static final TableId TABLE_ID = TableId.of("dataset", "table"); - private static final String FRIENDLY_NAME = "friendlyName"; - private static final String DESCRIPTION = "description"; - private static final Long NUM_BYTES = 42L; - private static final Long NUM_ROWS = 43L; - private static final Long CREATION_TIME = 10L; - private static final Long EXPIRATION_TIME = 100L; - private static final Long LAST_MODIFIED_TIME = 20L; - private static final String LOCATION = "US"; - private static final StreamingBuffer STREAMING_BUFFER = new StreamingBuffer(1L, 2L, 3L); - private static final TableInfo TABLE_INFO = - TableInfo.builder(TABLE_ID, TABLE_SCHEMA) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .expirationTime(EXPIRATION_TIME) - .friendlyName(FRIENDLY_NAME) - .id(ID) - .lastModifiedTime(LAST_MODIFIED_TIME) - .location(LOCATION) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) - .selfLink(SELF_LINK) - .streamingBuffer(STREAMING_BUFFER) - .build(); - private static final ExternalTableInfo EXTERNAL_TABLE_INFO = - ExternalTableInfo.builder(TABLE_ID, CONFIGURATION) - .creationTime(CREATION_TIME) - .description(DESCRIPTION) - .etag(ETAG) - .expirationTime(EXPIRATION_TIME) - .friendlyName(FRIENDLY_NAME) - .id(ID) - .lastModifiedTime(LAST_MODIFIED_TIME) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) - .selfLink(SELF_LINK) + private static final CsvOptions CSV_OPTIONS = CsvOptions.builder().build(); + private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + .compression(COMPRESSION) + .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) + .maxBadRecords(MAX_BAD_RECORDS) .build(); + + private static final String VIEW_QUERY = "VIEW QUERY"; private static final List USER_DEFINED_FUNCTIONS = ImmutableList.of(UserDefinedFunction.inline("Function"), UserDefinedFunction.fromUri("URI")); - private static final ViewInfo VIEW_INFO = - ViewInfo.builder(TABLE_ID, VIEW_QUERY, USER_DEFINED_FUNCTIONS) + private static final ViewDefinition VIEW_TYPE = + ViewDefinition.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + + private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_DEFINITION) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .expirationTime(EXPIRATION_TIME) + .friendlyName(FRIENDLY_NAME) + .id(ID) + .lastModifiedTime(LAST_MODIFIED_TIME) + .selfLink(SELF_LINK) + .build(); + private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_TYPE) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .expirationTime(EXPIRATION_TIME) + .friendlyName(FRIENDLY_NAME) + .id(ID) + .lastModifiedTime(LAST_MODIFIED_TIME) + .selfLink(SELF_LINK) + .build(); + private static final TableInfo EXTERNAL_TABLE_INFO = + TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -107,40 +112,37 @@ public class TableInfoTest { .friendlyName(FRIENDLY_NAME) .id(ID) .lastModifiedTime(LAST_MODIFIED_TIME) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) .selfLink(SELF_LINK) .build(); @Test public void testToBuilder() { compareTableInfo(TABLE_INFO, TABLE_INFO.toBuilder().build()); - compareViewInfo(VIEW_INFO, VIEW_INFO.toBuilder().build()); - compareExternalTableInfo(EXTERNAL_TABLE_INFO, EXTERNAL_TABLE_INFO.toBuilder().build()); - BaseTableInfo tableInfo = TABLE_INFO.toBuilder() + compareTableInfo(VIEW_INFO, VIEW_INFO.toBuilder().build()); + compareTableInfo(EXTERNAL_TABLE_INFO, EXTERNAL_TABLE_INFO.toBuilder().build()); + TableInfo tableInfo = TABLE_INFO.toBuilder() .description("newDescription") .build(); assertEquals("newDescription", tableInfo.description()); tableInfo = tableInfo.toBuilder() .description("description") .build(); - compareBaseTableInfo(TABLE_INFO, tableInfo); + compareTableInfo(TABLE_INFO, tableInfo); } @Test public void testToBuilderIncomplete() { - BaseTableInfo tableInfo = TableInfo.of(TABLE_ID, TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TABLE_ID, TABLE_DEFINITION); assertEquals(tableInfo, tableInfo.toBuilder().build()); - tableInfo = ViewInfo.of(TABLE_ID, VIEW_QUERY); + tableInfo = TableInfo.of(TABLE_ID, VIEW_TYPE); assertEquals(tableInfo, tableInfo.toBuilder().build()); - tableInfo = ExternalTableInfo.of(TABLE_ID, CONFIGURATION); + tableInfo = TableInfo.of(TABLE_ID, EXTERNAL_TABLE_DEFINITION); assertEquals(tableInfo, tableInfo.toBuilder().build()); } @Test public void testBuilder() { assertEquals(TABLE_ID, TABLE_INFO.tableId()); - assertEquals(TABLE_SCHEMA, TABLE_INFO.schema()); assertEquals(CREATION_TIME, TABLE_INFO.creationTime()); assertEquals(DESCRIPTION, TABLE_INFO.description()); assertEquals(ETAG, TABLE_INFO.etag()); @@ -148,16 +150,10 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, TABLE_INFO.friendlyName()); assertEquals(ID, TABLE_INFO.id()); assertEquals(LAST_MODIFIED_TIME, TABLE_INFO.lastModifiedTime()); - assertEquals(LOCATION, TABLE_INFO.location()); - assertEquals(NUM_BYTES, TABLE_INFO.numBytes()); - assertEquals(NUM_ROWS, TABLE_INFO.numRows()); + assertEquals(TABLE_DEFINITION, TABLE_INFO.definition()); assertEquals(SELF_LINK, TABLE_INFO.selfLink()); - assertEquals(STREAMING_BUFFER, TABLE_INFO.streamingBuffer()); - assertEquals(BaseTableInfo.Type.TABLE, TABLE_INFO.type()); assertEquals(TABLE_ID, VIEW_INFO.tableId()); - assertEquals(null, VIEW_INFO.schema()); - assertEquals(VIEW_QUERY, VIEW_INFO.query()); - assertEquals(BaseTableInfo.Type.VIEW, VIEW_INFO.type()); + assertEquals(VIEW_TYPE, VIEW_INFO.definition()); assertEquals(CREATION_TIME, VIEW_INFO.creationTime()); assertEquals(DESCRIPTION, VIEW_INFO.description()); assertEquals(ETAG, VIEW_INFO.etag()); @@ -165,13 +161,9 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, VIEW_INFO.friendlyName()); assertEquals(ID, VIEW_INFO.id()); assertEquals(LAST_MODIFIED_TIME, VIEW_INFO.lastModifiedTime()); - assertEquals(NUM_BYTES, VIEW_INFO.numBytes()); - assertEquals(NUM_ROWS, VIEW_INFO.numRows()); + assertEquals(VIEW_TYPE, VIEW_INFO.definition()); assertEquals(SELF_LINK, VIEW_INFO.selfLink()); - assertEquals(BaseTableInfo.Type.VIEW, VIEW_INFO.type()); assertEquals(TABLE_ID, EXTERNAL_TABLE_INFO.tableId()); - assertEquals(null, EXTERNAL_TABLE_INFO.schema()); - assertEquals(CONFIGURATION, EXTERNAL_TABLE_INFO.configuration()); assertEquals(CREATION_TIME, EXTERNAL_TABLE_INFO.creationTime()); assertEquals(DESCRIPTION, EXTERNAL_TABLE_INFO.description()); assertEquals(ETAG, EXTERNAL_TABLE_INFO.etag()); @@ -179,21 +171,15 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, EXTERNAL_TABLE_INFO.friendlyName()); assertEquals(ID, EXTERNAL_TABLE_INFO.id()); assertEquals(LAST_MODIFIED_TIME, EXTERNAL_TABLE_INFO.lastModifiedTime()); - assertEquals(NUM_BYTES, EXTERNAL_TABLE_INFO.numBytes()); - assertEquals(NUM_ROWS, EXTERNAL_TABLE_INFO.numRows()); + assertEquals(EXTERNAL_TABLE_DEFINITION, EXTERNAL_TABLE_INFO.definition()); assertEquals(SELF_LINK, EXTERNAL_TABLE_INFO.selfLink()); - assertEquals(BaseTableInfo.Type.EXTERNAL, EXTERNAL_TABLE_INFO.type()); } @Test public void testToAndFromPb() { - assertTrue(BaseTableInfo.fromPb(TABLE_INFO.toPb()) instanceof TableInfo); - compareTableInfo(TABLE_INFO, BaseTableInfo.fromPb(TABLE_INFO.toPb())); - assertTrue(BaseTableInfo.fromPb(VIEW_INFO.toPb()) instanceof ViewInfo); - compareViewInfo(VIEW_INFO, BaseTableInfo.fromPb(VIEW_INFO.toPb())); - assertTrue(BaseTableInfo.fromPb(EXTERNAL_TABLE_INFO.toPb()) instanceof ExternalTableInfo); - compareExternalTableInfo(EXTERNAL_TABLE_INFO, - BaseTableInfo.fromPb(EXTERNAL_TABLE_INFO.toPb())); + compareTableInfo(TABLE_INFO, TableInfo.fromPb(TABLE_INFO.toPb())); + compareTableInfo(VIEW_INFO, TableInfo.fromPb(VIEW_INFO.toPb())); + compareTableInfo(EXTERNAL_TABLE_INFO, TableInfo.fromPb(EXTERNAL_TABLE_INFO.toPb())); } @Test @@ -203,11 +189,10 @@ public void testSetProjectId() { assertEquals("project", VIEW_INFO.setProjectId("project").tableId().project()); } - private void compareBaseTableInfo(BaseTableInfo expected, BaseTableInfo value) { + private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected, value); assertEquals(expected.tableId(), value.tableId()); - assertEquals(expected.schema(), value.schema()); - assertEquals(expected.type(), value.type()); + assertEquals(expected.definition(), value.definition()); assertEquals(expected.creationTime(), value.creationTime()); assertEquals(expected.description(), value.description()); assertEquals(expected.etag(), value.etag()); @@ -215,29 +200,8 @@ private void compareBaseTableInfo(BaseTableInfo expected, BaseTableInfo value) { assertEquals(expected.friendlyName(), value.friendlyName()); assertEquals(expected.id(), value.id()); assertEquals(expected.lastModifiedTime(), value.lastModifiedTime()); - assertEquals(expected.numBytes(), value.numBytes()); - assertEquals(expected.numRows(), value.numRows()); assertEquals(expected.selfLink(), value.selfLink()); - assertEquals(expected.type(), value.type()); - } - - private void compareTableInfo(TableInfo expected, TableInfo value) { - compareBaseTableInfo(expected, value); - assertEquals(expected, value); - assertEquals(expected.location(), value.location()); - assertEquals(expected.streamingBuffer(), value.streamingBuffer()); - } - - private void compareViewInfo(ViewInfo expected, ViewInfo value) { - compareBaseTableInfo(expected, value); - assertEquals(expected, value); - assertEquals(expected.query(), value.query()); - assertEquals(expected.userDefinedFunctions(), value.userDefinedFunctions()); - } - - private void compareExternalTableInfo(ExternalTableInfo expected, ExternalTableInfo value) { - compareBaseTableInfo(expected, value); - assertEquals(expected, value); - assertEquals(expected.configuration(), value.configuration()); + assertEquals(expected.definition(), value.definition()); + assertEquals(expected.hashCode(), value.hashCode()); } } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java index 2d0b7e528750..3b16593a7f79 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableTest.java @@ -55,7 +55,9 @@ public class TableTest { private static final JobInfo EXTRACT_JOB_INFO = JobInfo.of(ExtractJobConfiguration.of(TABLE_ID1, ImmutableList.of("URI"), "CSV")); private static final Field FIELD = Field.of("FieldName", Field.Type.integer()); - private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, Schema.of(FIELD)); + private static final TableDefinition TABLE_DEFINITION = + StandardTableDefinition.of(Schema.of(FIELD)); + private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, TABLE_DEFINITION); private static final List ROWS_TO_INSERT = ImmutableList.of( RowToInsert.of("id1", ImmutableMap.of("key", "val1")), RowToInsert.of("id2", ImmutableMap.of("key", "val2"))); @@ -149,7 +151,7 @@ public void testReloadWithOptions() throws Exception { @Test public void testUpdate() throws Exception { - BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); + TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); expect(bigquery.update(updatedInfo)).andReturn(updatedInfo); replay(bigquery); Table updatedTable = table.update(updatedInfo); @@ -181,7 +183,7 @@ public void testUpdateWithDifferentDatasetId() throws Exception { @Test public void testUpdateWithOptions() throws Exception { - BaseTableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); + TableInfo updatedInfo = TABLE_INFO.toBuilder().description("Description").build(); expect(bigquery.update(updatedInfo, BigQuery.TableOption.fields())).andReturn(updatedInfo); replay(bigquery); Table updatedTable = table.update(updatedInfo, BigQuery.TableOption.fields()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java new file mode 100644 index 000000000000..ebab7a6e87ca --- /dev/null +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java @@ -0,0 +1,75 @@ +/* + * 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 org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; + +import java.util.List; + +public class ViewDefinitionTest { + + private static final String VIEW_QUERY = "VIEW QUERY"; + private static final List USER_DEFINED_FUNCTIONS = + ImmutableList.of(UserDefinedFunction.inline("Function"), UserDefinedFunction.fromUri("URI")); + private static final ViewDefinition VIEW_DEFINITION = + ViewDefinition.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + + @Test + public void testToBuilder() { + compareViewDefinition(VIEW_DEFINITION, VIEW_DEFINITION.toBuilder().build()); + ViewDefinition viewDefinition = VIEW_DEFINITION.toBuilder() + .query("NEW QUERY") + .build(); + assertEquals("NEW QUERY", viewDefinition.query()); + viewDefinition = viewDefinition.toBuilder() + .query(VIEW_QUERY) + .build(); + compareViewDefinition(VIEW_DEFINITION, viewDefinition); + } + + @Test + public void testToBuilderIncomplete() { + TableDefinition viewDefinition = ViewDefinition.of(VIEW_QUERY); + assertEquals(viewDefinition, viewDefinition.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(VIEW_QUERY, VIEW_DEFINITION.query()); + assertEquals(TableDefinition.Type.VIEW, VIEW_DEFINITION.type()); + assertEquals(USER_DEFINED_FUNCTIONS, VIEW_DEFINITION.userDefinedFunctions()); + } + + @Test + public void testToAndFromPb() { + assertTrue(TableDefinition.fromPb(VIEW_DEFINITION.toPb()) instanceof ViewDefinition); + compareViewDefinition(VIEW_DEFINITION, + TableDefinition.fromPb(VIEW_DEFINITION.toPb())); + } + + private void compareViewDefinition(ViewDefinition expected, ViewDefinition value) { + assertEquals(expected, value); + assertEquals(expected.query(), value.query()); + assertEquals(expected.userDefinedFunctions(), value.userDefinedFunctions()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/WriteChannelConfigurationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/WriteChannelConfigurationTest.java index 17fa8446d097..dfde4795dacd 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/WriteChannelConfigurationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/WriteChannelConfigurationTest.java @@ -47,15 +47,16 @@ public class WriteChannelConfigurationTest { .description("FieldDescription") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA); - private static final WriteChannelConfiguration LOAD_CONFIGURATION = WriteChannelConfiguration.builder(TABLE_ID) - .createDisposition(CREATE_DISPOSITION) - .writeDisposition(WRITE_DISPOSITION) - .formatOptions(CSV_OPTIONS) - .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) - .maxBadRecords(MAX_BAD_RECORDS) - .projectionFields(PROJECTION_FIELDS) - .schema(TABLE_SCHEMA) - .build(); + private static final WriteChannelConfiguration LOAD_CONFIGURATION = + WriteChannelConfiguration.builder(TABLE_ID) + .createDisposition(CREATE_DISPOSITION) + .writeDisposition(WRITE_DISPOSITION) + .formatOptions(CSV_OPTIONS) + .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) + .maxBadRecords(MAX_BAD_RECORDS) + .projectionFields(PROJECTION_FIELDS) + .schema(TABLE_SCHEMA) + .build(); @Test public void testToBuilder() { @@ -106,7 +107,8 @@ public void testToPbAndFromPb() { compareLoadConfiguration(configuration, WriteChannelConfiguration.fromPb(configuration.toPb())); } - private void compareLoadConfiguration(WriteChannelConfiguration expected, WriteChannelConfiguration value) { + private void compareLoadConfiguration(WriteChannelConfiguration expected, + WriteChannelConfiguration value) { assertEquals(expected, value); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java index 8fe78cbd50ad..1b1478eb5be5 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java @@ -18,15 +18,13 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.WriteChannel; -import com.google.gcloud.bigquery.BaseTableInfo; import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryError; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.CopyJobConfiguration; import com.google.gcloud.bigquery.DatasetId; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.ExternalDataConfiguration; -import com.google.gcloud.bigquery.ExternalTableInfo; +import com.google.gcloud.bigquery.ExternalTableDefinition; import com.google.gcloud.bigquery.ExtractJobConfiguration; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; @@ -34,14 +32,15 @@ import com.google.gcloud.bigquery.JobId; import com.google.gcloud.bigquery.JobInfo; import com.google.gcloud.bigquery.JobStatus; -import com.google.gcloud.bigquery.WriteChannelConfiguration; import com.google.gcloud.bigquery.LoadJobConfiguration; import com.google.gcloud.bigquery.QueryRequest; import com.google.gcloud.bigquery.QueryResponse; import com.google.gcloud.bigquery.Schema; +import com.google.gcloud.bigquery.StandardTableDefinition; import com.google.gcloud.bigquery.TableId; import com.google.gcloud.bigquery.TableInfo; -import com.google.gcloud.bigquery.ViewInfo; +import com.google.gcloud.bigquery.ViewDefinition; +import com.google.gcloud.bigquery.WriteChannelConfiguration; import com.google.gcloud.spi.BigQueryRpc.Tuple; import java.nio.channels.FileChannel; @@ -212,7 +211,7 @@ public String params() { private static class ListTablesAction extends DatasetAction { @Override public void run(BigQuery bigquery, DatasetId datasetId) { - Iterator tableInfoIterator = bigquery.listTables(datasetId).iterateAll(); + Iterator tableInfoIterator = bigquery.listTables(datasetId).iterateAll(); while (tableInfoIterator.hasNext()) { System.out.println(tableInfoIterator.next()); } @@ -391,10 +390,10 @@ public void run(BigQuery bigquery, JobId jobId) { } } - private abstract static class CreateTableAction extends BigQueryAction { + private abstract static class CreateTableAction extends BigQueryAction { @Override - void run(BigQuery bigquery, BaseTableInfo table) throws Exception { - BaseTableInfo createTable = bigquery.create(table); + void run(BigQuery bigquery, TableInfo table) throws Exception { + TableInfo createTable = bigquery.create(table); System.out.println("Created table:"); System.out.println(createTable.toString()); } @@ -435,20 +434,20 @@ static Schema parseSchema(String[] args, int start, int end) { } /** - * This class demonstrates how to create a simple BigQuery Table (i.e. a table of type - * {@link BaseTableInfo.Type#TABLE}). + * This class demonstrates how to create a simple BigQuery Table (i.e. a table created from a + * {@link StandardTableDefinition}). * * @see Tables: insert * */ private static class CreateSimpleTableAction extends CreateTableAction { @Override - BaseTableInfo parse(String... args) throws Exception { + TableInfo parse(String... args) throws Exception { if (args.length >= 3) { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - return TableInfo.of(tableId, parseSchema(args, 2, args.length)); + return TableInfo.of(tableId, StandardTableDefinition.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); } @@ -460,23 +459,23 @@ protected String params() { } /** - * This class demonstrates how to create a BigQuery External Table (i.e. a table of type - * {@link BaseTableInfo.Type#EXTERNAL}). + * This class demonstrates how to create a BigQuery External Table (i.e. a table created from a + * {@link ExternalTableDefinition}). * * @see Tables: insert * */ private static class CreateExternalTableAction extends CreateTableAction { @Override - BaseTableInfo parse(String... args) throws Exception { + TableInfo parse(String... args) throws Exception { if (args.length >= 5) { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - ExternalDataConfiguration configuration = - ExternalDataConfiguration.of(args[args.length - 1], + ExternalTableDefinition externalTableDefinition = + ExternalTableDefinition.of(args[args.length - 1], parseSchema(args, 3, args.length - 1), FormatOptions.of(args[2])); - return ExternalTableInfo.of(tableId, configuration); + return TableInfo.of(tableId, externalTableDefinition); } throw new IllegalArgumentException("Missing required arguments."); } @@ -488,22 +487,22 @@ protected String params() { } /** - * This class demonstrates how to create a BigQuery View Table (i.e. a table of type - * {@link BaseTableInfo.Type#VIEW}). + * This class demonstrates how to create a BigQuery View Table (i.e. a table created from a + * {@link ViewDefinition}). * * @see Tables: insert * */ private static class CreateViewAction extends CreateTableAction { @Override - BaseTableInfo parse(String... args) throws Exception { + TableInfo parse(String... args) throws Exception { String message; if (args.length == 3) { String dataset = args[0]; String table = args[1]; String query = args[2]; TableId tableId = TableId.of(dataset, table); - return ViewInfo.of(tableId, query); + return TableInfo.of(tableId, ViewDefinition.of(query)); } else if (args.length < 3) { message = "Missing required dataset id, table id or query."; } else {