From c010bc897e59f096e77128a06f65f3450aaef6da Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 29 Jan 2016 19:39:14 +0100 Subject: [PATCH 1/5] Remove TableInfo hierarchy, add TableType hierarchy --- README.md | 3 +- gcloud-java-bigquery/README.md | 10 +- .../google/gcloud/bigquery/BaseTableInfo.java | 439 ------------------ .../google/gcloud/bigquery/BaseTableType.java | 182 ++++++++ .../com/google/gcloud/bigquery/BigQuery.java | 24 +- .../google/gcloud/bigquery/BigQueryImpl.java | 28 +- .../google/gcloud/bigquery/CsvOptions.java | 2 +- .../com/google/gcloud/bigquery/Dataset.java | 74 +-- .../gcloud/bigquery/DefaultTableType.java | 280 +++++++++++ .../gcloud/bigquery/ExternalTableInfo.java | 149 ------ ...figuration.java => ExternalTableType.java} | 181 ++++---- .../gcloud/bigquery/InsertAllRequest.java | 12 +- .../bigquery/QueryJobConfiguration.java | 14 +- .../com/google/gcloud/bigquery/Table.java | 10 +- .../com/google/gcloud/bigquery/TableInfo.java | 360 +++++++++----- .../bigquery/{ViewInfo.java => ViewType.java} | 91 ++-- .../google/gcloud/bigquery/package-info.java | 5 +- .../gcloud/bigquery/BigQueryImplTest.java | 37 +- .../google/gcloud/bigquery/DatasetTest.java | 89 +--- .../gcloud/bigquery/DefaultTableTypeTest.java | 104 +++++ ...onTest.java => ExternalTableTypeTest.java} | 58 +-- .../gcloud/bigquery/ITBigQueryTest.java | 124 ++--- .../gcloud/bigquery/InsertAllRequestTest.java | 3 +- .../google/gcloud/bigquery/JobInfoTest.java | 14 +- .../bigquery/QueryJobConfigurationTest.java | 14 +- .../gcloud/bigquery/SerializationTest.java | 56 +-- .../google/gcloud/bigquery/TableInfoTest.java | 180 +++---- .../com/google/gcloud/bigquery/TableTest.java | 7 +- .../google/gcloud/bigquery/ViewTypeTest.java | 74 +++ .../gcloud/examples/BigQueryExample.java | 39 +- 30 files changed, 1351 insertions(+), 1312 deletions(-) delete mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableInfo.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java delete mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableInfo.java rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{ExternalDataConfiguration.java => ExternalTableType.java} (71%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{ViewInfo.java => ViewType.java} (65%) create mode 100644 gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java rename gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/{ExternalDataConfigurationTest.java => ExternalTableTypeTest.java} (56%) create mode 100644 gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java diff --git a/README.md b/README.md index 87be133c16fc..cb8d3b4afd8d 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,8 @@ BaseTableInfo 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, DefaultTableType.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..0b77a3907337 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -111,7 +111,7 @@ 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.DefaultTableType; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.Schema; import com.google.gcloud.bigquery.TableId; @@ -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)); +DefaultTableType tableType = DefaultTableType.of(schema); +TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); ``` #### Loading data into a table @@ -204,10 +205,10 @@ 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; +import com.google.gcloud.bigquery.DefaultTableType; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; import com.google.gcloud.bigquery.InsertAllRequest; @@ -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)); + DefaultTableType tableType = DefaultTableType.of(schema); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); // Define rows to insert Map firstRow = new HashMap<>(); 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/BaseTableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.java new file mode 100644 index 000000000000..f61953e9fe03 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.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 BaseTableType implements Serializable{ + + private static final long serialVersionUID = -374760330662959529L; + + /** + * The table type. + */ + public enum Type { + /** + * A normal BigQuery table. Instances of {@code BaseTableType} for this type are implemented by + * {@link DefaultTableType}. + */ + TABLE, + + /** + * A virtual table defined by a SQL query. Instances of {@code BaseTableType} for this type are + * implemented by {@link ViewType}. + * + * @see Views + */ + VIEW, + + /** + * A BigQuery table backed by external data. Instances of {@code BaseTableType} for this type + * are implemented by {@link ExternalTableType}. + * + * @see Federated Data + * Sources + */ + EXTERNAL + } + + private final Type type; + private final Schema schema; + + /** + * 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(BaseTableType tableType) { + this.type = tableType.type; + this.schema = tableType.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(); + } + + BaseTableType(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(BaseTableType 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) DefaultTableType.fromPb(tablePb); + case VIEW: + return (T) ViewType.fromPb(tablePb); + case EXTERNAL: + return (T) ExternalTableType.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..2dea48214ef0 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 + * {@link TableInfo#type()} 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 + * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, + * {@link TableInfo#id()} and {@link TableInfo#type()}). 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 + * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, + * {@link TableInfo#id()} and {@link TableInfo#type()}). 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..4799612ef287 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 ExternalTableType#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..2aabcf85a275 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,22 @@ public Page
list(BigQuery.TableListOption... options) { * @throws BigQueryException upon failure */ public Table get(String table, BigQuery.TableOption... options) { - BaseTableInfo tableInfo = + 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 type the table's type * @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, BaseTableType type, BigQuery.TableOption... options) { + TableInfo tableInfo = TableInfo.of(TableId.of(info.datasetId().dataset(), table), type); return new Table(bigquery, bigquery.create(tableInfo, options)); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java new file mode 100644 index 000000000000..a8b0b30d2db6 --- /dev/null +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java @@ -0,0 +1,280 @@ +/* + * 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 DefaultTableType extends BaseTableType { + + 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 BaseTableType.Builder { + + private Long numBytes; + private Long numRows; + private String location; + private StreamingBuffer streamingBuffer; + + private Builder() { + super(Type.TABLE); + } + + private Builder(DefaultTableType tableType) { + super(tableType); + this.numBytes = tableType.numBytes; + this.numRows = tableType.numRows; + this.location = tableType.location; + this.streamingBuffer = tableType.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 DefaultTableType} object. + */ + @Override + public DefaultTableType build() { + return new DefaultTableType(this); + } + } + + private DefaultTableType(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 DefaultTableType of(Schema schema) { + return builder().schema(schema).build(); + } + + /** + * Returns a builder for the {@code DefaultTableType} 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 DefaultTableType && baseEquals((DefaultTableType) 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 DefaultTableType fromPb(Table tablePb) { + return new Builder(tablePb).build(); + } +} 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/ExternalDataConfiguration.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableType.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/ExternalTableType.java index 4344aeba186b..a9fe31ebb96a 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/ExternalTableType.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,88 @@ 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 ExternalTableType extends BaseTableType { - 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 ExternalTableType apply(ExternalDataConfiguration pb) { + return ExternalTableType.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(ExternalTableType 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 BaseTableType.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(ExternalTableType tableType) { + super(tableType); + this.sourceUris = tableType.sourceUris; + this.formatOptions = tableType.formatOptions; + this.maxBadRecords = tableType.maxBadRecords; + this.ignoreUnknownValues = tableType.ignoreUnknownValues; + this.compression = tableType.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 +114,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 +163,19 @@ public Builder compression(String compression) { } /** - * Creates an {@code ExternalDataConfiguration} object. + * Creates a {@code ExternalTableType} object. */ - public ExternalDataConfiguration build() { - return new ExternalDataConfiguration(this); + @Override + public ExternalTableType build() { + return new ExternalTableType(this); } } - ExternalDataConfiguration(Builder builder) { + private ExternalTableType(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 +212,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 +234,42 @@ public F formatOptions() { } /** - * Returns a builder for the {@code ExternalDataConfiguration} object. + * Returns a builder for the {@code ExternalTableType} 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 ExternalTableType && baseEquals((ExternalTableType) 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 +281,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 +297,7 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() { } /** - * Creates a builder for an ExternalDataConfiguration object. + * Creates a builder for an ExternalTableType 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 +305,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 +316,25 @@ public static Builder builder(List sourceUris, Schema schema, FormatOpti } /** - * Creates a builder for an ExternalDataConfiguration object. + * Creates a builder for an ExternalTableType 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 ExternalTableType 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 +342,41 @@ 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, - FormatOptions format) { + public static ExternalTableType 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 ExternalTableType 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 ExternalTableType fromPb(Table tablePb) { + return new Builder(tablePb).build(); + } + + static ExternalTableType 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/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..000f71b8c067 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);
+            ExternalTableType.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, ExternalTableType 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;
   }
 
@@ -498,7 +498,7 @@ com.google.api.services.bigquery.model.JobConfiguration toPb() {
     }
     if (tableDefinitions != null) {
       queryConfigurationPb.setTableDefinitions(
-          Maps.transformValues(tableDefinitions, ExternalDataConfiguration.TO_PB_FUNCTION));
+          Maps.transformValues(tableDefinitions, ExternalTableType.TO_EXTERNAL_DATA_FUNCTION));
     }
     if (useQueryCache != null) {
       queryConfigurationPb.setUseQueryCache(useQueryCache);
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/TableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java
index aeb1eadd9771..124d78a7b70d 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,318 @@
 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 DefaultTableType} to create simple BigQuery table.
+ * Use {@link ViewType} to create a BigQuery view. Use {@link ExternalTableType} 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 BaseTableType type;
 
   /**
-   * 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 BaseTableType type;
 
-    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.type = tableInfo.type;
     }
 
-    /**
-     * 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.type = BaseTableType.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 type.
+     */
+    public Builder type(BaseTableType type) {
+      this.type = checkNotNull(type);
+      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.type = builder.type;
   }
 
   /**
-   * 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 type.
+   */
+  @SuppressWarnings("unchecked")
+  public  T type() {
+    return (T) type;
+  }
+
+  /**
+   * 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("type", type);
   }
 
   @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 type.
+   */
+  public static Builder builder(TableId tableId, BaseTableType type) {
+    return new Builder().tableId(tableId).type(type);
+  }
+
+  /**
+   * Returns a {@code TableInfo} object given table identity and type.
+   */
+  public static TableInfo of(TableId tableId, BaseTableType type) {
+    return builder(tableId, type).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 = type.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/ViewType.java
similarity index 65%
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/ViewType.java
index 2698921bc034..af85f5005fbb 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/ViewType.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.
@@ -28,33 +28,34 @@
 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 ViewType extends BaseTableType {
 
-  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 BaseTableType.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(ViewType viewType) {
+      super(viewType);
+      this.query = viewType.query;
+      this.userDefinedFunctions = viewType.userDefinedFunctions;
     }
 
-    protected Builder(Table tablePb) {
+    private Builder(Table tablePb) {
       super(tablePb);
       ViewDefinition viewPb = tablePb.getView();
       if (viewPb != null) {
@@ -97,15 +98,15 @@ public Builder userDefinedFunctions(UserDefinedFunction... userDefinedFunctions)
     }
 
     /**
-     * Creates a {@code ViewInfo} object.
+     * Creates a {@code ViewType} object.
      */
     @Override
-    public ViewInfo build() {
-      return new ViewInfo(this);
+    public ViewType build() {
+      return new ViewType(this);
     }
   }
 
-  private ViewInfo(Builder builder) {
+  private ViewType(Builder builder) {
     super(builder);
     this.query = builder.query;
     this.userDefinedFunctions = builder.userDefinedFunctions;
@@ -146,7 +147,7 @@ ToStringHelper toStringHelper() {
 
   @Override
   public boolean equals(Object obj) {
-    return obj instanceof ViewInfo && baseEquals((ViewInfo) obj);
+    return obj instanceof ViewType && baseEquals((ViewType) obj);
   }
 
   @Override
@@ -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 ViewType 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 ViewType 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 ViewType of(String query, UserDefinedFunction... functions) {
+    return builder(query, functions).build();
   }
 
   @SuppressWarnings("unchecked")
-  static ViewInfo fromPb(Table tablePb) {
+  static ViewType 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..f3fb07ea284c 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, DefaultTableType.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..9357f3c4b6a4 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,11 @@ 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 DefaultTableType TABLE_TYPE = DefaultTableType.of(TABLE_SCHEMA);
+  private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE);
+  private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_TYPE);
   private static final TableInfo TABLE_INFO_WITH_PROJECT =
-      TableInfo.of(TABLE_ID_WITH_PROJECT, TABLE_SCHEMA);
+      TableInfo.of(TABLE_ID_WITH_PROJECT, TABLE_TYPE);
   private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION =
       LoadJobConfiguration.of(TABLE_ID, "URI");
   private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION_WITH_PROJECT =
@@ -511,47 +512,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..bd01d0435fa3 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,20 @@
 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 DefaultTableType TABLE_TYPE = DefaultTableType.of(Schema.of(FIELD));
+  private static final ViewType VIEW_TYPE = ViewType.of("QUERY");
+  private static final ExternalTableType EXTERNAL_TABLE_TYPE =
+      ExternalTableType.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv());
+  private static final Iterable TABLE_INFO_RESULTS = ImmutableList.of(
+      TableInfo.builder(TableId.of("dataset", "table1"), TABLE_TYPE).build(),
+      TableInfo.builder(TableId.of("dataset", "table2"), VIEW_TYPE).build(),
+      TableInfo.builder(TableId.of("dataset", "table2"), EXTERNAL_TABLE_TYPE).build());
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
@@ -168,13 +166,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 +186,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 +206,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_TYPE).build(); expect(bigquery.getTable(TableId.of("dataset", "table1"))).andReturn(info); replay(bigquery); Table table = dataset.get("table1"); @@ -225,7 +223,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_TYPE).build(); expect(bigquery.getTable(TableId.of("dataset", "table1"), BigQuery.TableOption.fields())) .andReturn(info); replay(bigquery); @@ -236,70 +234,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_TYPE).build(); expect(bigquery.create(info)).andReturn(info); replay(bigquery); - Table table = dataset.create("table1", Schema.of(FIELD)); + Table table = dataset.create("table1", TABLE_TYPE); 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_TYPE).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_TYPE, BigQuery.TableOption.fields()); assertEquals(info, table.info()); } diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java new file mode 100644 index 000000000000..184cd8b74691 --- /dev/null +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java @@ -0,0 +1,104 @@ +/* + * 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.DefaultTableType.StreamingBuffer; + +import org.junit.Test; + +public class DefaultTableTypeTest { + + 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 DefaultTableType DEFAULT_TABLE_TYPE = DefaultTableType.builder() + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .streamingBuffer(STREAMING_BUFFER) + .schema(TABLE_SCHEMA) + .build(); + + + @Test + public void testToBuilder() { + compareDefaultTableType(DEFAULT_TABLE_TYPE, DEFAULT_TABLE_TYPE.toBuilder().build()); + DefaultTableType tableType = DEFAULT_TABLE_TYPE.toBuilder() + .location("EU") + .build(); + assertEquals("EU", tableType.location()); + tableType = tableType.toBuilder() + .location(LOCATION) + .build(); + compareDefaultTableType(DEFAULT_TABLE_TYPE, tableType); + } + + @Test + public void testToBuilderIncomplete() { + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + assertEquals(tableType, tableType.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(BaseTableType.Type.TABLE, DEFAULT_TABLE_TYPE.type()); + assertEquals(TABLE_SCHEMA, DEFAULT_TABLE_TYPE.schema()); + assertEquals(LOCATION, DEFAULT_TABLE_TYPE.location()); + assertEquals(NUM_BYTES, DEFAULT_TABLE_TYPE.numBytes()); + assertEquals(NUM_ROWS, DEFAULT_TABLE_TYPE.numRows()); + assertEquals(STREAMING_BUFFER, DEFAULT_TABLE_TYPE.streamingBuffer()); + } + + @Test + public void testToAndFromPb() { + assertTrue(BaseTableType.fromPb(DEFAULT_TABLE_TYPE.toPb()) instanceof DefaultTableType); + compareDefaultTableType(DEFAULT_TABLE_TYPE, + BaseTableType.fromPb(DEFAULT_TABLE_TYPE.toPb())); + } + + private void compareDefaultTableType(DefaultTableType expected, DefaultTableType 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/ExternalDataConfigurationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableTypeTest.java similarity index 56% 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/ExternalTableTypeTest.java index f9b7c31e1071..57e7ab18cb26 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/ExternalTableTypeTest.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 ExternalTableTypeTest { private static final List SOURCE_URIS = ImmutableList.of("uri1", "uri2"); private static final Field FIELD_SCHEMA1 = @@ -47,51 +47,52 @@ 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 ExternalTableType EXTERNAL_TABLE_TYPE = + ExternalTableType.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() + compareExternalTableType(EXTERNAL_TABLE_TYPE, EXTERNAL_TABLE_TYPE.toBuilder().build()); + ExternalTableType externalTableType = EXTERNAL_TABLE_TYPE.toBuilder().compression("NONE").build(); + assertEquals("NONE", externalTableType.compression()); + externalTableType = externalTableType.toBuilder() .compression(COMPRESSION) .build(); - compareConfiguration(CONFIGURATION, configuration); + compareExternalTableType(EXTERNAL_TABLE_TYPE, externalTableType); } @Test public void testToBuilderIncomplete() { - ExternalDataConfiguration configuration = - ExternalDataConfiguration.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); - assertEquals(configuration, configuration.toBuilder().build()); + ExternalTableType externalTableType = + ExternalTableType.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); + assertEquals(externalTableType, externalTableType.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(BaseTableType.Type.EXTERNAL, EXTERNAL_TABLE_TYPE.type()); + assertEquals(COMPRESSION, EXTERNAL_TABLE_TYPE.compression()); + assertEquals(CSV_OPTIONS, EXTERNAL_TABLE_TYPE.formatOptions()); + assertEquals(IGNORE_UNKNOWN_VALUES, EXTERNAL_TABLE_TYPE.ignoreUnknownValues()); + assertEquals(MAX_BAD_RECORDS, EXTERNAL_TABLE_TYPE.maxBadRecords()); + assertEquals(TABLE_SCHEMA, EXTERNAL_TABLE_TYPE.schema()); + assertEquals(SOURCE_URIS, EXTERNAL_TABLE_TYPE.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())); + compareExternalTableType(EXTERNAL_TABLE_TYPE, + ExternalTableType.fromPb(EXTERNAL_TABLE_TYPE.toPb())); + ExternalTableType externalTableType = + ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS).build(); + compareExternalTableType(externalTableType, ExternalTableType.fromPb(externalTableType.toPb())); } - private void compareConfiguration(ExternalDataConfiguration expected, - ExternalDataConfiguration value) { + private void compareExternalTableType(ExternalTableType expected, ExternalTableType value) { assertEquals(expected, value); assertEquals(expected.compression(), value.compression()); assertEquals(expected.formatOptions(), value.formatOptions()); @@ -99,5 +100,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..7b58303cadbb 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)); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); 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.type() instanceof DefaultTableType); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableInfo.Type.TABLE, remoteTableInfo.type()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.schema()); + assertEquals(BaseTableType.Type.TABLE, remoteTableInfo.type().type()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.type().schema()); assertNotNull(remoteTableInfo.creationTime()); assertNotNull(remoteTableInfo.lastModifiedTime()); - assertNotNull(remoteTableInfo.numBytes()); - assertNotNull(remoteTableInfo.numRows()); + assertNotNull(remoteTableInfo.type().numBytes()); + assertNotNull(remoteTableInfo.type().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)); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); 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.type() instanceof DefaultTableType); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableInfo.Type.TABLE, remoteTableInfo.type()); + assertEquals(BaseTableType.Type.TABLE, remoteTableInfo.type().type()); assertNotNull(remoteTableInfo.creationTime()); - assertNull(remoteTableInfo.schema()); + assertNull(remoteTableInfo.type().schema()); assertNull(remoteTableInfo.lastModifiedTime()); - assertNull(remoteTableInfo.numBytes()); - assertNull(remoteTableInfo.numRows()); + assertNull(remoteTableInfo.type().numBytes()); + assertNull(remoteTableInfo.type().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( + ExternalTableType externalTableType = ExternalTableType.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, externalTableType); + 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.type() instanceof ExternalTableType); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.schema()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.type().schema()); QueryRequest request = QueryRequest.builder( "SELECT TimestampField, StringField, IntegerField, BooleanField FROM " + DATASET + "." + tableName) @@ -350,17 +352,17 @@ 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 + "." - + TABLE_ID.table()); - BaseTableInfo createdTableInfo = bigquery.create(tableInfo); + ViewType viewType = ViewType.of("SELECT TimestampField, StringField, BooleanField FROM " + + DATASET + "." + TABLE_ID.table()); + TableInfo tableInfo = TableInfo.of(tableId, viewType); + 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.type() instanceof ViewType); Schema expectedSchema = Schema.builder() .addField( Field.builder("TimestampField", Field.Type.timestamp()) @@ -375,7 +377,7 @@ public void testCreateViewTable() throws InterruptedException { .mode(Field.Mode.NULLABLE) .build()) .build(); - assertEquals(expectedSchema, remoteTableInfo.schema()); + assertEquals(expectedSchema, remoteTableInfo.type().schema()); QueryRequest request = QueryRequest.builder("SELECT * FROM " + tableName) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) @@ -406,12 +408,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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + 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 +427,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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + 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.type().schema()); assertEquals("newDescription", updatedTableInfo.description()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -439,26 +443,28 @@ 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + 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.type() instanceof DefaultTableType); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); assertEquals("newDescr", updatedTableInfo.description()); - assertNull(updatedTableInfo.schema()); + assertNull(updatedTableInfo.type().schema()); assertNull(updatedTableInfo.lastModifiedTime()); - assertNull(updatedTableInfo.numBytes()); - assertNull(updatedTableInfo.numRows()); + assertNull(updatedTableInfo.type().numBytes()); + assertNull(updatedTableInfo.type().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"), + DefaultTableType.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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + 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); + DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + 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.type().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..7a47a269dc1a 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 BaseTableType TABLE_TYPE = DefaultTableType.of(TABLE_SCHEMA); + private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE); 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..b4e7a4f1434b 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 ExternalTableType TABLE_CONFIGURATION = + ExternalTableType.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..fd2b9b9bae2f 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 ExternalTableType TABLE_CONFIGURATION = + ExternalTableType.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..6f2ca3f4cbe6 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.DefaultTableType.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 ExternalTableType EXTERNAL_TABLE_TYPE = + ExternalTableType.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 BaseTableType TABLE_TYPE = DefaultTableType.builder() + .schema(TABLE_SCHEMA) + .location(LOCATION) + .streamingBuffer(STREAMING_BUFFER) + .build(); + private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_TYPE) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .id(ID) + .build(); + private static final BaseTableType VIEW_TYPE = ViewType.of("QUERY"); + private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_TYPE) + .creationTime(CREATION_TIME) + .description(DESCRIPTION) + .etag(ETAG) + .id(ID) + .build(); + private static final TableInfo EXTERNAL_TABLE_INFO = + TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_TYPE) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -244,12 +246,12 @@ 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, - LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST, - INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, + DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_TYPE, EXTERNAL_TABLE_TYPE, + VIEW_TYPE, 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(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.maxResults(42L), BigQuery.JobOption.fields(), 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..0df5a9d2c012 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 DefaultTableType.StreamingBuffer STREAMING_BUFFER = + new DefaultTableType.StreamingBuffer(1L, 2L, 3L); + private static final DefaultTableType DEFAULT_TABLE_TYPE = DefaultTableType.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 ExternalTableType EXTERNAL_TABLE_TYPE = + ExternalTableType.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 ViewType VIEW_TYPE = + ViewType.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + + private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, DEFAULT_TABLE_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 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_TYPE) .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, DEFAULT_TABLE_TYPE); 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_TYPE); 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(DEFAULT_TABLE_TYPE, TABLE_INFO.type()); 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.type()); 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.type()); 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_TYPE, EXTERNAL_TABLE_INFO.type()); 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,10 +189,9 @@ 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.creationTime(), value.creationTime()); assertEquals(expected.description(), value.description()); @@ -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.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..4c03cb0ee77a 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,8 @@ 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 BaseTableType TABLE_TYPE = DefaultTableType.of(Schema.of(FIELD)); + private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, TABLE_TYPE); 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 +150,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 +182,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/ViewTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java new file mode 100644 index 000000000000..83a2b011582c --- /dev/null +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java @@ -0,0 +1,74 @@ +/* + * 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 ViewTypeTest { + + 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 ViewType VIEW_TYPE = + ViewType.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + + @Test + public void testToBuilder() { + compareViewType(VIEW_TYPE, VIEW_TYPE.toBuilder().build()); + ViewType viewType = VIEW_TYPE.toBuilder() + .query("NEW QUERY") + .build(); + assertEquals("NEW QUERY", viewType.query()); + viewType = viewType.toBuilder() + .query(VIEW_QUERY) + .build(); + compareViewType(VIEW_TYPE, viewType); + } + + @Test + public void testToBuilderIncomplete() { + BaseTableType tableType = ViewType.of(VIEW_QUERY); + assertEquals(tableType, tableType.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(VIEW_QUERY, VIEW_TYPE.query()); + assertEquals(BaseTableType.Type.VIEW, VIEW_TYPE.type()); + assertEquals(USER_DEFINED_FUNCTIONS, VIEW_TYPE.userDefinedFunctions()); + } + + @Test + public void testToAndFromPb() { + assertTrue(BaseTableType.fromPb(VIEW_TYPE.toPb()) instanceof ViewType); + compareViewType(VIEW_TYPE, BaseTableType.fromPb(VIEW_TYPE.toPb())); + } + + private void compareViewType(ViewType expected, ViewType value) { + assertEquals(expected, value); + assertEquals(expected.query(), value.query()); + assertEquals(expected.userDefinedFunctions(), value.userDefinedFunctions()); + assertEquals(expected.hashCode(), value.hashCode()); + } +} 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..8dc72b5b30a9 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,15 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.WriteChannel; -import com.google.gcloud.bigquery.BaseTableInfo; +import com.google.gcloud.bigquery.DefaultTableType; +import com.google.gcloud.bigquery.ExternalTableType; +import com.google.gcloud.bigquery.TableInfo; 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.ExtractJobConfiguration; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; @@ -34,14 +34,13 @@ import com.google.gcloud.bigquery.JobId; import com.google.gcloud.bigquery.JobInfo; import com.google.gcloud.bigquery.JobStatus; +import com.google.gcloud.bigquery.ViewType; 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.TableId; -import com.google.gcloud.bigquery.TableInfo; -import com.google.gcloud.bigquery.ViewInfo; 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()); } @@ -436,19 +435,19 @@ 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}). + * {@link DefaultTableType}). * * @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, DefaultTableType.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); } @@ -461,22 +460,22 @@ protected String params() { /** * This class demonstrates how to create a BigQuery External Table (i.e. a table of type - * {@link BaseTableInfo.Type#EXTERNAL}). + * {@link ExternalTableType}). * * @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], + ExternalTableType externalTableType = + ExternalTableType.of(args[args.length - 1], parseSchema(args, 3, args.length - 1), FormatOptions.of(args[2])); - return ExternalTableInfo.of(tableId, configuration); + return TableInfo.of(tableId, externalTableType); } throw new IllegalArgumentException("Missing required arguments."); } @@ -489,21 +488,21 @@ protected String params() { /** * This class demonstrates how to create a BigQuery View Table (i.e. a table of type - * {@link BaseTableInfo.Type#VIEW}). + * {@link ViewType}). * * @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, ViewType.of(query)); } else if (args.length < 3) { message = "Missing required dataset id, table id or query."; } else { From 931b4d35405300b9893683ecd5f7a6fc91774003 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 1 Feb 2016 09:51:08 +0100 Subject: [PATCH 2/5] Rename TableType to TableDefinition and other minor fixes --- README.md | 5 +- gcloud-java-bigquery/README.md | 12 +-- ...ableType.java => BaseTableDefinition.java} | 34 +++---- .../com/google/gcloud/bigquery/BigQuery.java | 10 +- .../google/gcloud/bigquery/CsvOptions.java | 2 +- .../com/google/gcloud/bigquery/Dataset.java | 7 +- ...eType.java => DefaultTableDefinition.java} | 33 ++++--- ...Type.java => ExternalTableDefinition.java} | 62 ++++++------ .../bigquery/QueryJobConfiguration.java | 16 +-- .../com/google/gcloud/bigquery/TableInfo.java | 44 ++++----- .../{ViewType.java => ViewDefinition.java} | 36 +++---- .../google/gcloud/bigquery/package-info.java | 2 +- .../gcloud/bigquery/BigQueryImplTest.java | 2 +- .../google/gcloud/bigquery/DatasetTest.java | 27 ++--- ...t.java => DefaultTableDefinitionTest.java} | 58 ++++++----- ....java => ExternalTableDefinitionTest.java} | 52 +++++----- .../gcloud/bigquery/ITBigQueryTest.java | 98 +++++++++---------- .../gcloud/bigquery/InsertAllRequestTest.java | 2 +- .../google/gcloud/bigquery/JobInfoTest.java | 6 +- .../bigquery/QueryJobConfigurationTest.java | 6 +- .../gcloud/bigquery/SerializationTest.java | 10 +- .../google/gcloud/bigquery/TableInfoTest.java | 26 ++--- .../com/google/gcloud/bigquery/TableTest.java | 2 +- ...wTypeTest.java => ViewDefinitionTest.java} | 32 +++--- .../WriteChannelConfigurationTest.java | 22 +++-- .../gcloud/examples/BigQueryExample.java | 32 +++--- 26 files changed, 327 insertions(+), 311 deletions(-) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{BaseTableType.java => BaseTableDefinition.java} (79%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{DefaultTableType.java => DefaultTableDefinition.java} (88%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{ExternalTableType.java => ExternalTableDefinition.java} (88%) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{ViewType.java => ViewDefinition.java} (84%) rename gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/{DefaultTableTypeTest.java => DefaultTableDefinitionTest.java} (58%) rename gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/{ExternalTableTypeTest.java => ExternalTableDefinitionTest.java} (58%) rename gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/{ViewTypeTest.java => ViewDefinitionTest.java} (58%) diff --git a/README.md b/README.md index cb8d3b4afd8d..4c35daa67e87 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,6 @@ 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; @@ -137,12 +136,12 @@ 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()); Schema schema = Schema.of(integerField); - bigquery.create(TableInfo.of(tableId, DefaultTableType.of(schema))); + bigquery.create(TableInfo.of(tableId, DefaultTableDefinition.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 0b77a3907337..e11699a660d6 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -111,7 +111,7 @@ 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.DefaultTableType; +import com.google.gcloud.bigquery.DefaultTableDefinition; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.Schema; import com.google.gcloud.bigquery.TableId; @@ -126,8 +126,8 @@ Field stringField = Field.of("StringField", Field.Type.string()); // Table schema definition Schema schema = Schema.of(stringField); // Create a table -DefaultTableType tableType = DefaultTableType.of(schema); -TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); +DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(schema); +TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); ``` #### Loading data into a table @@ -208,7 +208,7 @@ display on your webpage. import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.DefaultTableType; +import com.google.gcloud.bigquery.DefaultTableDefinition; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; import com.google.gcloud.bigquery.InsertAllRequest; @@ -241,8 +241,8 @@ public class GcloudBigQueryExample { // Table schema definition Schema schema = Schema.of(stringField); // Create a table - DefaultTableType tableType = DefaultTableType.of(schema); - TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(schema); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); // Define rows to insert Map firstRow = new HashMap<>(); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java similarity index 79% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java index f61953e9fe03..0282ff6bf0d0 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableType.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java @@ -27,7 +27,7 @@ /** * Base class for a Google BigQuery table type. */ -public abstract class BaseTableType implements Serializable{ +public abstract class BaseTableDefinition implements Serializable { private static final long serialVersionUID = -374760330662959529L; @@ -36,22 +36,22 @@ public abstract class BaseTableType implements Serializable{ */ public enum Type { /** - * A normal BigQuery table. Instances of {@code BaseTableType} for this type are implemented by - * {@link DefaultTableType}. + * A normal BigQuery table. Instances of {@code BaseTableDefinition} for this type are + * implemented by {@link DefaultTableDefinition}. */ TABLE, /** - * A virtual table defined by a SQL query. Instances of {@code BaseTableType} for this type are - * implemented by {@link ViewType}. + * A virtual table defined by a SQL query. Instances of {@code BaseTableDefinition} for this + * type are implemented by {@link ViewDefinition}. * * @see Views */ VIEW, /** - * A BigQuery table backed by external data. Instances of {@code BaseTableType} for this type - * are implemented by {@link ExternalTableType}. + * A BigQuery table backed by external data. Instances of {@code BaseTableDefinition} for this + * type are implemented by {@link ExternalTableDefinition}. * * @see Federated Data * Sources @@ -68,7 +68,7 @@ public enum Type { * @param the table type class * @param the table type builder */ - public abstract static class Builder> { + public abstract static class Builder> { private Type type; private Schema schema; @@ -77,9 +77,9 @@ public abstract static class Builder T fromPb(Table tablePb) { + static T fromPb(Table tablePb) { switch (Type.valueOf(tablePb.getType())) { case TABLE: - return (T) DefaultTableType.fromPb(tablePb); + return (T) DefaultTableDefinition.fromPb(tablePb); case VIEW: - return (T) ViewType.fromPb(tablePb); + return (T) ViewDefinition.fromPb(tablePb); case EXTERNAL: - return (T) ExternalTableType.fromPb(tablePb); + 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/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 2dea48214ef0..53a07e8a67c3 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 @@ -276,7 +276,7 @@ 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 TableInfo#tableId()} and - * {@link TableInfo#type()} are always returned, even if not specified. + * {@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)); @@ -563,7 +563,7 @@ TableInfo getTable(TableId tableId, TableOption... options) /** * Lists the tables in the dataset. This method returns partial information on each table * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, - * {@link TableInfo#id()} and {@link TableInfo#type()}). To get complete information use + * {@link TableInfo#id()} and {@link TableInfo#definition()}). To get complete information use * either {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * @@ -574,9 +574,9 @@ Page listTables(String datasetId, TableListOption... options) /** * Lists the tables in the dataset. This method returns partial information on each table - * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, - * {@link TableInfo#id()} and {@link TableInfo#type()}). To get complete information use - * either {@link #getTable(TableId, TableOption...)} or + * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and + * {@link TableInfo#definition()}). To get complete information use either + * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * * @throws BigQueryException upon failure 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 4799612ef287..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 ExternalTableType#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 2aabcf85a275..647631c1d75e 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 @@ -220,13 +220,14 @@ public Table get(String table, BigQuery.TableOption... options) { * Creates a new table in this dataset. * * @param table the table's user-defined id - * @param type the table's type + * @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, BaseTableType type, BigQuery.TableOption... options) { - TableInfo tableInfo = TableInfo.of(TableId.of(info.datasetId().dataset(), table), type); + public Table create(String table, BaseTableDefinition 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/DefaultTableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java similarity index 88% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java index a8b0b30d2db6..6462969488c6 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableType.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java @@ -33,7 +33,7 @@ * * @see Managing Tables */ -public class DefaultTableType extends BaseTableType { +public class DefaultTableDefinition extends BaseTableDefinition { private static final long serialVersionUID = 2113445776046717900L; @@ -115,7 +115,8 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { } } - public static final class Builder extends BaseTableType.Builder { + public static final class Builder + extends BaseTableDefinition.Builder { private Long numBytes; private Long numRows; @@ -126,12 +127,12 @@ private Builder() { super(Type.TABLE); } - private Builder(DefaultTableType tableType) { - super(tableType); - this.numBytes = tableType.numBytes; - this.numRows = tableType.numRows; - this.location = tableType.location; - this.streamingBuffer = tableType.streamingBuffer; + private Builder(DefaultTableDefinition tableDefinition) { + super(tableDefinition); + this.numBytes = tableDefinition.numBytes; + this.numRows = tableDefinition.numRows; + this.location = tableDefinition.location; + this.streamingBuffer = tableDefinition.streamingBuffer; } private Builder(Table tablePb) { @@ -167,15 +168,15 @@ Builder streamingBuffer(StreamingBuffer streamingBuffer) { } /** - * Creates a {@code DefaultTableType} object. + * Creates a {@code DefaultTableDefinition} object. */ @Override - public DefaultTableType build() { - return new DefaultTableType(this); + public DefaultTableDefinition build() { + return new DefaultTableDefinition(this); } } - private DefaultTableType(Builder builder) { + private DefaultTableDefinition(Builder builder) { super(builder); this.numBytes = builder.numBytes; this.numRows = builder.numRows; @@ -228,12 +229,12 @@ public static Builder builder() { * * @param schema the schema of the table */ - public static DefaultTableType of(Schema schema) { + public static DefaultTableDefinition of(Schema schema) { return builder().schema(schema).build(); } /** - * Returns a builder for the {@code DefaultTableType} object. + * Returns a builder for the {@code DefaultTableDefinition} object. */ @Override public Builder toBuilder() { @@ -251,7 +252,7 @@ ToStringHelper toStringHelper() { @Override public boolean equals(Object obj) { - return obj instanceof DefaultTableType && baseEquals((DefaultTableType) obj); + return obj instanceof DefaultTableDefinition && baseEquals((DefaultTableDefinition) obj); } @Override @@ -274,7 +275,7 @@ Table toPb() { } @SuppressWarnings("unchecked") - static DefaultTableType fromPb(Table tablePb) { + static DefaultTableDefinition fromPb(Table tablePb) { return new Builder(tablePb).build(); } } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java similarity index 88% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableType.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java index a9fe31ebb96a..52384ae08cc3 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableType.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java @@ -35,19 +35,21 @@ * @see Federated Data Sources * */ -public class ExternalTableType extends BaseTableType { +public class ExternalTableDefinition extends BaseTableDefinition { - static final Function FROM_EXTERNAL_DATA_FUNCTION = - new Function() { + static final Function + FROM_EXTERNAL_DATA_FUNCTION = + new Function() { @Override - public ExternalTableType apply(ExternalDataConfiguration pb) { - return ExternalTableType.fromExternalDataConfiguration(pb); + public ExternalTableDefinition apply(ExternalDataConfiguration pb) { + return ExternalTableDefinition.fromExternalDataConfiguration(pb); } }; - static final Function TO_EXTERNAL_DATA_FUNCTION = - new Function() { + static final Function + TO_EXTERNAL_DATA_FUNCTION = + new Function() { @Override - public ExternalDataConfiguration apply(ExternalTableType tableInfo) { + public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) { return tableInfo.toExternalDataConfigurationPb(); } }; @@ -60,7 +62,8 @@ public ExternalDataConfiguration apply(ExternalTableType tableInfo) { private final Boolean ignoreUnknownValues; private final String compression; - public static final class Builder extends BaseTableType.Builder { + public static final class Builder + extends BaseTableDefinition.Builder { private List sourceUris; private FormatOptions formatOptions; @@ -72,13 +75,13 @@ private Builder() { super(Type.EXTERNAL); } - private Builder(ExternalTableType tableType) { - super(tableType); - this.sourceUris = tableType.sourceUris; - this.formatOptions = tableType.formatOptions; - this.maxBadRecords = tableType.maxBadRecords; - this.ignoreUnknownValues = tableType.ignoreUnknownValues; - this.compression = tableType.compression; + 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) { @@ -163,15 +166,15 @@ public Builder compression(String compression) { } /** - * Creates a {@code ExternalTableType} object. + * Creates an {@code ExternalTableDefinition} object. */ @Override - public ExternalTableType build() { - return new ExternalTableType(this); + public ExternalTableDefinition build() { + return new ExternalTableDefinition(this); } } - private ExternalTableType(Builder builder) { + private ExternalTableDefinition(Builder builder) { super(builder); this.compression = builder.compression; this.ignoreUnknownValues = builder.ignoreUnknownValues; @@ -234,7 +237,7 @@ public F formatOptions() { } /** - * Returns a builder for the {@code ExternalTableType} object. + * Returns a builder for the {@code ExternalTableDefinition} object. */ @Override public Builder toBuilder() { @@ -253,7 +256,7 @@ ToStringHelper toStringHelper() { @Override public boolean equals(Object obj) { - return obj instanceof ExternalTableType && baseEquals((ExternalTableType) obj); + return obj instanceof ExternalTableDefinition && baseEquals((ExternalTableDefinition) obj); } @Override @@ -297,7 +300,7 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataC } /** - * Creates a builder for an ExternalTableType 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. @@ -316,7 +319,7 @@ public static Builder builder(List sourceUris, Schema schema, FormatOpti } /** - * Creates a builder for an ExternalTableType 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 @@ -334,7 +337,7 @@ public static Builder builder(String sourceUri, Schema schema, FormatOptions for } /** - * Creates an ExternalTableType 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. @@ -348,7 +351,8 @@ public static Builder builder(String sourceUri, Schema schema, FormatOptions for * @see * Source Format */ - public static ExternalTableType of(List sourceUris, Schema schema, FormatOptions format) { + public static ExternalTableDefinition of(List sourceUris, Schema schema, + FormatOptions format) { return builder(sourceUris, schema, format).build(); } @@ -366,16 +370,16 @@ public static ExternalTableType of(List sourceUris, Schema schema, Forma * @see * Source Format */ - public static ExternalTableType of(String sourceUri, Schema schema, FormatOptions format) { + public static ExternalTableDefinition of(String sourceUri, Schema schema, FormatOptions format) { return builder(sourceUri, schema, format).build(); } @SuppressWarnings("unchecked") - static ExternalTableType fromPb(Table tablePb) { + static ExternalTableDefinition fromPb(Table tablePb) { return new Builder(tablePb).build(); } - static ExternalTableType fromExternalDataConfiguration( + static ExternalTableDefinition fromExternalDataConfiguration( ExternalDataConfiguration externalDataConfiguration) { Builder builder = new Builder(); if (externalDataConfiguration.getSourceUris() != null) { 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 000f71b8c067..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(), - ExternalTableType.FROM_EXTERNAL_DATA_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 tableDefinitions) * @param tableName name of the table * @param tableDefinition external data configuration for the table used by this query */ - public Builder addTableDefinition(String tableName, ExternalTableType 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, ExternalTableType.TO_EXTERNAL_DATA_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/TableInfo.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java index 124d78a7b70d..7e99e2850bdd 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 @@ -30,9 +30,9 @@ import java.util.Objects; /** - * Google BigQuery table information. Use {@link DefaultTableType} to create simple BigQuery table. - * Use {@link ViewType} to create a BigQuery view. Use {@link ExternalTableType} to create a - * BigQuery a table backed by external data. + * Google BigQuery table information. Use {@link DefaultTableDefinition} 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 */ @@ -64,7 +64,7 @@ public Table apply(TableInfo tableInfo) { private final Long creationTime; private final Long expirationTime; private final Long lastModifiedTime; - private final BaseTableType type; + private final BaseTableDefinition definition; /** * Builder for tables. @@ -80,7 +80,7 @@ public static class Builder { private Long creationTime; private Long expirationTime; private Long lastModifiedTime; - private BaseTableType type; + private BaseTableDefinition definition; private Builder() {} @@ -94,7 +94,7 @@ private Builder(TableInfo tableInfo) { this.creationTime = tableInfo.creationTime; this.expirationTime = tableInfo.expirationTime; this.lastModifiedTime = tableInfo.lastModifiedTime; - this.type = tableInfo.type; + this.definition = tableInfo.definition; } private Builder(Table tablePb) { @@ -109,7 +109,7 @@ private Builder(Table tablePb) { this.etag = tablePb.getEtag(); this.id = tablePb.getId(); this.selfLink = tablePb.getSelfLink(); - this.type = BaseTableType.fromPb(tablePb); + this.definition = BaseTableDefinition.fromPb(tablePb); } Builder creationTime(Long creationTime) { @@ -171,10 +171,10 @@ public Builder tableId(TableId tableId) { } /** - * Sets the table type. + * Sets the table definition. */ - public Builder type(BaseTableType type) { - this.type = checkNotNull(type); + public Builder definition(BaseTableDefinition definition) { + this.definition = checkNotNull(definition); return this; } @@ -196,7 +196,7 @@ private TableInfo(Builder builder) { this.creationTime = builder.creationTime; this.expirationTime = builder.expirationTime; this.lastModifiedTime = builder.lastModifiedTime; - this.type = builder.type; + this.definition = builder.definition; } /** @@ -265,11 +265,11 @@ public Long lastModifiedTime() { } /** - * Returns the table type. + * Returns the table definition. */ @SuppressWarnings("unchecked") - public T type() { - return (T) type; + public T definition() { + return (T) definition; } /** @@ -290,7 +290,7 @@ ToStringHelper toStringHelper() { .add("expirationTime", expirationTime) .add("creationTime", creationTime) .add("lastModifiedTime", lastModifiedTime) - .add("type", type); + .add("definition", definition); } @Override @@ -309,17 +309,17 @@ public boolean equals(Object obj) { } /** - * Returns a builder for a {@code TableInfo} object given table identity and type. + * Returns a builder for a {@code TableInfo} object given table identity and definition. */ - public static Builder builder(TableId tableId, BaseTableType type) { - return new Builder().tableId(tableId).type(type); + public static Builder builder(TableId tableId, BaseTableDefinition definition) { + return new Builder().tableId(tableId).definition(definition); } /** - * Returns a {@code TableInfo} object given table identity and type. + * Returns a {@code TableInfo} object given table identity and definition. */ - public static TableInfo of(TableId tableId, BaseTableType type) { - return builder(tableId, type).build(); + public static TableInfo of(TableId tableId, BaseTableDefinition definition) { + return builder(tableId, definition).build(); } TableInfo setProjectId(String projectId) { @@ -327,7 +327,7 @@ TableInfo setProjectId(String projectId) { } Table toPb() { - Table tablePb = type.toPb(); + Table tablePb = definition.toPb(); tablePb.setTableReference(tableId.toPb()); if (lastModifiedTime != null) { tablePb.setLastModifiedTime(BigInteger.valueOf(lastModifiedTime)); diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewType.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java similarity index 84% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewType.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java index af85f5005fbb..1358dc9eaecd 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewType.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java @@ -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; @@ -33,14 +32,14 @@ * * @see Views */ -public final class ViewType extends BaseTableType { +public final class ViewDefinition extends BaseTableDefinition { private static final long serialVersionUID = -8789311196910794545L; private final String query; private final List userDefinedFunctions; - public static final class Builder extends BaseTableType.Builder { + public static final class Builder extends BaseTableDefinition.Builder { private String query; private List userDefinedFunctions; @@ -49,15 +48,15 @@ private Builder() { super(Type.VIEW); } - private Builder(ViewType viewType) { - super(viewType); - this.query = viewType.query; - this.userDefinedFunctions = viewType.userDefinedFunctions; + private Builder(ViewDefinition viewDefinition) { + super(viewDefinition); + this.query = viewDefinition.query; + this.userDefinedFunctions = viewDefinition.userDefinedFunctions; } 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) { @@ -98,15 +97,15 @@ public Builder userDefinedFunctions(UserDefinedFunction... userDefinedFunctions) } /** - * Creates a {@code ViewType} object. + * Creates a {@code ViewDefinition} object. */ @Override - public ViewType build() { - return new ViewType(this); + public ViewDefinition build() { + return new ViewDefinition(this); } } - private ViewType(Builder builder) { + private ViewDefinition(Builder builder) { super(builder); this.query = builder.query; this.userDefinedFunctions = builder.userDefinedFunctions; @@ -147,7 +146,7 @@ ToStringHelper toStringHelper() { @Override public boolean equals(Object obj) { - return obj instanceof ViewType && baseEquals((ViewType) obj); + return obj instanceof ViewDefinition && baseEquals((ViewDefinition) obj); } @Override @@ -158,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)); @@ -201,7 +201,7 @@ public static Builder builder(String query, UserDefinedFunction... functions) { * * @param query the query used to generate the table */ - public static ViewType of(String query) { + public static ViewDefinition of(String query) { return builder(query).build(); } @@ -211,7 +211,7 @@ public static ViewType of(String query) { * @param query the query used to generate the table * @param functions user-defined functions that can be used by the query */ - public static ViewType of(String query, List functions) { + public static ViewDefinition of(String query, List functions) { return builder(query, functions).build(); } @@ -221,12 +221,12 @@ public static ViewType of(String query, List functions) { * @param query the query used to generate the table * @param functions user-defined functions that can be used by the query */ - public static ViewType of(String query, UserDefinedFunction... functions) { + public static ViewDefinition of(String query, UserDefinedFunction... functions) { return builder(query, functions).build(); } @SuppressWarnings("unchecked") - static ViewType 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 f3fb07ea284c..c0cfb279cbcf 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 @@ -26,7 +26,7 @@ * System.out.println("Creating table " + tableId); * Field integerField = Field.of("fieldName", Field.Type.integer()); * Schema schema = Schema.of(integerField); - * bigquery.create(TableInfo.of(tableId, DefaultTableType.of(schema))); + * bigquery.create(TableInfo.of(tableId, DefaultTableDefinition.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 9357f3c4b6a4..c0aa518a3270 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,7 +104,7 @@ public class BigQueryImplTest { .description("FieldDescription3") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); - private static final DefaultTableType TABLE_TYPE = DefaultTableType.of(TABLE_SCHEMA); + private static final DefaultTableDefinition TABLE_TYPE = DefaultTableDefinition.of(TABLE_SCHEMA); private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE); private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_TYPE); private static final TableInfo TABLE_INFO_WITH_PROJECT = 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 bd01d0435fa3..fcc9d0c5cc45 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 @@ -44,14 +44,15 @@ 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 DefaultTableType TABLE_TYPE = DefaultTableType.of(Schema.of(FIELD)); - private static final ViewType VIEW_TYPE = ViewType.of("QUERY"); - private static final ExternalTableType EXTERNAL_TABLE_TYPE = - ExternalTableType.of(ImmutableList.of("URI"), Schema.of(), FormatOptions.csv()); + private static final DefaultTableDefinition TABLE_DEFINITION = + DefaultTableDefinition.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_TYPE).build(), - TableInfo.builder(TableId.of("dataset", "table2"), VIEW_TYPE).build(), - TableInfo.builder(TableId.of("dataset", "table2"), EXTERNAL_TABLE_TYPE).build()); + 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(); @@ -206,7 +207,7 @@ public void testListWithOptions() throws Exception { @Test public void testGet() throws Exception { - TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_TYPE).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"); @@ -223,7 +224,7 @@ public void testGetNull() throws Exception { @Test public void testGetWithOptions() throws Exception { - TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_TYPE).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); @@ -234,19 +235,19 @@ public void testGetWithOptions() throws Exception { @Test public void testCreateTable() throws Exception { - TableInfo info = TableInfo.builder(TableId.of("dataset", "table1"), TABLE_TYPE).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", TABLE_TYPE); + 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"), TABLE_TYPE).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", TABLE_TYPE, 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/DefaultTableTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java similarity index 58% rename from gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java rename to gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java index 184cd8b74691..d595d7bb4a6c 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableTypeTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java @@ -19,11 +19,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.google.gcloud.bigquery.DefaultTableType.StreamingBuffer; +import com.google.gcloud.bigquery.DefaultTableDefinition.StreamingBuffer; import org.junit.Test; -public class DefaultTableTypeTest { +public class DefaultTableDefinitionTest { private static final Field FIELD_SCHEMA1 = Field.builder("StringField", Field.Type.string()) @@ -45,52 +45,56 @@ public class DefaultTableTypeTest { 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 DefaultTableType DEFAULT_TABLE_TYPE = DefaultTableType.builder() - .location(LOCATION) - .numBytes(NUM_BYTES) - .numRows(NUM_ROWS) - .streamingBuffer(STREAMING_BUFFER) - .schema(TABLE_SCHEMA) - .build(); - + private static final DefaultTableDefinition DEFAULT_TABLE_DEFINITION = + DefaultTableDefinition.builder() + .location(LOCATION) + .numBytes(NUM_BYTES) + .numRows(NUM_ROWS) + .streamingBuffer(STREAMING_BUFFER) + .schema(TABLE_SCHEMA) + .build(); @Test public void testToBuilder() { - compareDefaultTableType(DEFAULT_TABLE_TYPE, DEFAULT_TABLE_TYPE.toBuilder().build()); - DefaultTableType tableType = DEFAULT_TABLE_TYPE.toBuilder() + compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, + DEFAULT_TABLE_DEFINITION.toBuilder().build()); + DefaultTableDefinition tableDefinition = DEFAULT_TABLE_DEFINITION.toBuilder() .location("EU") .build(); - assertEquals("EU", tableType.location()); - tableType = tableType.toBuilder() + assertEquals("EU", tableDefinition.location()); + tableDefinition = tableDefinition.toBuilder() .location(LOCATION) .build(); - compareDefaultTableType(DEFAULT_TABLE_TYPE, tableType); + compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, tableDefinition); } @Test public void testToBuilderIncomplete() { - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - assertEquals(tableType, tableType.toBuilder().build()); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + assertEquals(tableDefinition, tableDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(BaseTableType.Type.TABLE, DEFAULT_TABLE_TYPE.type()); - assertEquals(TABLE_SCHEMA, DEFAULT_TABLE_TYPE.schema()); - assertEquals(LOCATION, DEFAULT_TABLE_TYPE.location()); - assertEquals(NUM_BYTES, DEFAULT_TABLE_TYPE.numBytes()); - assertEquals(NUM_ROWS, DEFAULT_TABLE_TYPE.numRows()); - assertEquals(STREAMING_BUFFER, DEFAULT_TABLE_TYPE.streamingBuffer()); + assertEquals(BaseTableDefinition.Type.TABLE, DEFAULT_TABLE_DEFINITION.type()); + assertEquals(TABLE_SCHEMA, DEFAULT_TABLE_DEFINITION.schema()); + assertEquals(LOCATION, DEFAULT_TABLE_DEFINITION.location()); + assertEquals(NUM_BYTES, DEFAULT_TABLE_DEFINITION.numBytes()); + assertEquals(NUM_ROWS, DEFAULT_TABLE_DEFINITION.numRows()); + assertEquals(STREAMING_BUFFER, DEFAULT_TABLE_DEFINITION.streamingBuffer()); } @Test public void testToAndFromPb() { - assertTrue(BaseTableType.fromPb(DEFAULT_TABLE_TYPE.toPb()) instanceof DefaultTableType); - compareDefaultTableType(DEFAULT_TABLE_TYPE, - BaseTableType.fromPb(DEFAULT_TABLE_TYPE.toPb())); + assertTrue( + BaseTableDefinition.fromPb(DEFAULT_TABLE_DEFINITION.toPb()) + instanceof DefaultTableDefinition); + compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, + BaseTableDefinition.fromPb(DEFAULT_TABLE_DEFINITION.toPb())); } - private void compareDefaultTableType(DefaultTableType expected, DefaultTableType value) { + private void compareDefaultTableDefinition(DefaultTableDefinition expected, + DefaultTableDefinition value) { assertEquals(expected, value); assertEquals(expected.schema(), value.schema()); assertEquals(expected.type(), value.type()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java similarity index 58% rename from gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableTypeTest.java rename to gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java index 57e7ab18cb26..8a821bf32d4e 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableTypeTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java @@ -24,7 +24,7 @@ import java.util.List; -public class ExternalTableTypeTest { +public class ExternalTableDefinitionTest { private static final List SOURCE_URIS = ImmutableList.of("uri1", "uri2"); private static final Field FIELD_SCHEMA1 = @@ -47,8 +47,8 @@ public class ExternalTableTypeTest { 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 ExternalTableType EXTERNAL_TABLE_TYPE = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + 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) @@ -56,43 +56,47 @@ public class ExternalTableTypeTest { @Test public void testToBuilder() { - compareExternalTableType(EXTERNAL_TABLE_TYPE, EXTERNAL_TABLE_TYPE.toBuilder().build()); - ExternalTableType externalTableType = EXTERNAL_TABLE_TYPE.toBuilder().compression("NONE").build(); - assertEquals("NONE", externalTableType.compression()); - externalTableType = externalTableType.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(); - compareExternalTableType(EXTERNAL_TABLE_TYPE, externalTableType); + compareExternalTableDefinition(EXTERNAL_TABLE_DEFINITION, externalTableDefinition); } @Test public void testToBuilderIncomplete() { - ExternalTableType externalTableType = - ExternalTableType.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); - assertEquals(externalTableType, externalTableType.toBuilder().build()); + ExternalTableDefinition externalTableDefinition = + ExternalTableDefinition.of(SOURCE_URIS, TABLE_SCHEMA, FormatOptions.json()); + assertEquals(externalTableDefinition, externalTableDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(BaseTableType.Type.EXTERNAL, EXTERNAL_TABLE_TYPE.type()); - assertEquals(COMPRESSION, EXTERNAL_TABLE_TYPE.compression()); - assertEquals(CSV_OPTIONS, EXTERNAL_TABLE_TYPE.formatOptions()); - assertEquals(IGNORE_UNKNOWN_VALUES, EXTERNAL_TABLE_TYPE.ignoreUnknownValues()); - assertEquals(MAX_BAD_RECORDS, EXTERNAL_TABLE_TYPE.maxBadRecords()); - assertEquals(TABLE_SCHEMA, EXTERNAL_TABLE_TYPE.schema()); - assertEquals(SOURCE_URIS, EXTERNAL_TABLE_TYPE.sourceUris()); + assertEquals(BaseTableDefinition.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() { - compareExternalTableType(EXTERNAL_TABLE_TYPE, - ExternalTableType.fromPb(EXTERNAL_TABLE_TYPE.toPb())); - ExternalTableType externalTableType = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS).build(); - compareExternalTableType(externalTableType, ExternalTableType.fromPb(externalTableType.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 compareExternalTableType(ExternalTableType expected, ExternalTableType value) { + private void compareExternalTableDefinition(ExternalTableDefinition expected, + ExternalTableDefinition value) { assertEquals(expected, value); assertEquals(expected.compression(), value.compression()); assertEquals(expected.formatOptions(), value.formatOptions()); 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 7b58303cadbb..20aaba7dd293 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,21 +257,21 @@ public void testGetNonExistingTable() { public void testCreateAndGetTable() { String tableName = "test_create_and_get_table"; TableId tableId = TableId.of(DATASET, tableName); - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.type() instanceof DefaultTableType); + assertTrue(remoteTableInfo.definition() instanceof DefaultTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableType.Type.TABLE, remoteTableInfo.type().type()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.type().schema()); + assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); assertNotNull(remoteTableInfo.creationTime()); assertNotNull(remoteTableInfo.lastModifiedTime()); - assertNotNull(remoteTableInfo.type().numBytes()); - assertNotNull(remoteTableInfo.type().numRows()); + assertNotNull(remoteTableInfo.definition().numBytes()); + assertNotNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -279,22 +279,22 @@ public void testCreateAndGetTable() { public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableType)); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName, TableOption.fields(TableField.CREATION_TIME)); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.type() instanceof DefaultTableType); + assertTrue(remoteTableInfo.definition() instanceof DefaultTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableType.Type.TABLE, remoteTableInfo.type().type()); + assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertNotNull(remoteTableInfo.creationTime()); - assertNull(remoteTableInfo.type().schema()); + assertNull(remoteTableInfo.definition().schema()); assertNull(remoteTableInfo.lastModifiedTime()); - assertNull(remoteTableInfo.type().numBytes()); - assertNull(remoteTableInfo.type().numRows()); + assertNull(remoteTableInfo.definition().numBytes()); + assertNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -302,18 +302,18 @@ public void testCreateAndGetTableWithSelectedField() { public void testCreateExternalTable() throws InterruptedException { String tableName = "test_create_external_table"; TableId tableId = TableId.of(DATASET, tableName); - ExternalTableType externalTableType = ExternalTableType.of( + ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of( "gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json()); - TableInfo tableInfo = TableInfo.of(tableId, externalTableType); + TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.type() instanceof ExternalTableType); + assertTrue(remoteTableInfo.definition() instanceof ExternalTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.type().schema()); + assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); QueryRequest request = QueryRequest.builder( "SELECT TimestampField, StringField, IntegerField, BooleanField FROM " + DATASET + "." + tableName) @@ -352,9 +352,10 @@ public void testCreateExternalTable() throws InterruptedException { public void testCreateViewTable() throws InterruptedException { String tableName = "test_create_view_table"; TableId tableId = TableId.of(DATASET, tableName); - ViewType viewType = ViewType.of("SELECT TimestampField, StringField, BooleanField FROM " - + DATASET + "." + TABLE_ID.table()); - TableInfo tableInfo = TableInfo.of(tableId, viewType); + ViewDefinition viewDefinition = + ViewDefinition.of("SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + + TABLE_ID.table()); + TableInfo tableInfo = TableInfo.of(tableId, viewDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -362,7 +363,7 @@ public void testCreateViewTable() throws InterruptedException { TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertTrue(remoteTableInfo.type() instanceof ViewType); + assertTrue(remoteTableInfo.definition() instanceof ViewDefinition); Schema expectedSchema = Schema.builder() .addField( Field.builder("TimestampField", Field.Type.timestamp()) @@ -377,7 +378,7 @@ public void testCreateViewTable() throws InterruptedException { .mode(Field.Mode.NULLABLE) .build()) .build(); - assertEquals(expectedSchema, remoteTableInfo.type().schema()); + assertEquals(expectedSchema, remoteTableInfo.definition().schema()); QueryRequest request = QueryRequest.builder("SELECT * FROM " + tableName) .defaultDataset(DatasetId.of(DATASET)) .maxWaitTime(60000L) @@ -408,8 +409,8 @@ public void testCreateViewTable() throws InterruptedException { @Test public void testListTables() { String tableName = "test_list_tables"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); Page tables = bigquery.listTables(DATASET); @@ -427,15 +428,15 @@ public void testListTables() { @Test public void testUpdateTable() { String tableName = "test_update_table"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder() .description("newDescription").build()); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); - assertEquals(TABLE_SCHEMA, updatedTableInfo.type().schema()); + assertEquals(TABLE_SCHEMA, updatedTableInfo.definition().schema()); assertEquals("newDescription", updatedTableInfo.description()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -443,28 +444,27 @@ public void testUpdateTable() { @Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder().description("newDescr") .build(), TableOption.fields(TableField.DESCRIPTION)); - assertTrue(updatedTableInfo.type() instanceof DefaultTableType); + assertTrue(updatedTableInfo.definition() instanceof DefaultTableDefinition); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); assertEquals("newDescr", updatedTableInfo.description()); - assertNull(updatedTableInfo.type().schema()); + assertNull(updatedTableInfo.definition().schema()); assertNull(updatedTableInfo.lastModifiedTime()); - assertNull(updatedTableInfo.type().numBytes()); - assertNull(updatedTableInfo.type().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"), - DefaultTableType.of(SIMPLE_SCHEMA)); + DefaultTableDefinition.of(SIMPLE_SCHEMA)); try { bigquery.update(tableInfo); fail("BigQueryException was expected"); @@ -484,8 +484,8 @@ public void testDeleteNonExistingTable() { @Test public void testInsertAll() { String tableName = "test_insert_all_table"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.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( @@ -516,8 +516,8 @@ public void testInsertAll() { @Test public void testInsertAllWithSuffix() throws InterruptedException { String tableName = "test_insert_all_with_suffix_table"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.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( @@ -557,8 +557,8 @@ public void testInsertAllWithSuffix() throws InterruptedException { @Test public void testInsertAllWithErrors() { String tableName = "test_insert_all_with_errors_table"; - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.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( @@ -689,8 +689,8 @@ 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); - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -722,8 +722,8 @@ 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); - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -762,8 +762,8 @@ 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); - DefaultTableType tableType = DefaultTableType.of(TABLE_SCHEMA); - TableInfo tableInfo = TableInfo.of(sourceTable, tableType); + DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -780,7 +780,7 @@ public void testCopyJob() throws InterruptedException { assertNotNull(remoteTableInfo); assertEquals(destinationTable.dataset(), remoteTableInfo.tableId().dataset()); assertEquals(destinationTableName, remoteTableInfo.tableId().table()); - assertEquals(TABLE_SCHEMA, remoteTableInfo.type().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 7a47a269dc1a..acfba8b6d0f6 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,7 @@ 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 BaseTableType TABLE_TYPE = DefaultTableType.of(TABLE_SCHEMA); + private static final BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.of(TABLE_SCHEMA); private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE); private static final boolean SKIP_INVALID_ROWS = true; private static final boolean IGNORE_UNKNOWN_VALUES = false; 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 b4e7a4f1434b..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,8 +118,8 @@ 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 ExternalTableType TABLE_CONFIGURATION = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + private static final ExternalTableDefinition TABLE_CONFIGURATION = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .compression(COMPRESSION) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .maxBadRecords(MAX_BAD_RECORDS) @@ -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 fd2b9b9bae2f..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 ExternalTableType TABLE_CONFIGURATION = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + 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 = + 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 6f2ca3f4cbe6..12f7c184087a 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.DefaultTableType.StreamingBuffer; +import com.google.gcloud.bigquery.DefaultTableDefinition.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 ExternalTableType EXTERNAL_TABLE_TYPE = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + private static final ExternalTableDefinition EXTERNAL_TABLE_TYPE = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .ignoreUnknownValues(true) .maxBadRecords(42) .build(); @@ -108,7 +108,7 @@ public class SerializationTest { new UserDefinedFunction.InlineFunction("inline"); private static final UserDefinedFunction URI_FUNCTION = new UserDefinedFunction.UriFunction("URI"); - private static final BaseTableType TABLE_TYPE = DefaultTableType.builder() + private static final BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.builder() .schema(TABLE_SCHEMA) .location(LOCATION) .streamingBuffer(STREAMING_BUFFER) @@ -119,7 +119,7 @@ public class SerializationTest { .etag(ETAG) .id(ID) .build(); - private static final BaseTableType VIEW_TYPE = ViewType.of("QUERY"); + private static final BaseTableDefinition VIEW_TYPE = ViewDefinition.of("QUERY"); private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_TYPE) .creationTime(CREATION_TIME) .description(DESCRIPTION) 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 0df5a9d2c012..615d401ae1fb 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 @@ -55,9 +55,9 @@ public class TableInfoTest { private static final Long NUM_BYTES = 42L; private static final Long NUM_ROWS = 43L; private static final String LOCATION = "US"; - private static final DefaultTableType.StreamingBuffer STREAMING_BUFFER = - new DefaultTableType.StreamingBuffer(1L, 2L, 3L); - private static final DefaultTableType DEFAULT_TABLE_TYPE = DefaultTableType.builder() + private static final DefaultTableDefinition.StreamingBuffer STREAMING_BUFFER = + new DefaultTableDefinition.StreamingBuffer(1L, 2L, 3L); + private static final DefaultTableDefinition DEFAULT_TABLE_TYPE = DefaultTableDefinition.builder() .location(LOCATION) .numBytes(NUM_BYTES) .numRows(NUM_ROWS) @@ -70,8 +70,8 @@ public class TableInfoTest { 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 ExternalTableType EXTERNAL_TABLE_TYPE = - ExternalTableType.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) + private static final ExternalTableDefinition EXTERNAL_TABLE_TYPE = + ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .compression(COMPRESSION) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) .maxBadRecords(MAX_BAD_RECORDS) @@ -80,8 +80,8 @@ public class TableInfoTest { 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 ViewType VIEW_TYPE = - ViewType.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + private static final ViewDefinition VIEW_TYPE = + ViewDefinition.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, DEFAULT_TABLE_TYPE) .creationTime(CREATION_TIME) @@ -150,10 +150,10 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, TABLE_INFO.friendlyName()); assertEquals(ID, TABLE_INFO.id()); assertEquals(LAST_MODIFIED_TIME, TABLE_INFO.lastModifiedTime()); - assertEquals(DEFAULT_TABLE_TYPE, TABLE_INFO.type()); + assertEquals(DEFAULT_TABLE_TYPE, TABLE_INFO.definition()); assertEquals(SELF_LINK, TABLE_INFO.selfLink()); assertEquals(TABLE_ID, VIEW_INFO.tableId()); - assertEquals(VIEW_TYPE, 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()); @@ -161,7 +161,7 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, VIEW_INFO.friendlyName()); assertEquals(ID, VIEW_INFO.id()); assertEquals(LAST_MODIFIED_TIME, VIEW_INFO.lastModifiedTime()); - assertEquals(VIEW_TYPE, VIEW_INFO.type()); + assertEquals(VIEW_TYPE, VIEW_INFO.definition()); assertEquals(SELF_LINK, VIEW_INFO.selfLink()); assertEquals(TABLE_ID, EXTERNAL_TABLE_INFO.tableId()); assertEquals(CREATION_TIME, EXTERNAL_TABLE_INFO.creationTime()); @@ -171,7 +171,7 @@ 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(EXTERNAL_TABLE_TYPE, EXTERNAL_TABLE_INFO.type()); + assertEquals(EXTERNAL_TABLE_TYPE, EXTERNAL_TABLE_INFO.definition()); assertEquals(SELF_LINK, EXTERNAL_TABLE_INFO.selfLink()); } @@ -192,7 +192,7 @@ public void testSetProjectId() { private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected, value); assertEquals(expected.tableId(), value.tableId()); - 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()); @@ -201,7 +201,7 @@ private void compareTableInfo(TableInfo expected, TableInfo value) { assertEquals(expected.id(), value.id()); assertEquals(expected.lastModifiedTime(), value.lastModifiedTime()); assertEquals(expected.selfLink(), value.selfLink()); - assertEquals(expected.type(), value.type()); + 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 4c03cb0ee77a..48bdde5867fb 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,7 @@ 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 BaseTableType TABLE_TYPE = DefaultTableType.of(Schema.of(FIELD)); + private static final BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.of(Schema.of(FIELD)); private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, TABLE_TYPE); private static final List ROWS_TO_INSERT = ImmutableList.of( RowToInsert.of("id1", ImmutableMap.of("key", "val1")), diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java similarity index 58% rename from gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java rename to gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java index 83a2b011582c..26e65d3a5d46 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewTypeTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java @@ -25,47 +25,47 @@ import java.util.List; -public class ViewTypeTest { +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 ViewType VIEW_TYPE = - ViewType.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + private static final ViewDefinition VIEW_DEFINITION = + ViewDefinition.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); @Test public void testToBuilder() { - compareViewType(VIEW_TYPE, VIEW_TYPE.toBuilder().build()); - ViewType viewType = VIEW_TYPE.toBuilder() + compareViewDefinition(VIEW_DEFINITION, VIEW_DEFINITION.toBuilder().build()); + ViewDefinition viewDefinition = VIEW_DEFINITION.toBuilder() .query("NEW QUERY") .build(); - assertEquals("NEW QUERY", viewType.query()); - viewType = viewType.toBuilder() + assertEquals("NEW QUERY", viewDefinition.query()); + viewDefinition = viewDefinition.toBuilder() .query(VIEW_QUERY) .build(); - compareViewType(VIEW_TYPE, viewType); + compareViewDefinition(VIEW_DEFINITION, viewDefinition); } @Test public void testToBuilderIncomplete() { - BaseTableType tableType = ViewType.of(VIEW_QUERY); - assertEquals(tableType, tableType.toBuilder().build()); + BaseTableDefinition viewDefinition = ViewDefinition.of(VIEW_QUERY); + assertEquals(viewDefinition, viewDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(VIEW_QUERY, VIEW_TYPE.query()); - assertEquals(BaseTableType.Type.VIEW, VIEW_TYPE.type()); - assertEquals(USER_DEFINED_FUNCTIONS, VIEW_TYPE.userDefinedFunctions()); + assertEquals(VIEW_QUERY, VIEW_DEFINITION.query()); + assertEquals(BaseTableDefinition.Type.VIEW, VIEW_DEFINITION.type()); + assertEquals(USER_DEFINED_FUNCTIONS, VIEW_DEFINITION.userDefinedFunctions()); } @Test public void testToAndFromPb() { - assertTrue(BaseTableType.fromPb(VIEW_TYPE.toPb()) instanceof ViewType); - compareViewType(VIEW_TYPE, BaseTableType.fromPb(VIEW_TYPE.toPb())); + assertTrue(BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb()) instanceof ViewDefinition); + compareViewDefinition(VIEW_DEFINITION, BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb())); } - private void compareViewType(ViewType expected, ViewType value) { + private void compareViewDefinition(ViewDefinition expected, ViewDefinition value) { assertEquals(expected, value); assertEquals(expected.query(), value.query()); assertEquals(expected.userDefinedFunctions(), value.userDefinedFunctions()); 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 8dc72b5b30a9..1f8648623d40 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,14 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.WriteChannel; -import com.google.gcloud.bigquery.DefaultTableType; -import com.google.gcloud.bigquery.ExternalTableType; -import com.google.gcloud.bigquery.TableInfo; 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.DefaultTableDefinition; +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,13 +33,14 @@ import com.google.gcloud.bigquery.JobId; import com.google.gcloud.bigquery.JobInfo; import com.google.gcloud.bigquery.JobStatus; -import com.google.gcloud.bigquery.ViewType; -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.TableId; +import com.google.gcloud.bigquery.TableInfo; +import com.google.gcloud.bigquery.ViewDefinition; +import com.google.gcloud.bigquery.WriteChannelConfiguration; import com.google.gcloud.spi.BigQueryRpc.Tuple; import java.nio.channels.FileChannel; @@ -434,8 +434,8 @@ 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 DefaultTableType}). + * This class demonstrates how to create a simple BigQuery Table (i.e. a table created from a + * {@link DefaultTableDefinition}). * * @see Tables: insert * @@ -447,7 +447,7 @@ TableInfo parse(String... args) throws Exception { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - return TableInfo.of(tableId, DefaultTableType.of(parseSchema(args, 2, args.length))); + return TableInfo.of(tableId, DefaultTableDefinition.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); } @@ -459,8 +459,8 @@ protected String params() { } /** - * This class demonstrates how to create a BigQuery External Table (i.e. a table of type - * {@link ExternalTableType}). + * This class demonstrates how to create a BigQuery External Table (i.e. a table created from a + * {@link ExternalTableDefinition}). * * @see Tables: insert * @@ -472,10 +472,10 @@ TableInfo parse(String... args) throws Exception { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - ExternalTableType externalTableType = - ExternalTableType.of(args[args.length - 1], + ExternalTableDefinition externalTableDefinition = + ExternalTableDefinition.of(args[args.length - 1], parseSchema(args, 3, args.length - 1), FormatOptions.of(args[2])); - return TableInfo.of(tableId, externalTableType); + return TableInfo.of(tableId, externalTableDefinition); } throw new IllegalArgumentException("Missing required arguments."); } @@ -487,8 +487,8 @@ protected String params() { } /** - * This class demonstrates how to create a BigQuery View Table (i.e. a table of type - * {@link ViewType}). + * This class demonstrates how to create a BigQuery View Table (i.e. a table created from a + * {@link ViewDefinition}). * * @see Tables: insert * @@ -502,7 +502,7 @@ TableInfo parse(String... args) throws Exception { String table = args[1]; String query = args[2]; TableId tableId = TableId.of(dataset, table); - return TableInfo.of(tableId, ViewType.of(query)); + return TableInfo.of(tableId, ViewDefinition.of(query)); } else if (args.length < 3) { message = "Missing required dataset id, table id or query."; } else { From 36d9dc353bf0a75385f8cb24a6fcb6b3ce390a26 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 1 Feb 2016 19:32:02 +0100 Subject: [PATCH 3/5] Fix minor docs and style errors --- gcloud-java-bigquery/README.md | 2 +- .../java/com/google/gcloud/bigquery/ViewDefinitionTest.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/gcloud-java-bigquery/README.md b/gcloud-java-bigquery/README.md index e11699a660d6..636573bfaa18 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -269,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/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ViewDefinitionTest.java index 26e65d3a5d46..a3578fb4494e 100644 --- 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 @@ -62,7 +62,8 @@ public void testBuilder() { @Test public void testToAndFromPb() { assertTrue(BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb()) instanceof ViewDefinition); - compareViewDefinition(VIEW_DEFINITION, BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb())); + compareViewDefinition(VIEW_DEFINITION, + BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb())); } private void compareViewDefinition(ViewDefinition expected, ViewDefinition value) { From 085903ae55a6ef6bedb5a8dfd03dce378dd25a41 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 1 Feb 2016 20:22:40 +0100 Subject: [PATCH 4/5] Rename DefaultTableDefinition to TableDefinition --- README.md | 4 +- gcloud-java-bigquery/README.md | 8 ++-- .../gcloud/bigquery/BaseTableDefinition.java | 4 +- ...leDefinition.java => TableDefinition.java} | 22 +++++----- .../com/google/gcloud/bigquery/TableInfo.java | 10 +++-- .../google/gcloud/bigquery/package-info.java | 2 +- .../gcloud/bigquery/BigQueryImplTest.java | 8 ++-- .../google/gcloud/bigquery/DatasetTest.java | 4 +- .../gcloud/bigquery/ITBigQueryTest.java | 42 +++++++++---------- .../gcloud/bigquery/InsertAllRequestTest.java | 4 +- .../gcloud/bigquery/SerializationTest.java | 27 ++++++------ ...tionTest.java => TableDefinitionTest.java} | 41 +++++++++--------- .../google/gcloud/bigquery/TableInfoTest.java | 20 ++++----- .../com/google/gcloud/bigquery/TableTest.java | 4 +- .../gcloud/examples/BigQueryExample.java | 6 +-- 15 files changed, 105 insertions(+), 101 deletions(-) rename gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/{DefaultTableDefinition.java => TableDefinition.java} (91%) rename gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/{DefaultTableDefinitionTest.java => TableDefinitionTest.java} (67%) diff --git a/README.md b/README.md index 4c35daa67e87..c98949371215 100644 --- a/README.md +++ b/README.md @@ -127,9 +127,11 @@ must [supply credentials](#authentication) and a project ID if running this snip ```java import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; +import com.google.gcloud.bigquery.TableDefinition; 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.TableId; import com.google.gcloud.bigquery.TableInfo; @@ -141,7 +143,7 @@ if (info == null) { System.out.println("Creating table " + tableId); Field integerField = Field.of("fieldName", Field.Type.integer()); Schema schema = Schema.of(integerField); - bigquery.create(TableInfo.of(tableId, DefaultTableDefinition.of(schema))); + bigquery.create(TableInfo.of(tableId, TableDefinition.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 636573bfaa18..11550b8c2351 100644 --- a/gcloud-java-bigquery/README.md +++ b/gcloud-java-bigquery/README.md @@ -111,7 +111,7 @@ 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.DefaultTableDefinition; +import com.google.gcloud.bigquery.TableDefinition; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.Schema; import com.google.gcloud.bigquery.TableId; @@ -126,7 +126,7 @@ Field stringField = Field.of("StringField", Field.Type.string()); // Table schema definition Schema schema = Schema.of(stringField); // Create a table -DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(schema); +TableDefinition tableDefinition = TableDefinition.of(schema); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); ``` @@ -208,7 +208,7 @@ display on your webpage. import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.DefaultTableDefinition; +import com.google.gcloud.bigquery.TableDefinition; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; import com.google.gcloud.bigquery.InsertAllRequest; @@ -241,7 +241,7 @@ public class GcloudBigQueryExample { // Table schema definition Schema schema = Schema.of(stringField); // Create a table - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(schema); + TableDefinition tableDefinition = TableDefinition.of(schema); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); // Define rows to insert diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java index 0282ff6bf0d0..908bb1199d36 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java @@ -37,7 +37,7 @@ public abstract class BaseTableDefinition implements Serializable { public enum Type { /** * A normal BigQuery table. Instances of {@code BaseTableDefinition} for this type are - * implemented by {@link DefaultTableDefinition}. + * implemented by {@link TableDefinition}. */ TABLE, @@ -169,7 +169,7 @@ Table toPb() { static T fromPb(Table tablePb) { switch (Type.valueOf(tablePb.getType())) { case TABLE: - return (T) DefaultTableDefinition.fromPb(tablePb); + return (T) TableDefinition.fromPb(tablePb); case VIEW: return (T) ViewDefinition.fromPb(tablePb); case EXTERNAL: diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java similarity index 91% rename from gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java rename to gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java index 6462969488c6..1a82fd6756be 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/DefaultTableDefinition.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java @@ -33,7 +33,7 @@ * * @see Managing Tables */ -public class DefaultTableDefinition extends BaseTableDefinition { +public class TableDefinition extends BaseTableDefinition { private static final long serialVersionUID = 2113445776046717900L; @@ -116,7 +116,7 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) { } public static final class Builder - extends BaseTableDefinition.Builder { + extends BaseTableDefinition.Builder { private Long numBytes; private Long numRows; @@ -127,7 +127,7 @@ private Builder() { super(Type.TABLE); } - private Builder(DefaultTableDefinition tableDefinition) { + private Builder(TableDefinition tableDefinition) { super(tableDefinition); this.numBytes = tableDefinition.numBytes; this.numRows = tableDefinition.numRows; @@ -168,15 +168,15 @@ Builder streamingBuffer(StreamingBuffer streamingBuffer) { } /** - * Creates a {@code DefaultTableDefinition} object. + * Creates a {@code TableDefinition} object. */ @Override - public DefaultTableDefinition build() { - return new DefaultTableDefinition(this); + public TableDefinition build() { + return new TableDefinition(this); } } - private DefaultTableDefinition(Builder builder) { + private TableDefinition(Builder builder) { super(builder); this.numBytes = builder.numBytes; this.numRows = builder.numRows; @@ -229,12 +229,12 @@ public static Builder builder() { * * @param schema the schema of the table */ - public static DefaultTableDefinition of(Schema schema) { + public static TableDefinition of(Schema schema) { return builder().schema(schema).build(); } /** - * Returns a builder for the {@code DefaultTableDefinition} object. + * Returns a builder for the {@code TableDefinition} object. */ @Override public Builder toBuilder() { @@ -252,7 +252,7 @@ ToStringHelper toStringHelper() { @Override public boolean equals(Object obj) { - return obj instanceof DefaultTableDefinition && baseEquals((DefaultTableDefinition) obj); + return obj instanceof TableDefinition && baseEquals((TableDefinition) obj); } @Override @@ -275,7 +275,7 @@ Table toPb() { } @SuppressWarnings("unchecked") - static DefaultTableDefinition fromPb(Table tablePb) { + static TableDefinition fromPb(Table tablePb) { return new Builder(tablePb).build(); } } 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 7e99e2850bdd..a2e3049f2188 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 @@ -30,9 +30,9 @@ import java.util.Objects; /** - * Google BigQuery table information. Use {@link DefaultTableDefinition} 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. + * Google BigQuery table information. Use {@link TableDefinition} 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 */ @@ -171,7 +171,9 @@ public Builder tableId(TableId tableId) { } /** - * Sets the table definition. + * Sets the table definition. Use {@link TableDefinition} 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(BaseTableDefinition definition) { this.definition = checkNotNull(definition); 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 c0cfb279cbcf..05024b46860f 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 @@ -26,7 +26,7 @@ * System.out.println("Creating table " + tableId); * Field integerField = Field.of("fieldName", Field.Type.integer()); * Schema schema = Schema.of(integerField); - * bigquery.create(TableInfo.of(tableId, DefaultTableDefinition.of(schema))); + * bigquery.create(TableInfo.of(tableId, TableDefinition.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 c0aa518a3270..20104bf5d857 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,11 +104,11 @@ public class BigQueryImplTest { .description("FieldDescription3") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); - private static final DefaultTableDefinition TABLE_TYPE = DefaultTableDefinition.of(TABLE_SCHEMA); - private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE); - private static final TableInfo OTHER_TABLE_INFO = TableInfo.of(OTHER_TABLE_ID, TABLE_TYPE); + private static final TableDefinition TABLE_DEFINITION = TableDefinition.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_TYPE); + 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 = 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 fcc9d0c5cc45..837736f5a395 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 @@ -44,8 +44,8 @@ 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 DefaultTableDefinition TABLE_DEFINITION = - DefaultTableDefinition.of(Schema.of(FIELD)); + private static final TableDefinition TABLE_DEFINITION = + TableDefinition.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()); 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 20aaba7dd293..ec921d5cf68f 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,21 +257,21 @@ public void testGetNonExistingTable() { public void testCreateAndGetTable() { String tableName = "test_create_and_get_table"; TableId tableId = TableId.of(DATASET, tableName); - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); assertEquals(tableName, createdTableInfo.tableId().table()); TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.definition() instanceof DefaultTableDefinition); + assertTrue(remoteTableInfo.definition() instanceof TableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); assertNotNull(remoteTableInfo.creationTime()); assertNotNull(remoteTableInfo.lastModifiedTime()); - assertNotNull(remoteTableInfo.definition().numBytes()); - assertNotNull(remoteTableInfo.definition().numRows()); + assertNotNull(remoteTableInfo.definition().numBytes()); + assertNotNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -279,7 +279,7 @@ public void testCreateAndGetTable() { public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -287,14 +287,14 @@ public void testCreateAndGetTableWithSelectedField() { TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName, TableOption.fields(TableField.CREATION_TIME)); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.definition() instanceof DefaultTableDefinition); + assertTrue(remoteTableInfo.definition() instanceof TableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertNotNull(remoteTableInfo.creationTime()); assertNull(remoteTableInfo.definition().schema()); assertNull(remoteTableInfo.lastModifiedTime()); - assertNull(remoteTableInfo.definition().numBytes()); - assertNull(remoteTableInfo.definition().numRows()); + assertNull(remoteTableInfo.definition().numBytes()); + assertNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -409,7 +409,7 @@ public void testCreateViewTable() throws InterruptedException { @Test public void testListTables() { String tableName = "test_list_tables"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -428,7 +428,7 @@ public void testListTables() { @Test public void testUpdateTable() { String tableName = "test_update_table"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -444,27 +444,27 @@ public void testUpdateTable() { @Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder().description("newDescr") .build(), TableOption.fields(TableField.DESCRIPTION)); - assertTrue(updatedTableInfo.definition() instanceof DefaultTableDefinition); + assertTrue(updatedTableInfo.definition() instanceof TableDefinition); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); assertEquals("newDescr", updatedTableInfo.description()); assertNull(updatedTableInfo.definition().schema()); assertNull(updatedTableInfo.lastModifiedTime()); - assertNull(updatedTableInfo.definition().numBytes()); - assertNull(updatedTableInfo.definition().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"), - DefaultTableDefinition.of(SIMPLE_SCHEMA)); + TableDefinition.of(SIMPLE_SCHEMA)); try { bigquery.update(tableInfo); fail("BigQueryException was expected"); @@ -484,7 +484,7 @@ public void testDeleteNonExistingTable() { @Test public void testInsertAll() { String tableName = "test_insert_all_table"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) @@ -516,7 +516,7 @@ public void testInsertAll() { @Test public void testInsertAllWithSuffix() throws InterruptedException { String tableName = "test_insert_all_with_suffix_table"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) @@ -557,7 +557,7 @@ public void testInsertAllWithSuffix() throws InterruptedException { @Test public void testInsertAllWithErrors() { String tableName = "test_insert_all_with_errors_table"; - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); assertNotNull(bigquery.create(tableInfo)); InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()) @@ -689,7 +689,7 @@ 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); - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -722,7 +722,7 @@ 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); - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -762,7 +762,7 @@ 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); - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); 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 acfba8b6d0f6..a0b931805645 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,8 +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 BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.of(TABLE_SCHEMA); - private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID, TABLE_TYPE); + private static final BaseTableDefinition TABLE_DEFINITION = TableDefinition.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/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 12f7c184087a..50880f15c603 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.DefaultTableDefinition.StreamingBuffer; +import com.google.gcloud.bigquery.TableDefinition.StreamingBuffer; import org.junit.Test; @@ -99,7 +99,7 @@ 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 ExternalTableDefinition EXTERNAL_TABLE_TYPE = + private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION = ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .ignoreUnknownValues(true) .maxBadRecords(42) @@ -108,26 +108,26 @@ public class SerializationTest { new UserDefinedFunction.InlineFunction("inline"); private static final UserDefinedFunction URI_FUNCTION = new UserDefinedFunction.UriFunction("URI"); - private static final BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.builder() + private static final BaseTableDefinition TABLE_DEFINITION = TableDefinition.builder() .schema(TABLE_SCHEMA) .location(LOCATION) .streamingBuffer(STREAMING_BUFFER) .build(); - private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_TYPE) + 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 BaseTableDefinition VIEW_TYPE = ViewDefinition.of("QUERY"); - private static final TableInfo VIEW_INFO = TableInfo.builder(TABLE_ID, VIEW_TYPE) + private static final BaseTableDefinition 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_TYPE) + TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -246,12 +246,13 @@ 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, TABLE_TYPE, EXTERNAL_TABLE_TYPE, - VIEW_TYPE, 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, + 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(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.maxResults(42L), BigQuery.JobOption.fields(), diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java similarity index 67% rename from gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java rename to gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java index d595d7bb4a6c..57884d337279 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/DefaultTableDefinitionTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/TableDefinitionTest.java @@ -19,11 +19,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.google.gcloud.bigquery.DefaultTableDefinition.StreamingBuffer; +import com.google.gcloud.bigquery.TableDefinition.StreamingBuffer; import org.junit.Test; -public class DefaultTableDefinitionTest { +public class TableDefinitionTest { private static final Field FIELD_SCHEMA1 = Field.builder("StringField", Field.Type.string()) @@ -45,8 +45,8 @@ public class DefaultTableDefinitionTest { 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 DefaultTableDefinition DEFAULT_TABLE_DEFINITION = - DefaultTableDefinition.builder() + private static final TableDefinition TABLE_DEFINITION = + TableDefinition.builder() .location(LOCATION) .numBytes(NUM_BYTES) .numRows(NUM_ROWS) @@ -56,45 +56,44 @@ public class DefaultTableDefinitionTest { @Test public void testToBuilder() { - compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, - DEFAULT_TABLE_DEFINITION.toBuilder().build()); - DefaultTableDefinition tableDefinition = DEFAULT_TABLE_DEFINITION.toBuilder() + compareTableDefinition(TABLE_DEFINITION, + TABLE_DEFINITION.toBuilder().build()); + TableDefinition tableDefinition = TABLE_DEFINITION.toBuilder() .location("EU") .build(); assertEquals("EU", tableDefinition.location()); tableDefinition = tableDefinition.toBuilder() .location(LOCATION) .build(); - compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, tableDefinition); + compareTableDefinition(TABLE_DEFINITION, tableDefinition); } @Test public void testToBuilderIncomplete() { - DefaultTableDefinition tableDefinition = DefaultTableDefinition.of(TABLE_SCHEMA); + TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); assertEquals(tableDefinition, tableDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(BaseTableDefinition.Type.TABLE, DEFAULT_TABLE_DEFINITION.type()); - assertEquals(TABLE_SCHEMA, DEFAULT_TABLE_DEFINITION.schema()); - assertEquals(LOCATION, DEFAULT_TABLE_DEFINITION.location()); - assertEquals(NUM_BYTES, DEFAULT_TABLE_DEFINITION.numBytes()); - assertEquals(NUM_ROWS, DEFAULT_TABLE_DEFINITION.numRows()); - assertEquals(STREAMING_BUFFER, DEFAULT_TABLE_DEFINITION.streamingBuffer()); + assertEquals(BaseTableDefinition.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( - BaseTableDefinition.fromPb(DEFAULT_TABLE_DEFINITION.toPb()) - instanceof DefaultTableDefinition); - compareDefaultTableDefinition(DEFAULT_TABLE_DEFINITION, - BaseTableDefinition.fromPb(DEFAULT_TABLE_DEFINITION.toPb())); + BaseTableDefinition.fromPb(TABLE_DEFINITION.toPb()) + instanceof TableDefinition); + compareTableDefinition(TABLE_DEFINITION, + BaseTableDefinition.fromPb(TABLE_DEFINITION.toPb())); } - private void compareDefaultTableDefinition(DefaultTableDefinition expected, - DefaultTableDefinition value) { + private void compareTableDefinition(TableDefinition expected, TableDefinition value) { assertEquals(expected, value); assertEquals(expected.schema(), value.schema()); assertEquals(expected.type(), value.type()); 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 615d401ae1fb..4187451889f0 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 @@ -55,9 +55,9 @@ public class TableInfoTest { private static final Long NUM_BYTES = 42L; private static final Long NUM_ROWS = 43L; private static final String LOCATION = "US"; - private static final DefaultTableDefinition.StreamingBuffer STREAMING_BUFFER = - new DefaultTableDefinition.StreamingBuffer(1L, 2L, 3L); - private static final DefaultTableDefinition DEFAULT_TABLE_TYPE = DefaultTableDefinition.builder() + private static final TableDefinition.StreamingBuffer STREAMING_BUFFER = + new TableDefinition.StreamingBuffer(1L, 2L, 3L); + private static final TableDefinition TABLE_DEFINITION = TableDefinition.builder() .location(LOCATION) .numBytes(NUM_BYTES) .numRows(NUM_ROWS) @@ -70,7 +70,7 @@ public class TableInfoTest { 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 ExternalTableDefinition EXTERNAL_TABLE_TYPE = + private static final ExternalTableDefinition EXTERNAL_TABLE_DEFINITION = ExternalTableDefinition.builder(SOURCE_URIS, TABLE_SCHEMA, CSV_OPTIONS) .compression(COMPRESSION) .ignoreUnknownValues(IGNORE_UNKNOWN_VALUES) @@ -83,7 +83,7 @@ public class TableInfoTest { private static final ViewDefinition VIEW_TYPE = ViewDefinition.builder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); - private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, DEFAULT_TABLE_TYPE) + private static final TableInfo TABLE_INFO = TableInfo.builder(TABLE_ID, TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -104,7 +104,7 @@ public class TableInfoTest { .selfLink(SELF_LINK) .build(); private static final TableInfo EXTERNAL_TABLE_INFO = - TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_TYPE) + TableInfo.builder(TABLE_ID, EXTERNAL_TABLE_DEFINITION) .creationTime(CREATION_TIME) .description(DESCRIPTION) .etag(ETAG) @@ -132,11 +132,11 @@ public void testToBuilder() { @Test public void testToBuilderIncomplete() { - TableInfo tableInfo = TableInfo.of(TABLE_ID, DEFAULT_TABLE_TYPE); + TableInfo tableInfo = TableInfo.of(TABLE_ID, TABLE_DEFINITION); assertEquals(tableInfo, tableInfo.toBuilder().build()); tableInfo = TableInfo.of(TABLE_ID, VIEW_TYPE); assertEquals(tableInfo, tableInfo.toBuilder().build()); - tableInfo = TableInfo.of(TABLE_ID, EXTERNAL_TABLE_TYPE); + tableInfo = TableInfo.of(TABLE_ID, EXTERNAL_TABLE_DEFINITION); assertEquals(tableInfo, tableInfo.toBuilder().build()); } @@ -150,7 +150,7 @@ public void testBuilder() { assertEquals(FRIENDLY_NAME, TABLE_INFO.friendlyName()); assertEquals(ID, TABLE_INFO.id()); assertEquals(LAST_MODIFIED_TIME, TABLE_INFO.lastModifiedTime()); - assertEquals(DEFAULT_TABLE_TYPE, TABLE_INFO.definition()); + assertEquals(TABLE_DEFINITION, TABLE_INFO.definition()); assertEquals(SELF_LINK, TABLE_INFO.selfLink()); assertEquals(TABLE_ID, VIEW_INFO.tableId()); assertEquals(VIEW_TYPE, VIEW_INFO.definition()); @@ -171,7 +171,7 @@ 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(EXTERNAL_TABLE_TYPE, EXTERNAL_TABLE_INFO.definition()); + assertEquals(EXTERNAL_TABLE_DEFINITION, EXTERNAL_TABLE_INFO.definition()); assertEquals(SELF_LINK, EXTERNAL_TABLE_INFO.selfLink()); } 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 48bdde5867fb..84f22672659e 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,8 +55,8 @@ 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 BaseTableDefinition TABLE_TYPE = DefaultTableDefinition.of(Schema.of(FIELD)); - private static final TableInfo TABLE_INFO = TableInfo.of(TABLE_ID1, TABLE_TYPE); + private static final BaseTableDefinition TABLE_DEFINITION = TableDefinition.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"))); 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 1f8648623d40..5e2f6199f87e 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 @@ -24,7 +24,7 @@ import com.google.gcloud.bigquery.CopyJobConfiguration; import com.google.gcloud.bigquery.DatasetId; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.DefaultTableDefinition; +import com.google.gcloud.bigquery.TableDefinition; import com.google.gcloud.bigquery.ExternalTableDefinition; import com.google.gcloud.bigquery.ExtractJobConfiguration; import com.google.gcloud.bigquery.Field; @@ -435,7 +435,7 @@ static Schema parseSchema(String[] args, int start, int end) { /** * This class demonstrates how to create a simple BigQuery Table (i.e. a table created from a - * {@link DefaultTableDefinition}). + * {@link TableDefinition}). * * @see Tables: insert * @@ -447,7 +447,7 @@ TableInfo parse(String... args) throws Exception { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - return TableInfo.of(tableId, DefaultTableDefinition.of(parseSchema(args, 2, args.length))); + return TableInfo.of(tableId, TableDefinition.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); } From 28a457e680c0ba41d841b056ad30c662956ddb78 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 2 Feb 2016 10:35:59 +0100 Subject: [PATCH 5/5] Rename TableDefinition to StandardTableDefinition and BaseTableDefinition to TableDefinition --- README.md | 4 +- gcloud-java-bigquery/README.md | 8 +- .../gcloud/bigquery/BaseTableDefinition.java | 182 ------------ .../com/google/gcloud/bigquery/BigQuery.java | 12 +- .../com/google/gcloud/bigquery/Dataset.java | 6 +- .../bigquery/ExternalTableDefinition.java | 4 +- .../bigquery/StandardTableDefinition.java | 281 ++++++++++++++++++ .../gcloud/bigquery/TableDefinition.java | 279 ++++++----------- .../com/google/gcloud/bigquery/TableInfo.java | 26 +- .../gcloud/bigquery/ViewDefinition.java | 4 +- .../google/gcloud/bigquery/package-info.java | 2 +- .../gcloud/bigquery/BigQueryImplTest.java | 9 +- .../google/gcloud/bigquery/DatasetTest.java | 4 +- .../bigquery/ExternalTableDefinitionTest.java | 2 +- .../gcloud/bigquery/ITBigQueryTest.java | 46 +-- .../gcloud/bigquery/InsertAllRequestTest.java | 2 +- .../gcloud/bigquery/SerializationTest.java | 6 +- .../gcloud/bigquery/TableDefinitionTest.java | 26 +- .../google/gcloud/bigquery/TableInfoTest.java | 6 +- .../com/google/gcloud/bigquery/TableTest.java | 3 +- .../gcloud/bigquery/ViewDefinitionTest.java | 8 +- .../gcloud/examples/BigQueryExample.java | 6 +- 22 files changed, 461 insertions(+), 465 deletions(-) delete mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java create mode 100644 gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/StandardTableDefinition.java diff --git a/README.md b/README.md index c98949371215..d269996bd1de 100644 --- a/README.md +++ b/README.md @@ -127,12 +127,12 @@ must [supply credentials](#authentication) and a project ID if running this snip ```java import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; -import com.google.gcloud.bigquery.TableDefinition; 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; @@ -143,7 +143,7 @@ if (info == null) { System.out.println("Creating table " + tableId); Field integerField = Field.of("fieldName", Field.Type.integer()); Schema schema = Schema.of(integerField); - bigquery.create(TableInfo.of(tableId, TableDefinition.of(schema))); + 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 11550b8c2351..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.TableDefinition; 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,7 @@ Field stringField = Field.of("StringField", Field.Type.string()); // Table schema definition Schema schema = Schema.of(stringField); // Create a table -TableDefinition tableDefinition = TableDefinition.of(schema); +StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); ``` @@ -208,7 +208,6 @@ display on your webpage. import com.google.gcloud.bigquery.BigQuery; import com.google.gcloud.bigquery.BigQueryOptions; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.TableDefinition; import com.google.gcloud.bigquery.Field; import com.google.gcloud.bigquery.FieldValue; import com.google.gcloud.bigquery.InsertAllRequest; @@ -216,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; @@ -241,7 +241,7 @@ public class GcloudBigQueryExample { // Table schema definition Schema schema = Schema.of(stringField); // Create a table - TableDefinition tableDefinition = TableDefinition.of(schema); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); // Define rows to insert diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java deleted file mode 100644 index 908bb1199d36..000000000000 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableDefinition.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * 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 BaseTableDefinition implements Serializable { - - private static final long serialVersionUID = -374760330662959529L; - - /** - * The table type. - */ - public enum Type { - /** - * A normal BigQuery table. Instances of {@code BaseTableDefinition} for this type are - * implemented by {@link TableDefinition}. - */ - TABLE, - - /** - * A virtual table defined by a SQL query. Instances of {@code BaseTableDefinition} for this - * type are implemented by {@link ViewDefinition}. - * - * @see Views - */ - VIEW, - - /** - * A BigQuery table backed by external data. Instances of {@code BaseTableDefinition} for this - * type are implemented by {@link ExternalTableDefinition}. - * - * @see Federated Data - * Sources - */ - EXTERNAL - } - - private final Type type; - private final Schema schema; - - /** - * 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(BaseTableDefinition 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(); - } - - BaseTableDefinition(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(BaseTableDefinition 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) TableDefinition.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/BigQuery.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java index 53a07e8a67c3..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 TableInfo#tableId()} and - * {@link TableInfo#definition()} 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)); @@ -562,9 +562,9 @@ TableInfo getTable(TableId tableId, TableOption... options) /** * Lists the tables in the dataset. This method returns partial information on each table - * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, - * {@link TableInfo#id()} and {@link TableInfo#definition()}). 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 @@ -575,7 +575,7 @@ Page listTables(String datasetId, TableListOption... options) /** * Lists the tables in the dataset. This method returns partial information on each table * ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and - * {@link TableInfo#definition()}). To get complete information use either + * type, which is part of {@link TableInfo#definition()}). To get complete information use either * {@link #getTable(TableId, TableOption...)} or * {@link #getTable(String, String, TableOption...)}. * 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 647631c1d75e..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 @@ -211,8 +211,7 @@ public Page
list(BigQuery.TableListOption... options) { * @throws BigQueryException upon failure */ public Table get(String table, BigQuery.TableOption... options) { - TableInfo 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; } @@ -225,8 +224,7 @@ public Table get(String table, BigQuery.TableOption... options) { * @return a {@code Table} object for the created table * @throws BigQueryException upon failure */ - public Table create(String table, BaseTableDefinition definition, - BigQuery.TableOption... options) { + 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/ExternalTableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java index 52384ae08cc3..882b1eb7065f 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableDefinition.java @@ -35,7 +35,7 @@ * @see Federated Data Sources * */ -public class ExternalTableDefinition extends BaseTableDefinition { +public class ExternalTableDefinition extends TableDefinition { static final Function FROM_EXTERNAL_DATA_FUNCTION = @@ -63,7 +63,7 @@ public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) { private final String compression; public static final class Builder - extends BaseTableDefinition.Builder { + extends TableDefinition.Builder { private List sourceUris; private FormatOptions formatOptions; 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/TableDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableDefinition.java index 1a82fd6756be..de858f54ac43 100644 --- 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 @@ -16,266 +16,167 @@ package com.google.gcloud.bigquery; -import com.google.api.services.bigquery.model.Streamingbuffer; +import static com.google.common.base.Preconditions.checkNotNull; + 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 + * Base class for a Google BigQuery table type. */ -public class TableDefinition extends BaseTableDefinition { +public abstract class TableDefinition implements Serializable { - private static final long serialVersionUID = 2113445776046717900L; + private static final long serialVersionUID = -374760330662959529L; - private final Long numBytes; - private final Long numRows; - private final String location; - private final StreamingBuffer streamingBuffer; + private final Type type; + private final Schema schema; /** - * 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. + * The table type. */ - 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; - } - + public enum Type { /** - * Returns a lower-bound estimate of the number of rows currently in the streaming buffer. + * A normal BigQuery table. Instances of {@code TableDefinition} for this type are implemented + * by {@link StandardTableDefinition}. */ - public long estimatedRows() { - return estimatedRows; - } + TABLE, /** - * Returns a lower-bound estimate of the number of bytes currently in the streaming buffer. + * A virtual table defined by a SQL query. Instances of {@code TableDefinition} for this type + * are implemented by {@link ViewDefinition}. + * + * @see Views */ - public long estimatedBytes() { - return estimatedBytes; - } + VIEW, /** - * Returns the timestamp of the oldest entry in the streaming buffer, in milliseconds since - * epoch. + * A BigQuery table backed by external data. Instances of {@code TableDefinition} for this type + * are implemented by {@link ExternalTableDefinition}. + * + * @see Federated Data + * Sources */ - 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()); - } + EXTERNAL } - public static final class Builder - extends BaseTableDefinition.Builder { + /** + * Base builder for table types. + * + * @param the table type class + * @param the table type builder + */ + public abstract static class Builder> { - private Long numBytes; - private Long numRows; - private String location; - private StreamingBuffer streamingBuffer; + private Type type; + private Schema schema; - private Builder() { - super(Type.TABLE); + Builder(Type type) { + this.type = type; } - private Builder(TableDefinition tableDefinition) { - super(tableDefinition); - this.numBytes = tableDefinition.numBytes; - this.numRows = tableDefinition.numRows; - this.location = tableDefinition.location; - this.streamingBuffer = tableDefinition.streamingBuffer; + Builder(TableDefinition tableDefinition) { + this.type = tableDefinition.type; + this.schema = tableDefinition.schema; } - 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(Table tablePb) { + this.type = Type.valueOf(tablePb.getType()); + if (tablePb.getSchema() != null) { + this.schema(Schema.fromPb(tablePb.getSchema())); } } - Builder numBytes(Long numBytes) { - this.numBytes = numBytes; - return self(); - } - - Builder numRows(Long numRows) { - this.numRows = numRows; - return self(); + @SuppressWarnings("unchecked") + B self() { + return (B) this; } - Builder location(String location) { - this.location = location; + B type(Type type) { + this.type = type; return self(); } - Builder streamingBuffer(StreamingBuffer streamingBuffer) { - this.streamingBuffer = streamingBuffer; + /** + * Sets the table schema. + */ + public B schema(Schema schema) { + this.schema = checkNotNull(schema); return self(); } /** - * Creates a {@code TableDefinition} object. + * Creates an object. */ - @Override - public TableDefinition build() { - return new TableDefinition(this); - } - } - - private TableDefinition(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; + public abstract T build(); } - /** - * Returns the geographic location where the table should reside. This value is inherited from the - * dataset. - * - * @see - * Dataset Location - */ - public String location() { - return location; + TableDefinition(Builder builder) { + this.type = builder.type; + this.schema = builder.schema; } /** - * Returns information on the table's streaming buffer if any exists. Returns {@code null} if no - * streaming buffer exists. + * 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 StreamingBuffer streamingBuffer() { - return streamingBuffer; + public Type type() { + return type; } /** - * Returns a builder for a BigQuery default table type. + * Returns the table's schema. */ - public static Builder builder() { - return new Builder(); + public Schema schema() { + return schema; } /** - * Creates a BigQuery default table type given its schema. - * - * @param schema the schema of the table + * Returns a builder for the object. */ - public static TableDefinition of(Schema schema) { - return builder().schema(schema).build(); - } + public abstract Builder toBuilder(); - /** - * Returns a builder for the {@code TableDefinition} object. - */ - @Override - public Builder toBuilder() { - return new Builder(this); + MoreObjects.ToStringHelper toStringHelper() { + return MoreObjects.toStringHelper(this).add("type", type).add("schema", schema); } @Override - ToStringHelper toStringHelper() { - return super.toStringHelper() - .add("numBytes", numBytes) - .add("numRows", numRows) - .add("location", location) - .add("streamingBuffer", streamingBuffer); + public String toString() { + return toStringHelper().toString(); } - @Override - public boolean equals(Object obj) { - return obj instanceof TableDefinition && baseEquals((TableDefinition) obj); + final int baseHashCode() { + return Objects.hash(type); } - @Override - public int hashCode() { - return Objects.hash(baseHashCode(), numBytes, numRows, location, streamingBuffer); + final boolean baseEquals(TableDefinition jobConfiguration) { + return Objects.equals(toPb(), jobConfiguration.toPb()); } - @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()); + Table tablePb = new Table(); + if (schema != null) { + tablePb.setSchema(schema.toPb()); } + tablePb.setType(type.name()); return tablePb; } @SuppressWarnings("unchecked") - static TableDefinition fromPb(Table tablePb) { - return new Builder(tablePb).build(); + 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 a2e3049f2188..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 @@ -30,9 +30,9 @@ import java.util.Objects; /** - * Google BigQuery table information. Use {@link TableDefinition} 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. + * 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 */ @@ -64,7 +64,7 @@ public Table apply(TableInfo tableInfo) { private final Long creationTime; private final Long expirationTime; private final Long lastModifiedTime; - private final BaseTableDefinition definition; + private final TableDefinition definition; /** * Builder for tables. @@ -80,7 +80,7 @@ public static class Builder { private Long creationTime; private Long expirationTime; private Long lastModifiedTime; - private BaseTableDefinition definition; + private TableDefinition definition; private Builder() {} @@ -109,7 +109,7 @@ private Builder(Table tablePb) { this.etag = tablePb.getEtag(); this.id = tablePb.getId(); this.selfLink = tablePb.getSelfLink(); - this.definition = BaseTableDefinition.fromPb(tablePb); + this.definition = TableDefinition.fromPb(tablePb); } Builder creationTime(Long creationTime) { @@ -171,11 +171,11 @@ public Builder tableId(TableId tableId) { } /** - * Sets the table definition. Use {@link TableDefinition} 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. + * 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(BaseTableDefinition definition) { + public Builder definition(TableDefinition definition) { this.definition = checkNotNull(definition); return this; } @@ -270,7 +270,7 @@ public Long lastModifiedTime() { * Returns the table definition. */ @SuppressWarnings("unchecked") - public T definition() { + public T definition() { return (T) definition; } @@ -313,14 +313,14 @@ public boolean equals(Object obj) { /** * Returns a builder for a {@code TableInfo} object given table identity and definition. */ - public static Builder builder(TableId tableId, BaseTableDefinition 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, BaseTableDefinition definition) { + public static TableInfo of(TableId tableId, TableDefinition definition) { return builder(tableId, definition).build(); } diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java index 1358dc9eaecd..7065bcc155d2 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewDefinition.java @@ -32,14 +32,14 @@ * * @see Views */ -public final class ViewDefinition extends BaseTableDefinition { +public final class ViewDefinition extends TableDefinition { private static final long serialVersionUID = -8789311196910794545L; private final String query; private final List userDefinedFunctions; - public static final class Builder extends BaseTableDefinition.Builder { + public static final class Builder extends TableDefinition.Builder { private String query; private List userDefinedFunctions; 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 05024b46860f..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 @@ -26,7 +26,7 @@ * System.out.println("Creating table " + tableId); * Field integerField = Field.of("fieldName", Field.Type.integer()); * Schema schema = Schema.of(integerField); - * bigquery.create(TableInfo.of(tableId, TableDefinition.of(schema))); + * 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 20104bf5d857..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,7 +104,8 @@ public class BigQueryImplTest { .description("FieldDescription3") .build(); private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); - private static final TableDefinition TABLE_DEFINITION = TableDefinition.of(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 = @@ -513,7 +514,7 @@ public void testGetTableWithSelectedFields() { public void testListTables() { String cursor = "cursor"; ImmutableList tableList = - ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); + ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); Tuple> result = Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result); @@ -528,7 +529,7 @@ public void testListTables() { public void testListTablesFromDatasetId() { String cursor = "cursor"; ImmutableList tableList = - ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); + ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); Tuple> result = Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(DATASET, EMPTY_RPC_OPTIONS)).andReturn(result); @@ -543,7 +544,7 @@ public void testListTablesFromDatasetId() { public void testListTablesWithOptions() { String cursor = "cursor"; ImmutableList tableList = - ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); + ImmutableList.of(TABLE_INFO_WITH_PROJECT, OTHER_TABLE_INFO); Tuple> result = Tuple.of(cursor, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(DATASET, TABLE_LIST_OPTIONS)).andReturn(result); 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 837736f5a395..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 @@ -44,8 +44,8 @@ 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 TableDefinition TABLE_DEFINITION = - TableDefinition.of(Schema.of(FIELD)); + 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()); diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java index 8a821bf32d4e..247032dff890 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/ExternalTableDefinitionTest.java @@ -76,7 +76,7 @@ public void testToBuilderIncomplete() { @Test public void testBuilder() { - assertEquals(BaseTableDefinition.Type.EXTERNAL, EXTERNAL_TABLE_DEFINITION.type()); + 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()); 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 ec921d5cf68f..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,21 +257,21 @@ public void testGetNonExistingTable() { public void testCreateAndGetTable() { String tableName = "test_create_and_get_table"; TableId tableId = TableId.of(DATASET, tableName); - TableDefinition tableDefinition = TableDefinition.of(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()); TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.definition() instanceof TableDefinition); + assertTrue(remoteTableInfo.definition() instanceof StandardTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); + assertEquals(TableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertEquals(TABLE_SCHEMA, remoteTableInfo.definition().schema()); assertNotNull(remoteTableInfo.creationTime()); assertNotNull(remoteTableInfo.lastModifiedTime()); - assertNotNull(remoteTableInfo.definition().numBytes()); - assertNotNull(remoteTableInfo.definition().numRows()); + assertNotNull(remoteTableInfo.definition().numBytes()); + assertNotNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -279,7 +279,7 @@ public void testCreateAndGetTable() { public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition)); assertNotNull(createdTableInfo); assertEquals(DATASET, createdTableInfo.tableId().dataset()); @@ -287,14 +287,14 @@ public void testCreateAndGetTableWithSelectedField() { TableInfo remoteTableInfo = bigquery.getTable(DATASET, tableName, TableOption.fields(TableField.CREATION_TIME)); assertNotNull(remoteTableInfo); - assertTrue(remoteTableInfo.definition() instanceof TableDefinition); + assertTrue(remoteTableInfo.definition() instanceof StandardTableDefinition); assertEquals(createdTableInfo.tableId(), remoteTableInfo.tableId()); - assertEquals(BaseTableDefinition.Type.TABLE, remoteTableInfo.definition().type()); + assertEquals(TableDefinition.Type.TABLE, remoteTableInfo.definition().type()); assertNotNull(remoteTableInfo.creationTime()); assertNull(remoteTableInfo.definition().schema()); assertNull(remoteTableInfo.lastModifiedTime()); - assertNull(remoteTableInfo.definition().numBytes()); - assertNull(remoteTableInfo.definition().numRows()); + assertNull(remoteTableInfo.definition().numBytes()); + assertNull(remoteTableInfo.definition().numRows()); assertTrue(bigquery.delete(DATASET, tableName)); } @@ -409,7 +409,7 @@ public void testCreateViewTable() throws InterruptedException { @Test public void testListTables() { String tableName = "test_list_tables"; - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -428,7 +428,7 @@ public void testListTables() { @Test public void testUpdateTable() { String tableName = "test_update_table"; - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -444,27 +444,27 @@ public void testUpdateTable() { @Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); TableInfo updatedTableInfo = bigquery.update(tableInfo.toBuilder().description("newDescr") .build(), TableOption.fields(TableField.DESCRIPTION)); - assertTrue(updatedTableInfo.definition() instanceof TableDefinition); + assertTrue(updatedTableInfo.definition() instanceof StandardTableDefinition); assertEquals(DATASET, updatedTableInfo.tableId().dataset()); assertEquals(tableName, updatedTableInfo.tableId().table()); assertEquals("newDescr", updatedTableInfo.description()); assertNull(updatedTableInfo.definition().schema()); assertNull(updatedTableInfo.lastModifiedTime()); - assertNull(updatedTableInfo.definition().numBytes()); - assertNull(updatedTableInfo.definition().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"), - TableDefinition.of(SIMPLE_SCHEMA)); + StandardTableDefinition.of(SIMPLE_SCHEMA)); try { bigquery.update(tableInfo); fail("BigQueryException was expected"); @@ -484,7 +484,7 @@ public void testDeleteNonExistingTable() { @Test public void testInsertAll() { String tableName = "test_insert_all_table"; - TableDefinition tableDefinition = TableDefinition.of(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()) @@ -516,7 +516,7 @@ public void testInsertAll() { @Test public void testInsertAllWithSuffix() throws InterruptedException { String tableName = "test_insert_all_with_suffix_table"; - TableDefinition tableDefinition = TableDefinition.of(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()) @@ -557,7 +557,7 @@ public void testInsertAllWithSuffix() throws InterruptedException { @Test public void testInsertAllWithErrors() { String tableName = "test_insert_all_with_errors_table"; - TableDefinition tableDefinition = TableDefinition.of(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()) @@ -689,7 +689,7 @@ 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); - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -722,7 +722,7 @@ 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); - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); @@ -762,7 +762,7 @@ 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); - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition); TableInfo createdTableInfo = bigquery.create(tableInfo); assertNotNull(createdTableInfo); 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 a0b931805645..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,7 @@ 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 BaseTableDefinition TABLE_DEFINITION = TableDefinition.of(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; 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 50880f15c603..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.TableDefinition.StreamingBuffer; +import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; import org.junit.Test; @@ -108,7 +108,7 @@ public class SerializationTest { new UserDefinedFunction.InlineFunction("inline"); private static final UserDefinedFunction URI_FUNCTION = new UserDefinedFunction.UriFunction("URI"); - private static final BaseTableDefinition TABLE_DEFINITION = TableDefinition.builder() + private static final TableDefinition TABLE_DEFINITION = StandardTableDefinition.builder() .schema(TABLE_SCHEMA) .location(LOCATION) .streamingBuffer(STREAMING_BUFFER) @@ -119,7 +119,7 @@ public class SerializationTest { .etag(ETAG) .id(ID) .build(); - private static final BaseTableDefinition VIEW_DEFINITION = ViewDefinition.of("QUERY"); + 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) 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 index 57884d337279..d1e3635d00cb 100644 --- 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 @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.google.gcloud.bigquery.TableDefinition.StreamingBuffer; +import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; import org.junit.Test; @@ -45,8 +45,8 @@ public class TableDefinitionTest { 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 TableDefinition TABLE_DEFINITION = - TableDefinition.builder() + private static final StandardTableDefinition TABLE_DEFINITION = + StandardTableDefinition.builder() .location(LOCATION) .numBytes(NUM_BYTES) .numRows(NUM_ROWS) @@ -56,11 +56,8 @@ public class TableDefinitionTest { @Test public void testToBuilder() { - compareTableDefinition(TABLE_DEFINITION, - TABLE_DEFINITION.toBuilder().build()); - TableDefinition tableDefinition = TABLE_DEFINITION.toBuilder() - .location("EU") - .build(); + compareTableDefinition(TABLE_DEFINITION, TABLE_DEFINITION.toBuilder().build()); + StandardTableDefinition tableDefinition = TABLE_DEFINITION.toBuilder().location("EU").build(); assertEquals("EU", tableDefinition.location()); tableDefinition = tableDefinition.toBuilder() .location(LOCATION) @@ -70,13 +67,13 @@ public void testToBuilder() { @Test public void testToBuilderIncomplete() { - TableDefinition tableDefinition = TableDefinition.of(TABLE_SCHEMA); + StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); assertEquals(tableDefinition, tableDefinition.toBuilder().build()); } @Test public void testBuilder() { - assertEquals(BaseTableDefinition.Type.TABLE, TABLE_DEFINITION.type()); + assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.type()); assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.schema()); assertEquals(LOCATION, TABLE_DEFINITION.location()); assertEquals(NUM_BYTES, TABLE_DEFINITION.numBytes()); @@ -86,14 +83,13 @@ public void testBuilder() { @Test public void testToAndFromPb() { - assertTrue( - BaseTableDefinition.fromPb(TABLE_DEFINITION.toPb()) - instanceof TableDefinition); + assertTrue(TableDefinition.fromPb(TABLE_DEFINITION.toPb()) instanceof StandardTableDefinition); compareTableDefinition(TABLE_DEFINITION, - BaseTableDefinition.fromPb(TABLE_DEFINITION.toPb())); + TableDefinition.fromPb(TABLE_DEFINITION.toPb())); } - private void compareTableDefinition(TableDefinition expected, TableDefinition value) { + private void compareTableDefinition(StandardTableDefinition expected, + StandardTableDefinition value) { assertEquals(expected, value); assertEquals(expected.schema(), value.schema()); assertEquals(expected.type(), value.type()); 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 4187451889f0..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 @@ -55,9 +55,9 @@ public class TableInfoTest { private static final Long NUM_BYTES = 42L; private static final Long NUM_ROWS = 43L; private static final String LOCATION = "US"; - private static final TableDefinition.StreamingBuffer STREAMING_BUFFER = - new TableDefinition.StreamingBuffer(1L, 2L, 3L); - private static final TableDefinition TABLE_DEFINITION = TableDefinition.builder() + 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) 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 84f22672659e..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,8 @@ 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 BaseTableDefinition TABLE_DEFINITION = TableDefinition.of(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")), 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 index a3578fb4494e..ebab7a6e87ca 100644 --- 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 @@ -48,22 +48,22 @@ public void testToBuilder() { @Test public void testToBuilderIncomplete() { - BaseTableDefinition viewDefinition = ViewDefinition.of(VIEW_QUERY); + TableDefinition viewDefinition = ViewDefinition.of(VIEW_QUERY); assertEquals(viewDefinition, viewDefinition.toBuilder().build()); } @Test public void testBuilder() { assertEquals(VIEW_QUERY, VIEW_DEFINITION.query()); - assertEquals(BaseTableDefinition.Type.VIEW, VIEW_DEFINITION.type()); + assertEquals(TableDefinition.Type.VIEW, VIEW_DEFINITION.type()); assertEquals(USER_DEFINED_FUNCTIONS, VIEW_DEFINITION.userDefinedFunctions()); } @Test public void testToAndFromPb() { - assertTrue(BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb()) instanceof ViewDefinition); + assertTrue(TableDefinition.fromPb(VIEW_DEFINITION.toPb()) instanceof ViewDefinition); compareViewDefinition(VIEW_DEFINITION, - BaseTableDefinition.fromPb(VIEW_DEFINITION.toPb())); + TableDefinition.fromPb(VIEW_DEFINITION.toPb())); } private void compareViewDefinition(ViewDefinition expected, ViewDefinition value) { 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 5e2f6199f87e..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 @@ -24,7 +24,6 @@ import com.google.gcloud.bigquery.CopyJobConfiguration; import com.google.gcloud.bigquery.DatasetId; import com.google.gcloud.bigquery.DatasetInfo; -import com.google.gcloud.bigquery.TableDefinition; import com.google.gcloud.bigquery.ExternalTableDefinition; import com.google.gcloud.bigquery.ExtractJobConfiguration; import com.google.gcloud.bigquery.Field; @@ -37,6 +36,7 @@ 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.ViewDefinition; @@ -435,7 +435,7 @@ static Schema parseSchema(String[] args, int start, int end) { /** * This class demonstrates how to create a simple BigQuery Table (i.e. a table created from a - * {@link TableDefinition}). + * {@link StandardTableDefinition}). * * @see Tables: insert * @@ -447,7 +447,7 @@ TableInfo parse(String... args) throws Exception { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); - return TableInfo.of(tableId, TableDefinition.of(parseSchema(args, 2, args.length))); + return TableInfo.of(tableId, StandardTableDefinition.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); }