Repository navigation
Expand file tree
/
Copy pathJobInfo.java
More file actions
499 lines (431 loc) · 13.7 KB
/
Copy pathJobInfo.java
File metadata and controls
499 lines (431 loc) · 13.7 KB
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
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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 com.google.api.services.bigquery.model.Job;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
/**
* Google BigQuery Job information. Jobs are objects that manage asynchronous tasks such as running
* queries, loading data, and exporting data. Use {@link CopyJobConfiguration} for a job that
* copies an existing table. Use {@link ExtractJobConfiguration} for a job that exports a table to
* Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads data from Google
* Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that runs a query.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs">Jobs</a>
*/
public class JobInfo implements Serializable {
static final Function<Job, JobInfo> FROM_PB_FUNCTION =
new Function<Job, JobInfo>() {
@Override
public JobInfo apply(Job pb) {
return JobInfo.fromPb(pb);
}
};
private static final long serialVersionUID = 2740548743267670124L;
private final String etag;
private final String generatedId;
private final JobId jobId;
private final String selfLink;
private final JobStatus status;
private final JobStatistics statistics;
private final String userEmail;
private final JobConfiguration configuration;
/**
* Specifies whether the job is allowed to create new tables.
*/
public enum CreateDisposition {
/**
* Configures the job to create the table if it does not exist.
*/
CREATE_IF_NEEDED,
/**
* Configures the job to fail with a not-found error if the table does not exist.
*/
CREATE_NEVER
}
/**
* Specifies the action that occurs if the destination table already exists.
*/
public enum WriteDisposition {
/**
* Configures the job to overwrite the table data if table already exists.
*/
WRITE_TRUNCATE,
/**
* Configures the job to append data to the table if it already exists.
*/
WRITE_APPEND,
/**
* Configures the job to fail with a duplicate error if the table already exists.
*/
WRITE_EMPTY
}
/**
* A builder for {@code JobInfo} objects.
*/
public abstract static class Builder {
abstract Builder setEtag(String etag);
abstract Builder setGeneratedId(String generatedId);
/**
* Sets the job identity.
*/
@Deprecated
public abstract Builder jobId(JobId jobId);
/**
* Sets the job identity.
*/
public abstract Builder setJobId(JobId jobId);
abstract Builder setSelfLink(String selfLink);
abstract Builder setStatus(JobStatus status);
abstract Builder setStatistics(JobStatistics statistics);
abstract Builder setUserEmail(String userEmail);
/**
* Sets a configuration for the {@code JobInfo} object. Use {@link CopyJobConfiguration} for a
* job that copies an existing table. Use {@link ExtractJobConfiguration} for a job that exports
* a table to Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads data
* from Google Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that runs
* a query.
*/
@Deprecated
public abstract Builder configuration(JobConfiguration configuration);
/**
* Sets a configuration for the {@code JobInfo} object. Use {@link CopyJobConfiguration} for a
* job that copies an existing table. Use {@link ExtractJobConfiguration} for a job that exports
* a table to Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads data
* from Google Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that runs
* a query.
*/
public abstract Builder setConfiguration(JobConfiguration configuration);
/**
* Creates a {@code JobInfo} object.
*/
public abstract JobInfo build();
}
static final class BuilderImpl extends Builder {
private String etag;
private String generatedId;
private JobId jobId;
private String selfLink;
private JobStatus status;
private JobStatistics statistics;
private String userEmail;
private JobConfiguration configuration;
BuilderImpl() {}
BuilderImpl(JobInfo jobInfo) {
this.etag = jobInfo.etag;
this.generatedId = jobInfo.generatedId;
this.jobId = jobInfo.jobId;
this.selfLink = jobInfo.selfLink;
this.status = jobInfo.status;
this.statistics = jobInfo.statistics;
this.userEmail = jobInfo.userEmail;
this.configuration = jobInfo.configuration;
}
BuilderImpl(Job jobPb) {
this.etag = jobPb.getEtag();
this.generatedId = jobPb.getId();
if (jobPb.getJobReference() != null) {
this.jobId = JobId.fromPb(jobPb.getJobReference());
}
this.selfLink = jobPb.getSelfLink();
if (jobPb.getStatus() != null) {
this.status = JobStatus.fromPb(jobPb.getStatus());
}
if (jobPb.getStatistics() != null) {
this.statistics = JobStatistics.fromPb(jobPb.getStatistics());
}
this.userEmail = jobPb.getUserEmail();
this.configuration = JobConfiguration.fromPb(jobPb.getConfiguration());
}
@Override
Builder setEtag(String etag) {
this.etag = etag;
return this;
}
@Override
Builder setGeneratedId(String generatedId) {
this.generatedId = generatedId;
return this;
}
@Override
@Deprecated
public Builder jobId(JobId jobId) {
return setJobId(jobId);
}
@Override
public Builder setJobId(JobId jobId) {
this.jobId = jobId;
return this;
}
@Override
Builder setSelfLink(String selfLink) {
this.selfLink = selfLink;
return this;
}
@Override
Builder setStatus(JobStatus status) {
this.status = status;
return this;
}
@Override
Builder setStatistics(JobStatistics statistics) {
this.statistics = statistics;
return this;
}
@Override
Builder setUserEmail(String userEmail) {
this.userEmail = userEmail;
return this;
}
@Override
@Deprecated
public Builder configuration(JobConfiguration configuration) {
return setConfiguration(configuration);
}
@Override
public Builder setConfiguration(JobConfiguration configuration) {
this.configuration = configuration;
return this;
}
@Override
public JobInfo build() {
return new JobInfo(this);
}
}
JobInfo(BuilderImpl builder) {
this.jobId = builder.jobId;
this.etag = builder.etag;
this.generatedId = builder.generatedId;
this.selfLink = builder.selfLink;
this.status = builder.status;
this.statistics = builder.statistics;
this.userEmail = builder.userEmail;
this.configuration = builder.configuration;
}
/**
* Returns the hash of the job resource.
*/
@Deprecated
public String etag() {
return getEtag();
}
/**
* Returns the hash of the job resource.
*/
public String getEtag() {
return etag;
}
/**
* Returns the service-generated id for the job.
*/
@Deprecated
public String generatedId() {
return getGeneratedId();
}
/**
* Returns the service-generated id for the job.
*/
public String getGeneratedId() {
return generatedId;
}
/**
* Returns the job identity.
*/
@Deprecated
public JobId jobId() {
return getJobId();
}
/**
* Returns the job identity.
*/
public JobId getJobId() {
return jobId;
}
/**
* Returns an URL that can be used to access the resource again. The returned URL can be used for
* GET requests.
*/
@Deprecated
public String selfLink() {
return getSelfLink();
}
/**
* Returns an URL that can be used to access the resource again. The returned URL can be used for
* GET requests.
*/
public String getSelfLink() {
return selfLink;
}
/**
* Returns the status of this job. Examine this value when polling an asynchronous job to see if
* the job is complete.
*/
@Deprecated
public JobStatus status() {
return getStatus();
}
/**
* Returns the status of this job. Examine this value when polling an asynchronous job to see if
* the job is complete.
*/
public JobStatus getStatus() {
return status;
}
/**
* Returns information about the job, including starting time and ending time of the job.
*/
@SuppressWarnings("unchecked")
@Deprecated
public <S extends JobStatistics> S statistics() {
return getStatistics();
}
/**
* Returns information about the job, including starting time and ending time of the job.
*/
@SuppressWarnings("unchecked")
public <S extends JobStatistics> S getStatistics() {
return (S) statistics;
}
/**
* Returns the email address of the user who ran the job.
*/
@Deprecated
public String userEmail() {
return getUserEmail();
}
/**
* Returns the email address of the user who ran the job.
*/
public String getUserEmail() {
return userEmail;
}
/**
* Returns the job's configuration.
*/
@SuppressWarnings("unchecked")
@Deprecated
public <C extends JobConfiguration> C configuration() {
return getConfiguration();
}
/**
* Returns the job's configuration.
*/
@SuppressWarnings("unchecked")
public <C extends JobConfiguration> C getConfiguration() {
return (C) configuration;
}
/**
* Returns a builder for the job object.
*/
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("job", jobId)
.add("status", status)
.add("statistics", statistics)
.add("userEmail", userEmail)
.add("etag", etag)
.add("generatedId", generatedId)
.add("selfLink", selfLink)
.add("configuration", configuration)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(jobId);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(JobInfo.class)
&& Objects.equals(toPb(), ((JobInfo) obj).toPb());
}
JobInfo setProjectId(String projectId) {
Builder builder = toBuilder();
if (jobId != null) {
builder.setJobId(jobId.setProjectId(projectId));
}
return builder.setConfiguration(configuration.setProjectId(projectId)).build();
}
Job toPb() {
Job jobPb = new Job();
jobPb.setEtag(etag);
jobPb.setId(generatedId);
jobPb.setSelfLink(selfLink);
jobPb.setUserEmail(userEmail);
if (jobId != null) {
jobPb.setJobReference(jobId.toPb());
}
if (status != null) {
jobPb.setStatus(status.toPb());
}
if (statistics != null) {
jobPb.setStatistics(statistics.toPb());
}
jobPb.setConfiguration(configuration.toPb());
return jobPb;
}
/**
* Returns a builder for a {@code JobInfo} object given the job configuration. Use
* {@link CopyJobConfiguration} for a job that copies an existing table. Use
* {@link ExtractJobConfiguration} for a job that exports a table to Google Cloud Storage. Use
* {@link LoadJobConfiguration} for a job that loads data from Google Cloud Storage into a table.
* Use {@link QueryJobConfiguration} for a job that runs a query.
*/
@Deprecated
public static Builder builder(JobConfiguration configuration) {
return newBuilder(configuration);
}
/**
* Returns a builder for a {@code JobInfo} object given the job configuration. Use
* {@link CopyJobConfiguration} for a job that copies an existing table. Use
* {@link ExtractJobConfiguration} for a job that exports a table to Google Cloud Storage. Use
* {@link LoadJobConfiguration} for a job that loads data from Google Cloud Storage into a table.
* Use {@link QueryJobConfiguration} for a job that runs a query.
*/
public static Builder newBuilder(JobConfiguration configuration) {
return new BuilderImpl().setConfiguration(configuration);
}
/**
* Returns a {@code JobInfo} object given the job configuration. Use {@link CopyJobConfiguration}
* for a job that copies an existing table. Use {@link ExtractJobConfiguration} for a job that
* exports a table to Google Cloud Storage. Use {@link LoadJobConfiguration} for a job that loads
* data from Google Cloud Storage into a table. Use {@link QueryJobConfiguration} for a job that
* runs a query.
*/
public static JobInfo of(JobConfiguration configuration) {
return newBuilder(configuration).build();
}
/**
* Returns a builder for a {@code JobInfo} object given the job identity and configuration. Use
* {@link CopyJobConfiguration} for a job that copies an existing table. Use
* {@link ExtractJobConfiguration} for a job that exports a table to Google Cloud Storage. Use
* {@link LoadJobConfiguration} for a job that loads data from Google Cloud Storage into a table.
* Use {@link QueryJobConfiguration} for a job that runs a query.
*/
public static JobInfo of(JobId jobId, JobConfiguration configuration) {
return newBuilder(configuration).setJobId(jobId).build();
}
static JobInfo fromPb(Job jobPb) {
return new BuilderImpl(jobPb).build();
}
}