Skip to content

Commit b18666b

Browse files
committed
Refactor TableInfo and related classes
- TableSchema renamed to Schema - FieldSchema renamed to Field - Add class Type to Field to represent the field's type (and wrap record subfields) - Add factory method and setter from varargs for Schema - Better javadoc for CsvOptions and Field - Update tests according to changes
1 parent 8d2249e commit b18666b

14 files changed

Lines changed: 591 additions & 495 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.base.MoreObjects;
2020

2121
import java.io.Serializable;
22+
import java.nio.charset.Charset;
2223
import java.util.Objects;
2324

2425
/**
@@ -78,6 +79,16 @@ public Builder encoding(String encoding) {
7879
return this;
7980
}
8081

82+
/**
83+
* Sets the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
84+
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
85+
* using the values set in {@link #quote(String)} and {@link #fieldDelimiter(String)}.
86+
*/
87+
public Builder encoding(Charset encoding) {
88+
this.encoding = encoding.name();
89+
return this;
90+
}
91+
8192
/**
8293
* Sets the separator for fields in a CSV file. BigQuery converts the string to ISO-8859-1
8394
* encoding, and then uses the first byte of the encoded string to split the data in its raw,
@@ -113,7 +124,7 @@ public Builder skipLeadingRows(Integer skipLeadingRows) {
113124
}
114125

115126
/**
116-
* Creates an {@code ExternalDataConfiguration} object.
127+
* Creates a {@code CsvOptions} object.
117128
*/
118129
public CsvOptions build() {
119130
return new CsvOptions(this);
@@ -132,8 +143,9 @@ private CsvOptions(Builder builder) {
132143
/**
133144
* Returns whether BigQuery should accept rows that are missing trailing optional columns. If
134145
* {@code true}, BigQuery treats missing trailing columns as null values. If {@code false},
135-
* records with missing trailing columns are treated as bad records, and if there are too many
136-
* bad records, an invalid error is returned in the job result.
146+
* records with missing trailing columns are treated as bad records, and if the number of bad
147+
* records exceeds {@link ExternalDataConfiguration#maxBadRecords()}, an invalid error is returned
148+
* in the job result.
137149
*/
138150
public Boolean allowJaggedRows() {
139151
return allowJaggedRows;
@@ -148,8 +160,8 @@ public Boolean allowQuotedNewLines() {
148160
}
149161

150162
/**
151-
* Returns the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
152-
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
163+
* Returns the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. If
164+
* not set, UTF-8 is used. BigQuery decodes the data after the raw, binary data has been split
153165
* using the values set in {@link #quote()} and {@link #fieldDelimiter()}.
154166
*/
155167
public String encoding() {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public com.google.api.services.bigquery.model.ExternalDataConfiguration apply(
6262
private static final long serialVersionUID = -8004288831035566549L;
6363

6464
private final List<String> sourceUris;
65-
private final TableSchema schema;
65+
private final Schema schema;
6666
private final String sourceFormat;
6767
private final Integer maxBadRecords;
6868
private final Boolean ignoreUnknownValues;
@@ -72,7 +72,7 @@ public com.google.api.services.bigquery.model.ExternalDataConfiguration apply(
7272
public static final class Builder {
7373

7474
private List<String> sourceUris;
75-
private TableSchema schema;
75+
private Schema schema;
7676
private String sourceFormat;
7777
private Integer maxBadRecords;
7878
private Boolean ignoreUnknownValues;
@@ -97,13 +97,15 @@ public Builder sourceUris(List<String> sourceUris) {
9797
/**
9898
* Sets the schema for the external data.
9999
*/
100-
public Builder schema(TableSchema schema) {
100+
public Builder schema(Schema schema) {
101101
this.schema = checkNotNull(schema);
102102
return this;
103103
}
104104

105105
/**
106-
* Sets the source format of the external data.
106+
* Sets the source format of the external data. Supported values are {@code CSV} for CSV files,
107+
* and {@code NEWLINE_DELIMITED_JSON} for newline-delimited JSON. If not set, files are assumed
108+
* to be in CSV format.
107109
*
108110
* <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
109111
* Source Format</a>
@@ -212,7 +214,7 @@ public Integer maxBadRecords() {
212214
/**
213215
* Returns the schema for the external data.
214216
*/
215-
public TableSchema schema() {
217+
public Schema schema() {
216218
return schema;
217219
}
218220

@@ -327,7 +329,7 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() {
327329
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
328330
* Source Format</a>
329331
*/
330-
public static Builder builder(List<String> sourceUris, TableSchema schema, String format) {
332+
public static Builder builder(List<String> sourceUris, Schema schema, String format) {
331333
return new Builder().sourceUris(sourceUris).schema(schema).sourceFormat(format);
332334
}
333335

@@ -345,7 +347,7 @@ public static Builder builder(List<String> sourceUris, TableSchema schema, Strin
345347
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
346348
* Source Format</a>
347349
*/
348-
public static Builder builder(String sourceUri, TableSchema schema, String format) {
350+
public static Builder builder(String sourceUri, Schema schema, String format) {
349351
return new Builder()
350352
.sourceUris(ImmutableList.of(sourceUri))
351353
.schema(schema)
@@ -368,7 +370,7 @@ public static Builder builder(String sourceUri, TableSchema schema, String forma
368370
* Source Format</a>
369371
*/
370372
public static ExternalDataConfiguration of(
371-
List<String> sourceUris, TableSchema schema, String format) {
373+
List<String> sourceUris, Schema schema, String format) {
372374
return builder(sourceUris, schema, format).build();
373375
}
374376

@@ -386,7 +388,7 @@ public static ExternalDataConfiguration of(
386388
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
387389
* Source Format</a>
388390
*/
389-
public static ExternalDataConfiguration of(String sourceUri, TableSchema schema, String format) {
391+
public static ExternalDataConfiguration of(String sourceUri, Schema schema, String format) {
390392
return builder(sourceUri, schema, format).build();
391393
}
392394

@@ -397,7 +399,7 @@ static ExternalDataConfiguration fromPb(
397399
builder.sourceUris(externalDataConfiguration.getSourceUris());
398400
}
399401
if (externalDataConfiguration.getSchema() != null) {
400-
builder.schema(TableSchema.fromPb(externalDataConfiguration.getSchema()));
402+
builder.schema(Schema.fromPb(externalDataConfiguration.getSchema()));
401403
}
402404
if (externalDataConfiguration.getSourceFormat() != null) {
403405
builder.sourceFormat(externalDataConfiguration.getSourceFormat());

0 commit comments

Comments
 (0)