Skip to content

Commit 40f5455

Browse files
committed
Add TableInfo, CsvOptions, ExternalDataConfiguration and UserDefinedFunction models and tests
1 parent 3f0d511 commit 40f5455

13 files changed

Lines changed: 2685 additions & 1 deletion
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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.io.Serializable;
22+
import java.util.Objects;
23+
24+
/**
25+
* Google BigQuery CSV options. This class wraps some properties of CSV files used by BigQuery to
26+
* parse external data.
27+
*/
28+
public class CsvOptions implements Serializable {
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 separator for fields in a CSV file. BigQuery converts the string to ISO-8859-1
83+
* encoding, and then uses the first byte of the encoded string to split the data in its raw,
84+
* binary state. BigQuery also supports the escape sequence "\t" to specify a tab separator.
85+
* The default value is a comma (',').
86+
*/
87+
public Builder fieldDelimiter(String fieldDelimiter) {
88+
this.fieldDelimiter = fieldDelimiter;
89+
return this;
90+
}
91+
92+
/**
93+
* Sets the value that is used to quote data sections in a CSV file. BigQuery converts the
94+
* string to ISO-8859-1 encoding, and then uses the first byte of the encoded string to split
95+
* the data in its raw, binary state. The default value is a double-quote ('"'). If your data
96+
* does not contain quoted sections, set the property value to an empty string. If your data
97+
* contains quoted newline characters, you must also set {@link #allowQuotedNewLines(Boolean)}
98+
* property to {@code true}.
99+
*/
100+
public Builder quote(String quote) {
101+
this.quote = quote;
102+
return this;
103+
}
104+
105+
/**
106+
* Sets the number of rows at the top of a CSV file that BigQuery will skip when reading the
107+
* data. The default value is 0. This property is useful if you have header rows in the file
108+
* that should be skipped.
109+
*/
110+
public Builder skipLeadingRows(Integer skipLeadingRows) {
111+
this.skipLeadingRows = skipLeadingRows;
112+
return this;
113+
}
114+
115+
/**
116+
* Creates an {@code ExternalDataConfiguration} object.
117+
*/
118+
public CsvOptions build() {
119+
return new CsvOptions(this);
120+
}
121+
}
122+
123+
private CsvOptions(Builder builder) {
124+
this.allowJaggedRows = builder.allowJaggedRows;
125+
this.allowQuotedNewLines = builder.allowQuotedNewLines;
126+
this.encoding = builder.encoding;
127+
this.fieldDelimiter = builder.fieldDelimiter;
128+
this.quote = builder.quote;
129+
this.skipLeadingRows = builder.skipLeadingRows;
130+
}
131+
132+
/**
133+
* Returns whether BigQuery should accept rows that are missing trailing optional columns. If
134+
* {@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.
137+
*/
138+
public Boolean allowJaggedRows() {
139+
return allowJaggedRows;
140+
}
141+
142+
/**
143+
* Returns whether BigQuery should allow quoted data sections that contain newline characters in a
144+
* CSV file.
145+
*/
146+
public Boolean allowQuotedNewLines() {
147+
return allowQuotedNewLines;
148+
}
149+
150+
/**
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
153+
* using the values set in {@link #quote()} and {@link #fieldDelimiter()}.
154+
*/
155+
public String encoding() {
156+
return encoding;
157+
}
158+
159+
/**
160+
* Returns the separator for fields in a CSV file.
161+
*/
162+
public String fieldDelimiter() {
163+
return fieldDelimiter;
164+
}
165+
166+
/**
167+
* Returns the value that is used to quote data sections in a CSV file.
168+
*/
169+
public String quote() {
170+
return quote;
171+
}
172+
173+
/**
174+
* Returns the number of rows at the top of a CSV file that BigQuery will skip when reading the
175+
* data.
176+
*/
177+
public Integer skipLeadingRows() {
178+
return skipLeadingRows;
179+
}
180+
181+
public Builder toBuilder() {
182+
return new Builder()
183+
.allowJaggedRows(allowJaggedRows)
184+
.allowQuotedNewLines(allowQuotedNewLines)
185+
.encoding(encoding)
186+
.fieldDelimiter(fieldDelimiter)
187+
.quote(quote)
188+
.skipLeadingRows(skipLeadingRows);
189+
}
190+
191+
@Override
192+
public String toString() {
193+
return MoreObjects.toStringHelper(this)
194+
.add("allowJaggedRows", allowJaggedRows)
195+
.add("allowQuotedNewLines", allowQuotedNewLines)
196+
.add("encoding", encoding)
197+
.add("fieldDelimiter", fieldDelimiter)
198+
.add("quote", quote)
199+
.add("skipLeadingRows", skipLeadingRows)
200+
.toString();
201+
}
202+
203+
@Override
204+
public int hashCode() {
205+
return Objects.hash(allowJaggedRows, allowQuotedNewLines, encoding, fieldDelimiter, quote,
206+
skipLeadingRows);
207+
}
208+
209+
@Override
210+
public boolean equals(Object obj) {
211+
return obj instanceof CsvOptions && Objects.equals(toPb(), ((CsvOptions) obj).toPb());
212+
}
213+
214+
com.google.api.services.bigquery.model.CsvOptions toPb() {
215+
com.google.api.services.bigquery.model.CsvOptions csvOptions =
216+
new com.google.api.services.bigquery.model.CsvOptions();
217+
if (allowJaggedRows != null) {
218+
csvOptions.setAllowJaggedRows(allowJaggedRows);
219+
}
220+
if (allowQuotedNewLines != null) {
221+
csvOptions.setAllowQuotedNewlines(allowQuotedNewLines);
222+
}
223+
if (encoding != null) {
224+
csvOptions.setEncoding(encoding);
225+
}
226+
if (fieldDelimiter != null) {
227+
csvOptions.setFieldDelimiter(fieldDelimiter);
228+
}
229+
if (quote != null) {
230+
csvOptions.setQuote(quote);
231+
}
232+
if (skipLeadingRows != null) {
233+
csvOptions.setSkipLeadingRows(skipLeadingRows);
234+
}
235+
return csvOptions;
236+
}
237+
238+
/**
239+
* Returns a builder for a CsvOptions object.
240+
*/
241+
public static Builder builder() {
242+
return new Builder();
243+
}
244+
245+
static CsvOptions fromPb(com.google.api.services.bigquery.model.CsvOptions csvOptions) {
246+
Builder builder = builder();
247+
if (csvOptions.getAllowJaggedRows() != null) {
248+
builder.allowJaggedRows(csvOptions.getAllowJaggedRows());
249+
}
250+
if (csvOptions.getAllowQuotedNewlines() != null) {
251+
builder.allowQuotedNewLines(csvOptions.getAllowQuotedNewlines());
252+
}
253+
if (csvOptions.getEncoding() != null) {
254+
builder.encoding(csvOptions.getEncoding());
255+
}
256+
if (csvOptions.getFieldDelimiter() != null) {
257+
builder.fieldDelimiter(csvOptions.getFieldDelimiter());
258+
}
259+
if (csvOptions.getQuote() != null) {
260+
builder.quote(csvOptions.getQuote());
261+
}
262+
if (csvOptions.getSkipLeadingRows() != null) {
263+
builder.skipLeadingRows(csvOptions.getSkipLeadingRows());
264+
}
265+
return builder.build();
266+
}
267+
}

0 commit comments

Comments
 (0)