forked from googleapis/java-bigquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.java
370 lines (328 loc) · 10.8 KB
/
Dataset.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* 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.checkNotNull;
import com.google.api.gax.paging.Page;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQuery.DatasetOption;
import com.google.cloud.bigquery.BigQuery.TableListOption;
import com.google.cloud.bigquery.BigQuery.TableOption;
import com.google.common.base.Strings;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* A Google BigQuery Dataset.
*
* <p>Objects of this class are immutable. Operations that modify the dataset like {@link #update}
* return a new object. To get a {@code Dataset} object with the most recent information use {@link
* #reload}. {@code Dataset} adds a layer of service-related functionality over {@link DatasetInfo}.
*/
public class Dataset extends DatasetInfo {
private static final long serialVersionUID = -4272921483363065593L;
private final BigQueryOptions options;
private transient BigQuery bigquery;
/** A builder for {@code Dataset} objects. */
public static final class Builder extends DatasetInfo.Builder {
private final BigQuery bigquery;
private final DatasetInfo.BuilderImpl infoBuilder;
Builder(BigQuery bigquery, DatasetId datasetId) {
this.bigquery = bigquery;
this.infoBuilder = new DatasetInfo.BuilderImpl();
this.infoBuilder.setDatasetId(datasetId);
}
Builder(Dataset dataset) {
this.bigquery = dataset.bigquery;
this.infoBuilder = new DatasetInfo.BuilderImpl(dataset);
}
@Override
public Builder setDatasetId(DatasetId datasetId) {
infoBuilder.setDatasetId(datasetId);
return this;
}
@Override
public Builder setAcl(List<Acl> acl) {
infoBuilder.setAcl(acl);
return this;
}
@Override
Builder setCreationTime(Long creationTime) {
infoBuilder.setCreationTime(creationTime);
return this;
}
@Override
public Builder setDefaultTableLifetime(Long defaultTableLifetime) {
infoBuilder.setDefaultTableLifetime(defaultTableLifetime);
return this;
}
@Override
public Builder setDescription(String description) {
infoBuilder.setDescription(description);
return this;
}
@Override
Builder setEtag(String etag) {
infoBuilder.setEtag(etag);
return this;
}
@Override
public Builder setFriendlyName(String friendlyName) {
infoBuilder.setFriendlyName(friendlyName);
return this;
}
@Override
Builder setGeneratedId(String generatedId) {
infoBuilder.setGeneratedId(generatedId);
return this;
}
@Override
Builder setLastModified(Long lastModified) {
infoBuilder.setLastModified(lastModified);
return this;
}
@Override
public Builder setLocation(String location) {
infoBuilder.setLocation(location);
return this;
}
@Override
Builder setSelfLink(String selfLink) {
infoBuilder.setSelfLink(selfLink);
return this;
}
@Override
public Builder setLabels(Map<String, String> labels) {
infoBuilder.setLabels(labels);
return this;
}
@Override
public Builder setDefaultEncryptionConfiguration(
EncryptionConfiguration defaultEncryptionConfiguration) {
infoBuilder.setDefaultEncryptionConfiguration(defaultEncryptionConfiguration);
return this;
}
@Override
public Builder setDefaultPartitionExpirationMs(Long defaultPartitionExpirationMs) {
infoBuilder.setDefaultPartitionExpirationMs(defaultPartitionExpirationMs);
return this;
}
@Override
public Builder setDefaultCollation(String defaultCollation) {
infoBuilder.setDefaultCollation(defaultCollation);
return this;
}
@Override
public Builder setExternalDatasetReference(ExternalDatasetReference externalDatasetReference) {
infoBuilder.setExternalDatasetReference(externalDatasetReference);
return this;
}
@Override
public Builder setStorageBillingModel(String storageBillingModel) {
infoBuilder.setStorageBillingModel(storageBillingModel);
return this;
}
@Override
public Dataset build() {
return new Dataset(bigquery, infoBuilder);
}
}
Dataset(BigQuery bigquery, DatasetInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.bigquery = checkNotNull(bigquery);
this.options = bigquery.getOptions();
}
/**
* Checks if this dataset exists.
*
* <p>Example of checking whether a dataset exists.
*
* <pre>{@code
* boolean exists = dataset.exists();
* if (exists) {
* // the dataset exists
* } else {
* // the dataset was not found
* }
* }</pre>
*
* @return {@code true} if this dataset exists, {@code false} otherwise
* @throws BigQueryException upon failure
*/
public boolean exists() {
return bigquery.getDataset(getDatasetId(), DatasetOption.fields()) != null;
}
/**
* Fetches current dataset's latest information. Returns {@code null} if the dataset does not
* exist.
*
* <p>Example of reloading a dataset.
*
* <pre>{@code
* Dataset latestDataset = dataset.reload();
* if (latestDataset == null) {
* // The dataset was not found
* }
* }</pre>
*
* @param options dataset options
* @return a {@code Dataset} object with latest information or {@code null} if not found
* @throws BigQueryException upon failure
*/
public Dataset reload(DatasetOption... options) {
return bigquery.getDataset(getDatasetId().getDataset(), options);
}
/**
* Updates the dataset's information with this dataset's information. Dataset's user-defined id
* cannot be changed. A new {@code Dataset} object is returned.
*
* <p>Example of updating a dataset.
*
* <pre>{@code
* String friendlyName = "my_friendly_name";
* Builder builder = dataset.toBuilder();
* builder.setFriendlyName(friendlyName);
* Dataset updatedDataset = builder.build().update();
* }</pre>
*
* @param options dataset options
* @return a {@code Dataset} object with updated information
* @throws BigQueryException upon failure
*/
public Dataset update(DatasetOption... options) {
return bigquery.update(this, options);
}
/**
* Deletes this dataset.
*
* <p>Example of deleting a dataset.
*
* <pre>{@code
* boolean deleted = dataset.delete();
* if (deleted) {
* // The dataset was deleted
* } else {
* // The dataset was not found
* }
* }</pre>
*
* @return {@code true} if dataset was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
public boolean delete(DatasetDeleteOption... options) {
return bigquery.delete(getDatasetId(), options);
}
/**
* Returns the paginated list of tables in this dataset.
*
* <p>Example of listing tables in the dataset.
*
* <pre>{@code
* Page<Table> tables = dataset.list();
* for (Table table : tables.iterateAll()) {
* // do something with the table
* }
* }</pre>
*
* @param options options for listing tables
* @throws BigQueryException upon failure
*/
public Page<Table> list(TableListOption... options) {
return bigquery.listTables(getDatasetId(), options);
}
/**
* Returns the requested table in this dataset or {@code null} if not found.
*
* <p>Example of getting a table in the dataset.
*
* <pre>{@code
* String tableName = “my_table”;
* Table table = dataset.get(tableName);
* }</pre>
*
* @param tableId user-defined id of the requested table
* @param options table options
* @throws BigQueryException upon failure
*/
public Table get(String tableId, TableOption... options) {
// Adding the projectId used of getting the DataSet as a parameter for the issue:
// https://github.com/googleapis/java-bigquery/issues/1369
TableId tabId =
Strings.isNullOrEmpty(getDatasetId().getProject())
? TableId.of(getDatasetId().getDataset(), tableId)
: TableId.of(getDatasetId().getProject(), getDatasetId().getDataset(), tableId);
return bigquery.getTable(tabId, options);
}
/**
* Creates a new table in this dataset.
*
* <p>Example of creating a table in the dataset with schema and time partitioning.
*
* <pre>{@code
* String tableName = “my_table”;
* String fieldName = “my_field”;
* Schema schema = Schema.of(Field.of(fieldName, LegacySQLTypeName.STRING));
* StandardTableDefinition definition = StandardTableDefinition.newBuilder()
* .setSchema(schema)
* .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY))
* .build();
* Table table = dataset.create(tableName, definition);
* }</pre>
*
* @param tableId the table's user-defined id
* @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 tableId, TableDefinition definition, TableOption... options) {
TableInfo tableInfo =
TableInfo.of(TableId.of(getDatasetId().getDataset(), tableId), definition);
return bigquery.create(tableInfo, options);
}
/** Returns the dataset's {@code BigQuery} object used to issue requests. */
public BigQuery getBigQuery() {
return bigquery;
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(Dataset.class)) {
return false;
}
Dataset other = (Dataset) obj;
return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options);
}
@Override
public final int hashCode() {
return Objects.hash(super.hashCode(), options);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.bigquery = options.getService();
}
static Dataset fromPb(
BigQuery bigquery, com.google.api.services.bigquery.model.Dataset datasetPb) {
return new Dataset(bigquery, new DatasetInfo.BuilderImpl(datasetPb));
}
}