forked from googleapis/java-bigquery
/
FormatOptions.java
136 lines (115 loc) · 4.03 KB
/
FormatOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright 2015 Google LLC
*
* 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.cloud.bigquery;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
/**
* Base class for Google BigQuery format options. These class define the format of external data
* used by BigQuery, for either federated tables or load jobs.
*
* <p>Load jobs support the following formats: AVRO, CSV, DATASTORE_BACKUP, GOOGLE_SHEETS, JSON,
* ORC, PARQUET
*
* <p>Federated tables can be defined against following formats: AVRO, BIGTABLE, CSV,
* DATASTORE_BACKUP, GOOGLE_SHEETS, JSON
*/
public class FormatOptions implements Serializable {
static final String CSV = "CSV";
static final String JSON = "NEWLINE_DELIMITED_JSON";
static final String BIGTABLE = "BIGTABLE";
static final String DATASTORE_BACKUP = "DATASTORE_BACKUP";
static final String AVRO = "AVRO";
static final String GOOGLE_SHEETS = "GOOGLE_SHEETS";
static final String PARQUET = "PARQUET";
static final String ORC = "ORC";
private static final long serialVersionUID = -443376052020423691L;
private final String type;
FormatOptions(String type) {
this.type = type;
}
/** Returns the external data format, as a string. */
public String getType() {
return type;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("format", type).toString();
}
@Override
public int hashCode() {
return Objects.hash(type);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(FormatOptions.class)
&& Objects.equals(type, ((FormatOptions) obj).getType());
}
/** Default options for CSV format. */
public static CsvOptions csv() {
return CsvOptions.newBuilder().build();
}
/** Default options for NEWLINE_DELIMITED_JSON format. */
public static FormatOptions json() {
return new FormatOptions(JSON);
}
/** Default options for DATASTORE_BACKUP format. */
public static FormatOptions datastoreBackup() {
return DatastoreBackupOptions.newBuilder().build();
}
/** Default options for AVRO format. */
public static AvroOptions avro() {
return AvroOptions.newBuilder().build();
}
/** Default options for BIGTABLE format. */
public static FormatOptions bigtable() {
return BigtableOptions.newBuilder().build();
}
/** Default options for GOOGLE_SHEETS format. */
public static FormatOptions googleSheets() {
return GoogleSheetsOptions.newBuilder().build();
}
/** Default options for PARQUET format. */
public static FormatOptions parquet() {
return new FormatOptions(PARQUET);
}
/** Default options for the ORC format. */
public static FormatOptions orc() {
return new FormatOptions(ORC);
}
/** Default options for the provided format. */
public static FormatOptions of(String format) {
checkArgument(!isNullOrEmpty(format), "Provided format is null or empty");
if (format.equals(CSV)) {
return csv();
} else if (format.equals(AVRO)) {
return avro();
} else if (format.equals(DATASTORE_BACKUP)) {
return datastoreBackup();
} else if (format.equals(GOOGLE_SHEETS)) {
return googleSheets();
} else if (format.equals(BIGTABLE)) {
return bigtable();
} else if (format.equals(PARQUET)) {
return parquet();
}
return new FormatOptions(format);
}
}