/
DatasetInfo.java
657 lines (582 loc) · 23.5 KB
/
DatasetInfo.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* 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.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.util.Data;
import com.google.api.services.bigquery.model.Dataset;
import com.google.api.services.bigquery.model.RoutineReference;
import com.google.api.services.bigquery.model.TableReference;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Google BigQuery Dataset information. A dataset is a grouping mechanism that holds zero or more
* tables. Datasets are the lowest level unit of access control; you cannot control access at the
* table level.
*
* @see <a href="https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects#datasets">
* Managing Jobs, Datasets, and Projects</a>
*/
public class DatasetInfo implements Serializable {
static final Function<Dataset, DatasetInfo> FROM_PB_FUNCTION =
new Function<Dataset, DatasetInfo>() {
@Override
public DatasetInfo apply(Dataset pb) {
return DatasetInfo.fromPb(pb);
}
};
static final Function<DatasetInfo, Dataset> TO_PB_FUNCTION =
new Function<DatasetInfo, Dataset>() {
@Override
public Dataset apply(DatasetInfo datasetInfo) {
return datasetInfo.toPb();
}
};
private static final long serialVersionUID = 8469473744160758489L;
private final DatasetId datasetId;
private final List<Acl> acl;
private final Long creationTime;
private final Long defaultTableLifetime;
private final String description;
private final String etag;
private final String friendlyName;
private final String generatedId;
private final Long lastModified;
private final String location;
private final String selfLink;
private final Labels labels;
private final EncryptionConfiguration defaultEncryptionConfiguration;
private final Long defaultPartitionExpirationMs;
private final String defaultCollation;
private final ExternalDatasetReference externalDatasetReference;
/** A builder for {@code DatasetInfo} objects. */
public abstract static class Builder {
/** Sets the dataset identity. */
public abstract Builder setDatasetId(DatasetId datasetId);
/**
* Sets the dataset's access control configuration.
*
* @see <a href="https://cloud.google.com/bigquery/access-control">Access Control</a>
*/
public abstract Builder setAcl(List<Acl> acl);
abstract Builder setCreationTime(Long creationTime);
/**
* Sets the default lifetime of all tables in the dataset, in milliseconds. The minimum value is
* 3600000 milliseconds (one hour). Once this property is set, all newly-created tables in the
* dataset will have an expirationTime property set to the creation time plus the value in this
* property, and changing the value will only affect new tables, not existing ones. When the
* expirationTime for a given table is reached, that table will be deleted automatically. If a
* table's expirationTime is modified or removed before the table expires, or if you provide an
* explicit expirationTime when creating a table, that value takes precedence over the default
* expiration time indicated by this property. This property is experimental and might be
* subject to change or removed.
*/
public abstract Builder setDefaultTableLifetime(Long defaultTableLifetime);
/** Sets a user-friendly description for the dataset. */
public abstract Builder setDescription(String description);
abstract Builder setEtag(String etag);
/** Sets a user-friendly name for the dataset. */
public abstract Builder setFriendlyName(String friendlyName);
abstract Builder setGeneratedId(String generatedId);
abstract Builder setLastModified(Long lastModified);
/**
* Sets the geographic location where the dataset should reside. This property is experimental
* and might be subject to change or removed.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets#location">Dataset
* Location</a>
*/
public abstract Builder setLocation(String location);
abstract Builder setSelfLink(String selfLink);
public abstract Builder setLabels(Map<String, String> labels);
/**
* Optional. Information about the external metadata storage where the dataset is defined.
* Filled out when the dataset type is EXTERNAL
*/
public abstract Builder setExternalDatasetReference(
ExternalDatasetReference externalDatasetReference);
/**
* The default encryption key for all tables in the dataset. Once this property is set, all
* newly-created partitioned tables in the dataset will have encryption key set to this value,
* unless table creation request (or query) overrides the key.
*/
public abstract Builder setDefaultEncryptionConfiguration(
EncryptionConfiguration defaultEncryptionConfiguration);
/**
* [Optional] The default partition expiration time for all partitioned tables in the dataset,
* in milliseconds. Once this property is set, all newly-created partitioned tables in the
* dataset will has an expirationMs property in the timePartitioning settings set to this value.
* Changing the value only affect new tables, not existing ones. The storage in a partition will
* have an expiration time of its partition time plus this value. Setting this property
* overrides the use of defaultTableExpirationMs for partitioned tables: only one of
* defaultTableExpirationMs and defaultPartitionExpirationMs will be used for any new
* partitioned table. If you provide an explicit timePartitioning.expirationMs when creating or
* updating a partitioned table, that value takes precedence over the default partition
* expiration time indicated by this property. The value may be {@code null}.
*/
public abstract Builder setDefaultPartitionExpirationMs(Long defaultPartitionExpirationMs);
/**
* Optional. Defines the default collation specification of future tables created in the
* dataset. If a table is created in this dataset without table-level default collation, then
* the table inherits the dataset default collation, which is applied to the string fields that
* do not have explicit collation specified. A change to this field affects only tables created
* afterwards, and does not alter the existing tables. The following values are supported:
*
* <p>* 'und:ci': undetermined locale, case insensitive. * '': empty string. Default to
* case-sensitive behavior. (-- A wrapper is used here because it is possible to set the value
* to the empty string. --) (-- api-linter: standard-fields=disabled --)
*/
public abstract Builder setDefaultCollation(String defaultCollation);
/** Creates a {@code DatasetInfo} object. */
public abstract DatasetInfo build();
}
static final class BuilderImpl extends Builder {
private DatasetId datasetId;
private List<Acl> acl;
private Long creationTime;
private Long defaultTableLifetime;
private String description;
private String etag;
private String friendlyName;
private String generatedId;
private Long lastModified;
private String location;
private String selfLink;
private Labels labels = Labels.ZERO;
private EncryptionConfiguration defaultEncryptionConfiguration;
private Long defaultPartitionExpirationMs;
private String defaultCollation;
private ExternalDatasetReference externalDatasetReference;
BuilderImpl() {}
BuilderImpl(DatasetInfo datasetInfo) {
this.datasetId = datasetInfo.datasetId;
this.acl = datasetInfo.acl;
this.creationTime = datasetInfo.creationTime;
this.defaultTableLifetime = datasetInfo.defaultTableLifetime;
this.description = datasetInfo.description;
this.etag = datasetInfo.etag;
this.friendlyName = datasetInfo.friendlyName;
this.generatedId = datasetInfo.generatedId;
this.lastModified = datasetInfo.lastModified;
this.location = datasetInfo.location;
this.selfLink = datasetInfo.selfLink;
this.labels = datasetInfo.labels;
this.defaultEncryptionConfiguration = datasetInfo.defaultEncryptionConfiguration;
this.defaultPartitionExpirationMs = datasetInfo.defaultPartitionExpirationMs;
this.defaultCollation = datasetInfo.defaultCollation;
this.externalDatasetReference = datasetInfo.externalDatasetReference;
}
BuilderImpl(com.google.api.services.bigquery.model.Dataset datasetPb) {
if (datasetPb.getDatasetReference() != null) {
this.datasetId = DatasetId.fromPb(datasetPb.getDatasetReference());
}
if (datasetPb.getAccess() != null) {
this.acl =
Lists.transform(
datasetPb.getAccess(),
new Function<Dataset.Access, Acl>() {
@Override
public Acl apply(Dataset.Access accessPb) {
return Acl.fromPb(accessPb);
}
});
}
this.creationTime = datasetPb.getCreationTime();
this.defaultTableLifetime = datasetPb.getDefaultTableExpirationMs();
this.description = datasetPb.getDescription();
this.etag = datasetPb.getEtag();
this.friendlyName = datasetPb.getFriendlyName();
this.generatedId = datasetPb.getId();
this.lastModified = datasetPb.getLastModifiedTime();
this.location = datasetPb.getLocation();
this.selfLink = datasetPb.getSelfLink();
this.labels = Labels.fromPb(datasetPb.getLabels());
if (datasetPb.getDefaultEncryptionConfiguration() != null) {
this.defaultEncryptionConfiguration =
new EncryptionConfiguration.Builder(datasetPb.getDefaultEncryptionConfiguration())
.build();
}
this.defaultPartitionExpirationMs = datasetPb.getDefaultPartitionExpirationMs();
this.defaultCollation = datasetPb.getDefaultCollation();
if (datasetPb.getExternalDatasetReference() != null) {
this.externalDatasetReference =
ExternalDatasetReference.fromPb(datasetPb.getExternalDatasetReference());
}
}
@Override
public Builder setDatasetId(DatasetId datasetId) {
this.datasetId = checkNotNull(datasetId);
return this;
}
@Override
public Builder setAcl(List<Acl> acl) {
this.acl = acl != null ? ImmutableList.copyOf(acl) : null;
return this;
}
@Override
Builder setCreationTime(Long creationTime) {
this.creationTime = creationTime;
return this;
}
@Override
public Builder setDefaultTableLifetime(Long defaultTableLifetime) {
this.defaultTableLifetime = firstNonNull(defaultTableLifetime, Data.<Long>nullOf(Long.class));
return this;
}
@Override
public Builder setDescription(String description) {
this.description = firstNonNull(description, Data.<String>nullOf(String.class));
return this;
}
@Override
Builder setEtag(String etag) {
this.etag = etag;
return this;
}
@Override
public Builder setFriendlyName(String friendlyName) {
this.friendlyName = firstNonNull(friendlyName, Data.<String>nullOf(String.class));
return this;
}
@Override
Builder setGeneratedId(String generatedId) {
this.generatedId = generatedId;
return this;
}
@Override
Builder setLastModified(Long lastModified) {
this.lastModified = lastModified;
return this;
}
@Override
public Builder setLocation(String location) {
this.location = firstNonNull(location, Data.<String>nullOf(String.class));
return this;
}
@Override
Builder setSelfLink(String selfLink) {
this.selfLink = selfLink;
return this;
}
/**
* Sets the labels applied to this dataset.
*
* <p>When used with {@link BigQuery#update(DatasetInfo, DatasetOption...)}, setting {@code
* labels} to {@code null} removes all labels; otherwise all keys that are mapped to {@code
* null} values are removed and other keys are updated to their respective values.
*/
@Override
public Builder setLabels(Map<String, String> labels) {
this.labels = Labels.fromUser(labels);
return this;
}
@Override
public Builder setDefaultEncryptionConfiguration(
EncryptionConfiguration defaultEncryptionConfiguration) {
this.defaultEncryptionConfiguration = defaultEncryptionConfiguration;
return this;
}
@Override
public Builder setDefaultPartitionExpirationMs(Long defaultPartitionExpirationMs) {
this.defaultPartitionExpirationMs = defaultPartitionExpirationMs;
return this;
}
@Override
public Builder setDefaultCollation(String defaultCollation) {
this.defaultCollation = defaultCollation;
return this;
}
@Override
public Builder setExternalDatasetReference(ExternalDatasetReference externalDatasetReference) {
this.externalDatasetReference = externalDatasetReference;
return this;
}
@Override
public DatasetInfo build() {
return new DatasetInfo(this);
}
}
DatasetInfo(BuilderImpl builder) {
datasetId = checkNotNull(builder.datasetId);
acl = builder.acl;
creationTime = builder.creationTime;
defaultTableLifetime = builder.defaultTableLifetime;
description = builder.description;
etag = builder.etag;
friendlyName = builder.friendlyName;
generatedId = builder.generatedId;
lastModified = builder.lastModified;
location = builder.location;
selfLink = builder.selfLink;
labels = builder.labels;
defaultEncryptionConfiguration = builder.defaultEncryptionConfiguration;
defaultPartitionExpirationMs = builder.defaultPartitionExpirationMs;
defaultCollation = builder.defaultCollation;
externalDatasetReference = builder.externalDatasetReference;
}
/** Returns the dataset identity. */
public DatasetId getDatasetId() {
return datasetId;
}
/**
* Returns the dataset's access control configuration.
*
* <p>Update the ACLs for a dataset.
*
* <pre>{@code
* Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
* List<Acl> beforeAcls = dataset.getAcl();
*
* // Make a copy of the ACLs so that they can be modified.
* ArrayList<Acl> acls = new ArrayList<>(beforeAcls);
* acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER));
* Dataset.Builder builder = dataset.toBuilder();
* builder.setAcl(acls);
*
* bigquery.update(builder.build()); // API request.
* }</pre>
*
* @see <a href="https://cloud.google.com/bigquery/access-control">Access Control</a>
*/
public List<Acl> getAcl() {
return acl;
}
/** Returns the time when this dataset was created, in milliseconds since the epoch. */
public Long getCreationTime() {
return creationTime;
}
/**
* Returns the default lifetime of all tables in the dataset, in milliseconds. Once this property
* is set, all newly-created tables in the dataset will have an expirationTime property set to the
* creation time plus the value in this property, and changing the value will only affect new
* tables, not existing ones. When the expirationTime for a given table is reached, that table
* will be deleted automatically. If a table's expirationTime is modified or removed before the
* table expires, or if you provide an explicit expirationTime when creating a table, that value
* takes precedence over the default expiration time indicated by this property.
*
* <p>Update the default table expiration time for a dataset.
*
* <pre>{@code
* Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
* Long beforeExpiration = dataset.getDefaultTableLifetime();
*
* Long oneDayMilliseconds = 24 * 60 * 60 * 1000L;
* Dataset.Builder builder = dataset.toBuilder();
* builder.setDefaultTableLifetime(oneDayMilliseconds);
* bigquery.update(builder.build()); // API request.
* }</pre>
*/
public Long getDefaultTableLifetime() {
return defaultTableLifetime;
}
/** Returns a user-friendly description for the dataset. */
public String getDescription() {
return description;
}
/** Returns the hash of the dataset resource. */
public String getEtag() {
return etag;
}
/** Returns a user-friendly name for the dataset. */
public String getFriendlyName() {
return friendlyName;
}
/** Returns the service-generated id for the dataset. */
public String getGeneratedId() {
return generatedId;
}
/**
* Returns the time when this dataset or any of its tables was last modified, in milliseconds
* since the epoch.
*/
public Long getLastModified() {
return lastModified;
}
/**
* Returns the geographic location where the dataset should reside.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects#dataset-location">
* Dataset Location</a>
*/
public String getLocation() {
return location;
}
/**
* Returns an URL that can be used to access the resource again. The returned URL can be used for
* get or update requests.
*/
public String getSelfLink() {
return selfLink;
}
/**
* Return a map for labels applied to the dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">Labeling Datasets</a>
*/
public Map<String, String> getLabels() {
return labels.userMap();
}
public EncryptionConfiguration getDefaultEncryptionConfiguration() {
return defaultEncryptionConfiguration;
}
public Long getDefaultPartitionExpirationMs() {
return defaultPartitionExpirationMs;
}
public String getDefaultCollation() {
return defaultCollation;
}
/**
* Returns information about the external metadata storage where the dataset is defined. Filled
* out when the dataset type is EXTERNAL.
*/
public ExternalDatasetReference getExternalDatasetReference() {
return externalDatasetReference;
}
/** Returns a builder for the dataset object. */
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("datasetId", datasetId)
.add("creationTime", creationTime)
.add("defaultTableLifetime", defaultTableLifetime)
.add("description", description)
.add("etag", etag)
.add("friendlyName", friendlyName)
.add("generatedId", generatedId)
.add("lastModified", lastModified)
.add("location", location)
.add("selfLink", selfLink)
.add("acl", acl)
.add("labels", labels)
.add("defaultEncryptionConfiguration", defaultEncryptionConfiguration)
.add("defaultPartitionExpirationMs", defaultPartitionExpirationMs)
.add("defaultCollation", defaultCollation)
.add("externalDatasetReference", externalDatasetReference)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(datasetId);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(DatasetInfo.class)
&& Objects.equals(toPb(), ((DatasetInfo) obj).toPb());
}
DatasetInfo setProjectId(String projectId) {
Builder builder = toBuilder();
builder.setDatasetId(getDatasetId().setProjectId(projectId));
if (getAcl() != null) {
List<Acl> acls = Lists.newArrayListWithCapacity(getAcl().size());
for (Acl acl : getAcl()) {
if (acl.getEntity().getType() == Acl.Entity.Type.VIEW) {
Dataset.Access accessPb = acl.toPb();
TableReference viewReferencePb = accessPb.getView();
if (viewReferencePb.getProjectId() == null) {
viewReferencePb.setProjectId(projectId);
}
acls.add(Acl.of(new Acl.View(TableId.fromPb(viewReferencePb))));
} else if (acl.getEntity().getType() == Acl.Entity.Type.ROUTINE) {
Dataset.Access accessPb = acl.toPb();
RoutineReference routineReferencePb = accessPb.getRoutine();
if (routineReferencePb.getProjectId() == null) {
routineReferencePb.setProjectId(projectId);
}
acls.add(Acl.of(new Acl.Routine(RoutineId.fromPb(routineReferencePb))));
} else {
acls.add(acl);
}
}
builder.setAcl(acls);
}
return builder.build();
}
Dataset toPb() {
Dataset datasetPb = new Dataset();
datasetPb.setDatasetReference(datasetId.toPb());
datasetPb.setCreationTime(creationTime);
datasetPb.setDefaultTableExpirationMs(defaultTableLifetime);
datasetPb.setDescription(description);
datasetPb.setEtag(etag);
datasetPb.setFriendlyName(friendlyName);
datasetPb.setId(generatedId);
datasetPb.setLastModifiedTime(lastModified);
datasetPb.setLocation(location);
datasetPb.setSelfLink(selfLink);
if (acl != null) {
datasetPb.setAccess(
Lists.transform(
acl,
new Function<Acl, Dataset.Access>() {
@Override
public Dataset.Access apply(Acl acl) {
return acl.toPb();
}
}));
}
datasetPb.setLabels(labels.toPb());
if (defaultEncryptionConfiguration != null) {
datasetPb.setDefaultEncryptionConfiguration(defaultEncryptionConfiguration.toPb());
}
if (defaultPartitionExpirationMs != null) {
datasetPb.setDefaultPartitionExpirationMs(defaultPartitionExpirationMs);
}
if (defaultCollation != null) {
datasetPb.setDefaultCollation(defaultCollation);
}
if (externalDatasetReference != null) {
datasetPb.setExternalDatasetReference(externalDatasetReference.toPb());
}
return datasetPb;
}
/** Returns a builder for a {@code DatasetInfo} object given it's identity. */
public static Builder newBuilder(DatasetId datasetId) {
return new BuilderImpl().setDatasetId(datasetId);
}
/** Returns a builder for a {@code DatasetInfo} object given it's user-defined id. */
public static Builder newBuilder(String datasetId) {
return newBuilder(DatasetId.of(datasetId));
}
/**
* Returns a builder for the DatasetInfo object given it's user-defined project and dataset ids.
*/
public static Builder newBuilder(String projectId, String datasetId) {
return newBuilder(DatasetId.of(projectId, datasetId));
}
/** Returns a {@code DatasetInfo} object given it's identity. */
public static DatasetInfo of(DatasetId datasetId) {
return newBuilder(datasetId).build();
}
/** Returns a {@code DatasetInfo} object given it's user-defined id. */
public static DatasetInfo of(String datasetId) {
return newBuilder(datasetId).build();
}
static DatasetInfo fromPb(Dataset datasetPb) {
return new BuilderImpl(datasetPb).build();
}
}