/
ModelInfo.java
456 lines (389 loc) · 14.3 KB
/
ModelInfo.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
/*
* Copyright 2019 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.core.BetaApi;
import com.google.api.services.bigquery.model.Model;
import com.google.api.services.bigquery.model.TrainingRun;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Google BigQuery ML model information. Models are not created directly via the API, but by issuing
* a CREATE MODEL query.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-create">CREATE
* MODEL statement</a>
*/
public class ModelInfo implements Serializable {
static final Function<Model, ModelInfo> FROM_PB_FUNCTION =
new Function<Model, ModelInfo>() {
@Override
public ModelInfo apply(Model pb) {
return ModelInfo.fromPb(pb);
}
};
static final Function<ModelInfo, Model> TO_PB_FUNCTION =
new Function<ModelInfo, Model>() {
@Override
public Model apply(ModelInfo ModelInfo) {
return ModelInfo.toPb();
}
};
private static final long serialVersionUID = 609769793337719407L;
private final String etag;
private final ModelId modelId;
private final String description;
private final String modelType;
private final String friendlyName;
private final Long creationTime;
private final Long lastModifiedTime;
private final Long expirationTime;
private final Labels labels;
private final String location;
private final ImmutableList<TrainingRun> trainingRunList;
private final ImmutableList<StandardSQLField> featureColumnList;
private final ImmutableList<StandardSQLField> labelColumnList;
private final EncryptionConfiguration encryptionConfiguration;
/** A builder for {@code ModelInfo} objects. */
public abstract static class Builder {
/** Sets the user description for this model. */
public abstract Builder setDescription(String description);
/** Sets the user-friendly name for this model. */
public abstract Builder setFriendlyName(String friendlyName);
/**
* Set the time when this model expires, in milliseconds since the epoch. If not present, the
* model persists indefinitely. Expired models will be deleted.
*/
public abstract Builder setExpirationTime(Long expirationTime);
/**
* Set the labels applied to this model.
*
* <p>When used with {@link BigQuery#update(ModelInfo, ModelOption...)}, 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.
*/
public abstract Builder setLabels(Map<String, String> labels);
abstract Builder setLocation(String location);
public abstract Builder setModelId(ModelId modelId);
abstract Builder setEtag(String etag);
abstract Builder setModelType(String modelType);
abstract Builder setCreationTime(Long creation);
abstract Builder setLastModifiedTime(Long lastModifiedTime);
abstract Builder setTrainingRuns(List<TrainingRun> trainingRunList);
abstract Builder setLabelColumns(List<StandardSQLField> labelColumnList);
abstract Builder setFeatureColumns(List<StandardSQLField> featureColumnList);
public abstract Builder setEncryptionConfiguration(EncryptionConfiguration configuration);
/** Creates a {@code ModelInfo} object. */
public abstract ModelInfo build();
}
static class BuilderImpl extends Builder {
private String etag;
private ModelId modelId;
private String description;
private String modelType;
private String friendlyName;
private Long creationTime;
private Long lastModifiedTime;
private Long expirationTime;
private Labels labels = Labels.ZERO;
private String location;
private List<TrainingRun> trainingRunList = Collections.emptyList();
private List<StandardSQLField> labelColumnList = Collections.emptyList();
private List<StandardSQLField> featureColumnList = Collections.emptyList();
private EncryptionConfiguration encryptionConfiguration;
BuilderImpl() {}
BuilderImpl(ModelInfo modelInfo) {
this.etag = modelInfo.etag;
this.modelId = modelInfo.modelId;
this.description = modelInfo.description;
this.friendlyName = modelInfo.friendlyName;
this.modelType = modelInfo.modelType;
this.creationTime = modelInfo.creationTime;
this.lastModifiedTime = modelInfo.lastModifiedTime;
this.expirationTime = modelInfo.expirationTime;
this.trainingRunList = modelInfo.trainingRunList;
this.labelColumnList = modelInfo.labelColumnList;
this.featureColumnList = modelInfo.featureColumnList;
this.encryptionConfiguration = modelInfo.encryptionConfiguration;
this.location = modelInfo.location;
}
BuilderImpl(Model modelPb) {
this.modelId = ModelId.fromPb(modelPb.getModelReference());
if (modelPb.getLastModifiedTime() != null) {
this.setLastModifiedTime(modelPb.getLastModifiedTime().longValue());
}
this.etag = modelPb.getEtag();
this.description = modelPb.getDescription();
this.friendlyName = modelPb.getFriendlyName();
this.modelType = modelPb.getModelType();
this.creationTime = modelPb.getCreationTime();
this.lastModifiedTime = modelPb.getLastModifiedTime();
this.expirationTime = modelPb.getExpirationTime();
this.labels = Labels.fromPb(modelPb.getLabels());
this.location = modelPb.getLocation();
if (modelPb.getTrainingRuns() != null) {
this.trainingRunList = modelPb.getTrainingRuns();
}
if (modelPb.getLabelColumns() != null) {
this.labelColumnList =
Lists.transform(modelPb.getLabelColumns(), StandardSQLField.FROM_PB_FUNCTION);
}
if (modelPb.getFeatureColumns() != null) {
this.featureColumnList =
Lists.transform(modelPb.getFeatureColumns(), StandardSQLField.FROM_PB_FUNCTION);
}
if (modelPb.getEncryptionConfiguration() != null) {
this.encryptionConfiguration =
new EncryptionConfiguration.Builder(modelPb.getEncryptionConfiguration()).build();
}
}
@Override
Builder setEtag(String etag) {
this.etag = etag;
return this;
}
@Override
public Builder setDescription(String description) {
this.description = description;
return this;
}
@Override
public Builder setFriendlyName(String friendlyName) {
this.friendlyName = friendlyName;
return this;
}
@Override
Builder setModelType(String modelType) {
this.modelType = modelType;
return this;
}
@Override
Builder setCreationTime(Long creationTime) {
this.creationTime = creationTime;
return this;
}
@Override
Builder setLastModifiedTime(Long lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
return this;
}
@Override
public Builder setExpirationTime(Long expirationTime) {
this.expirationTime = expirationTime;
return this;
}
@Override
public Builder setModelId(ModelId modelId) {
this.modelId = modelId;
return this;
}
@Override
public Builder setLabels(Map<String, String> labels) {
this.labels = Labels.fromUser(labels);
return this;
}
@Override
Builder setLocation(String location) {
this.location = location;
return this;
}
@Override
Builder setTrainingRuns(List<TrainingRun> trainingRunList) {
this.trainingRunList = checkNotNull(trainingRunList);
return this;
}
@Override
Builder setLabelColumns(List<StandardSQLField> labelColumnList) {
this.labelColumnList = checkNotNull(labelColumnList);
return this;
}
@Override
Builder setFeatureColumns(List<StandardSQLField> featureColumnList) {
this.featureColumnList = checkNotNull(featureColumnList);
return this;
}
@Override
public Builder setEncryptionConfiguration(EncryptionConfiguration configuration) {
this.encryptionConfiguration = configuration;
return this;
}
@Override
public ModelInfo build() {
return new ModelInfo(this);
}
}
ModelInfo(BuilderImpl builder) {
this.modelId = checkNotNull(builder.modelId);
this.etag = builder.etag;
this.modelType = builder.modelType;
this.description = builder.description;
this.friendlyName = builder.friendlyName;
this.creationTime = builder.creationTime;
this.lastModifiedTime = builder.lastModifiedTime;
this.expirationTime = builder.expirationTime;
this.labels = builder.labels;
this.location = builder.location;
this.trainingRunList = ImmutableList.copyOf(builder.trainingRunList);
this.labelColumnList = ImmutableList.copyOf(builder.labelColumnList);
this.featureColumnList = ImmutableList.copyOf(builder.featureColumnList);
this.encryptionConfiguration = builder.encryptionConfiguration;
}
/** Returns the hash of the model resource. */
public String getEtag() {
return etag;
}
/** Returns the model identity. */
public ModelId getModelId() {
return modelId;
}
/** Returns the type of the ML model. */
public String getModelType() {
return modelType;
}
/** Returns the user description of the model. */
public String getDescription() {
return description;
}
/** Returns the user-friendly name for the model. */
public String getFriendlyName() {
return friendlyName;
}
/** Returns the time when this model was created, in milliseconds since the epoch. */
public Long getCreationTime() {
return creationTime;
}
/** Returns the time when this table was last modified, in milliseconds since the epoch. */
public Long getLastModifiedTime() {
return lastModifiedTime;
}
/**
* Returns this this model expires, in milliseconds since the epoch. If not present, the model
* will persist indefinitely. Expired models will be deleted.
*/
public Long getExpirationTime() {
return expirationTime;
}
/** Returns a map for labels applied to the model. */
public Map<String, String> getLabels() {
return labels.userMap();
}
/** Returns a location of the model. */
public String getLocation() {
return location;
}
/** Returns metadata about each training run iteration. */
@BetaApi
public ImmutableList<TrainingRun> getTrainingRuns() {
return trainingRunList;
}
/** Returns information about the label columns for this model. */
@BetaApi
public ImmutableList<StandardSQLField> getLabelColumns() {
return labelColumnList;
}
/** Returns information about the feature columns for this model. */
@BetaApi
public ImmutableList<StandardSQLField> getFeatureColumns() {
return featureColumnList;
}
public EncryptionConfiguration getEncryptionConfiguration() {
return encryptionConfiguration;
}
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("modelId", modelId)
.add("etag", etag)
.add("modelType", modelType)
.add("description", description)
.add("friendlyName", friendlyName)
.add("creationTime", creationTime)
.add("lastModifiedTime", lastModifiedTime)
.add("expirationTime", expirationTime)
.add("labels", labels)
.add("location", location)
.add("trainingRuns", trainingRunList)
.add("labelColumns", labelColumnList)
.add("featureColumns", featureColumnList)
.add("encryptionConfiguration", encryptionConfiguration)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(modelId);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(ModelInfo.class)
&& Objects.equals(toPb(), ((ModelInfo) obj).toPb());
}
/** Returns a builder for a {@code ModelInfo} object given table identity. */
public static Builder newBuilder(ModelId modelId) {
return new BuilderImpl().setModelId(modelId);
}
/** Returns a {@code ModelInfo} object given table identity. */
public static ModelInfo of(ModelId modelId) {
return newBuilder(modelId).build();
}
ModelInfo setProjectId(String projectId) {
if (Strings.isNullOrEmpty(getModelId().getProject())) {
return toBuilder().setModelId(getModelId().setProjectId(projectId)).build();
}
return this;
}
Model toPb() {
Model modelPb = new Model();
modelPb.setModelReference(modelId.toPb());
modelPb.setEtag(etag);
modelPb.setModelType(modelType);
modelPb.setDescription(description);
modelPb.setFriendlyName(friendlyName);
modelPb.setCreationTime(creationTime);
modelPb.setLastModifiedTime(lastModifiedTime);
modelPb.setExpirationTime(expirationTime);
modelPb.setLabels(labels.toPb());
modelPb.setLocation(location);
modelPb.setTrainingRuns(trainingRunList);
if (labelColumnList != null) {
modelPb.setLabelColumns(Lists.transform(labelColumnList, StandardSQLField.TO_PB_FUNCTION));
}
if (featureColumnList != null) {
modelPb.setFeatureColumns(
Lists.transform(featureColumnList, StandardSQLField.TO_PB_FUNCTION));
}
if (encryptionConfiguration != null) {
modelPb.setEncryptionConfiguration(encryptionConfiguration.toPb());
}
return modelPb;
}
static ModelInfo fromPb(Model modelPb) {
return new BuilderImpl(modelPb).build();
}
}