Skip to content

Commit 12185e8

Browse files
committed
Merge pull request #423 from mziccard/bigquery
Add TableInfo and related model classes
2 parents 4f05b98 + 9df4005 commit 12185e8

18 files changed

+3272
-1
lines changed

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

Lines changed: 502 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.bigquery;
18+
19+
import com.google.common.base.MoreObjects;
20+
21+
import java.nio.charset.Charset;
22+
import java.util.Objects;
23+
24+
/**
25+
* Google BigQuery options for CSV format. This class wraps some properties of CSV files used by
26+
* BigQuery to parse external data.
27+
*/
28+
public class CsvOptions extends FormatOptions {
29+
30+
private static final long serialVersionUID = 2193570529308612708L;
31+
32+
private final Boolean allowJaggedRows;
33+
private final Boolean allowQuotedNewLines;
34+
private final String encoding;
35+
private final String fieldDelimiter;
36+
private final String quote;
37+
private final Integer skipLeadingRows;
38+
39+
public static final class Builder {
40+
41+
private Boolean allowJaggedRows;
42+
private Boolean allowQuotedNewLines;
43+
private String encoding;
44+
private String fieldDelimiter;
45+
private String quote;
46+
private Integer skipLeadingRows;
47+
48+
private Builder() {}
49+
50+
/**
51+
* Set whether BigQuery should accept rows that are missing trailing optional columns. If
52+
* {@code true}, BigQuery treats missing trailing columns as null values. If {@code false},
53+
* records with missing trailing columns are treated as bad records, and if there are too many
54+
* bad records, an invalid error is returned in the job result. By default, rows with missing
55+
* trailing columns are considered bad records.
56+
*/
57+
public Builder allowJaggedRows(Boolean allowJaggedRows) {
58+
this.allowJaggedRows = allowJaggedRows;
59+
return this;
60+
}
61+
62+
/**
63+
* Sets whether BigQuery should allow quoted data sections that contain newline characters in a
64+
* CSV file. By default quoted newline are not allowed.
65+
*/
66+
public Builder allowQuotedNewLines(Boolean allowQuotedNewLines) {
67+
this.allowQuotedNewLines = allowQuotedNewLines;
68+
return this;
69+
}
70+
71+
/**
72+
* Sets the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
73+
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
74+
* using the values set in {@link #quote(String)} and {@link #fieldDelimiter(String)}.
75+
*/
76+
public Builder encoding(String encoding) {
77+
this.encoding = encoding;
78+
return this;
79+
}
80+
81+
/**
82+
* Sets the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
83+
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
84+
* using the values set in {@link #quote(String)} and {@link #fieldDelimiter(String)}.
85+
*/
86+
public Builder encoding(Charset encoding) {
87+
this.encoding = encoding.name();
88+
return this;
89+
}
90+
91+
/**
92+
* Sets the separator for fields in a CSV file. BigQuery converts the string to ISO-8859-1
93+
* encoding, and then uses the first byte of the encoded string to split the data in its raw,
94+
* binary state. BigQuery also supports the escape sequence "\t" to specify a tab separator.
95+
* The default value is a comma (',').
96+
*/
97+
public Builder fieldDelimiter(String fieldDelimiter) {
98+
this.fieldDelimiter = fieldDelimiter;
99+
return this;
100+
}
101+
102+
/**
103+
* Sets the value that is used to quote data sections in a CSV file. BigQuery converts the
104+
* string to ISO-8859-1 encoding, and then uses the first byte of the encoded string to split
105+
* the data in its raw, binary state. The default value is a double-quote ('"'). If your data
106+
* does not contain quoted sections, set the property value to an empty string. If your data
107+
* contains quoted newline characters, you must also set {@link #allowQuotedNewLines(Boolean)}
108+
* property to {@code true}.
109+
*/
110+
public Builder quote(String quote) {
111+
this.quote = quote;
112+
return this;
113+
}
114+
115+
/**
116+
* Sets the number of rows at the top of a CSV file that BigQuery will skip when reading the
117+
* data. The default value is 0. This property is useful if you have header rows in the file
118+
* that should be skipped.
119+
*/
120+
public Builder skipLeadingRows(Integer skipLeadingRows) {
121+
this.skipLeadingRows = skipLeadingRows;
122+
return this;
123+
}
124+
125+
/**
126+
* Creates a {@code CsvOptions} object.
127+
*/
128+
public CsvOptions build() {
129+
return new CsvOptions(this);
130+
}
131+
}
132+
133+
private CsvOptions(Builder builder) {
134+
super(FormatOptions.CSV);
135+
this.allowJaggedRows = builder.allowJaggedRows;
136+
this.allowQuotedNewLines = builder.allowQuotedNewLines;
137+
this.encoding = builder.encoding;
138+
this.fieldDelimiter = builder.fieldDelimiter;
139+
this.quote = builder.quote;
140+
this.skipLeadingRows = builder.skipLeadingRows;
141+
}
142+
143+
/**
144+
* Returns whether BigQuery should accept rows that are missing trailing optional columns. If
145+
* {@code true}, BigQuery treats missing trailing columns as null values. If {@code false},
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.
149+
*/
150+
public Boolean allowJaggedRows() {
151+
return allowJaggedRows;
152+
}
153+
154+
/**
155+
* Returns whether BigQuery should allow quoted data sections that contain newline characters in a
156+
* CSV file.
157+
*/
158+
public Boolean allowQuotedNewLines() {
159+
return allowQuotedNewLines;
160+
}
161+
162+
/**
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
165+
* using the values set in {@link #quote()} and {@link #fieldDelimiter()}.
166+
*/
167+
public String encoding() {
168+
return encoding;
169+
}
170+
171+
/**
172+
* Returns the separator for fields in a CSV file.
173+
*/
174+
public String fieldDelimiter() {
175+
return fieldDelimiter;
176+
}
177+
178+
/**
179+
* Returns the value that is used to quote data sections in a CSV file.
180+
*/
181+
public String quote() {
182+
return quote;
183+
}
184+
185+
/**
186+
* Returns the number of rows at the top of a CSV file that BigQuery will skip when reading the
187+
* data.
188+
*/
189+
public Integer skipLeadingRows() {
190+
return skipLeadingRows;
191+
}
192+
193+
public Builder toBuilder() {
194+
return new Builder()
195+
.allowJaggedRows(allowJaggedRows)
196+
.allowQuotedNewLines(allowQuotedNewLines)
197+
.encoding(encoding)
198+
.fieldDelimiter(fieldDelimiter)
199+
.quote(quote)
200+
.skipLeadingRows(skipLeadingRows);
201+
}
202+
203+
@Override
204+
public String toString() {
205+
return MoreObjects.toStringHelper(this)
206+
.add("type", type())
207+
.add("allowJaggedRows", allowJaggedRows)
208+
.add("allowQuotedNewLines", allowQuotedNewLines)
209+
.add("encoding", encoding)
210+
.add("fieldDelimiter", fieldDelimiter)
211+
.add("quote", quote)
212+
.add("skipLeadingRows", skipLeadingRows)
213+
.toString();
214+
}
215+
216+
@Override
217+
public int hashCode() {
218+
return Objects.hash(type(), allowJaggedRows, allowQuotedNewLines, encoding, fieldDelimiter,
219+
quote, skipLeadingRows);
220+
}
221+
222+
@Override
223+
public boolean equals(Object obj) {
224+
return obj instanceof CsvOptions && Objects.equals(toPb(), ((CsvOptions) obj).toPb());
225+
}
226+
227+
com.google.api.services.bigquery.model.CsvOptions toPb() {
228+
com.google.api.services.bigquery.model.CsvOptions csvOptions =
229+
new com.google.api.services.bigquery.model.CsvOptions();
230+
csvOptions.setAllowJaggedRows(allowJaggedRows);
231+
csvOptions.setAllowQuotedNewlines(allowQuotedNewLines);
232+
csvOptions.setEncoding(encoding);
233+
csvOptions.setFieldDelimiter(fieldDelimiter);
234+
csvOptions.setQuote(quote);
235+
csvOptions.setSkipLeadingRows(skipLeadingRows);
236+
return csvOptions;
237+
}
238+
239+
/**
240+
* Returns a builder for a CsvOptions object.
241+
*/
242+
public static Builder builder() {
243+
return new Builder();
244+
}
245+
246+
static CsvOptions fromPb(com.google.api.services.bigquery.model.CsvOptions csvOptions) {
247+
Builder builder = builder();
248+
if (csvOptions.getAllowJaggedRows() != null) {
249+
builder.allowJaggedRows(csvOptions.getAllowJaggedRows());
250+
}
251+
if (csvOptions.getAllowQuotedNewlines() != null) {
252+
builder.allowQuotedNewLines(csvOptions.getAllowQuotedNewlines());
253+
}
254+
if (csvOptions.getEncoding() != null) {
255+
builder.encoding(csvOptions.getEncoding());
256+
}
257+
if (csvOptions.getFieldDelimiter() != null) {
258+
builder.fieldDelimiter(csvOptions.getFieldDelimiter());
259+
}
260+
if (csvOptions.getQuote() != null) {
261+
builder.quote(csvOptions.getQuote());
262+
}
263+
if (csvOptions.getSkipLeadingRows() != null) {
264+
builder.skipLeadingRows(csvOptions.getSkipLeadingRows());
265+
}
266+
return builder.build();
267+
}
268+
}

0 commit comments

Comments
 (0)