forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyJobInfo.java
More file actions
258 lines (224 loc) · 8.59 KB
/
CopyJobInfo.java
File metadata and controls
258 lines (224 loc) · 8.59 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
/*
* 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.gcloud.bigquery;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.services.bigquery.model.Job;
import com.google.api.services.bigquery.model.JobConfiguration;
import com.google.api.services.bigquery.model.JobConfigurationTableCopy;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Objects;
/**
* Google BigQuery Copy Job. A Copy Job copies an existing table to another new or existing table.
*/
public class CopyJobInfo extends JobInfo<JobStatistics> {
private static final long serialVersionUID = 7830335512951916299L;
private final List<TableId> sourceTables;
private final TableId destinationTable;
private final CreateDisposition createDisposition;
private final WriteDisposition writeDisposition;
public static final class Builder extends JobInfo.Builder<CopyJobInfo, JobStatistics, Builder> {
private List<TableId> sourceTables;
private TableId destinationTable;
private CreateDisposition createDisposition;
private WriteDisposition writeDisposition;
private Builder() {}
private Builder(CopyJobInfo jobInfo) {
super(jobInfo);
this.sourceTables = jobInfo.sourceTables;
this.destinationTable = jobInfo.destinationTable;
this.createDisposition = jobInfo.createDisposition;
this.writeDisposition = jobInfo.writeDisposition;
}
private Builder(Job jobPb) {
super(jobPb);
JobConfigurationTableCopy copyConfigurationPb = jobPb.getConfiguration().getCopy();
this.destinationTable = TableId.fromPb(copyConfigurationPb.getDestinationTable());
if (copyConfigurationPb.getSourceTables() != null) {
this.sourceTables =
Lists.transform(copyConfigurationPb.getSourceTables(), TableId.FROM_PB_FUNCTION);
} else {
this.sourceTables = ImmutableList.of(TableId.fromPb(copyConfigurationPb.getSourceTable()));
}
if (copyConfigurationPb.getCreateDisposition() != null) {
this.createDisposition =
CreateDisposition.valueOf(copyConfigurationPb.getCreateDisposition());
}
if (copyConfigurationPb.getWriteDisposition() != null) {
this.writeDisposition = WriteDisposition.valueOf(copyConfigurationPb.getWriteDisposition());
}
}
/**
* Sets the source tables to copy.
*/
public Builder sourceTables(List<TableId> sourceTables) {
this.sourceTables = sourceTables != null ? ImmutableList.copyOf(sourceTables) : null;
return self();
}
/**
* Sets the destination table of the copy job.
*/
public Builder destinationTable(TableId destinationTable) {
this.destinationTable = destinationTable;
return self();
}
/**
* Sets whether the job is allowed to create new tables.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
* Create Disposition</a>
*/
public Builder createDisposition(CreateDisposition createDisposition) {
this.createDisposition = createDisposition;
return self();
}
/**
* Sets the action that should occur if the destination table already exists.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
* Write Disposition</a>
*/
public Builder writeDisposition(WriteDisposition writeDisposition) {
this.writeDisposition = writeDisposition;
return self();
}
@Override
public CopyJobInfo build() {
return new CopyJobInfo(this);
}
}
private CopyJobInfo(Builder builder) {
super(builder);
this.sourceTables = checkNotNull(builder.sourceTables);
this.destinationTable = checkNotNull(builder.destinationTable);
this.createDisposition = builder.createDisposition;
this.writeDisposition = builder.writeDisposition;
}
/**
* Returns the source tables to copy.
*/
public List<TableId> sourceTables() {
return sourceTables;
}
/**
* Returns the destination table to load the data into.
*/
public TableId destinationTable() {
return destinationTable;
}
/**
* Returns whether the job is allowed to create new tables.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition">
* Create Disposition</a>
*/
public CreateDisposition createDisposition() {
return this.createDisposition;
}
/**
* Returns the action that should occur if the destination table already exists.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition">
* Write Disposition</a>
*/
public WriteDisposition writeDisposition() {
return writeDisposition;
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
ToStringHelper toStringHelper() {
return super.toStringHelper()
.add("sourceTables", sourceTables)
.add("destinationTable", destinationTable)
.add("createDisposition", createDisposition)
.add("writeDisposition", writeDisposition);
}
@Override
public boolean equals(Object obj) {
return obj instanceof CopyJobInfo && baseEquals((CopyJobInfo) obj);
}
@Override
public int hashCode() {
return Objects.hash(baseHashCode(), sourceTables, destinationTable, createDisposition,
writeDisposition);
}
@Override
Job toPb() {
JobConfigurationTableCopy copyConfigurationPb = new JobConfigurationTableCopy();
copyConfigurationPb.setDestinationTable(destinationTable.toPb());
if (sourceTables.size() == 1) {
copyConfigurationPb.setSourceTable(sourceTables.get(0).toPb());
} else {
copyConfigurationPb.setSourceTables(Lists.transform(sourceTables, TableId.TO_PB_FUNCTION));
}
if (createDisposition != null) {
copyConfigurationPb.setCreateDisposition(createDisposition.toString());
}
if (writeDisposition != null) {
copyConfigurationPb.setWriteDisposition(writeDisposition.toString());
}
return super.toPb().setConfiguration(new JobConfiguration().setCopy(copyConfigurationPb));
}
/**
* Creates a builder for a BigQuery Copy Job given destination and source table.
*/
public static Builder builder(TableId destinationTable, TableId sourceTable) {
return builder(destinationTable, ImmutableList.of(checkNotNull(sourceTable)));
}
/**
* Creates a builder for a BigQuery Copy Job given destination and source tables.
*/
public static Builder builder(TableId destinationTable, List<TableId> sourceTables) {
return new Builder().destinationTable(destinationTable).sourceTables(sourceTables);
}
/**
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is chosen by
* the service.
*/
public static CopyJobInfo of(TableId destinationTable, TableId sourceTable) {
return builder(destinationTable, sourceTable).build();
}
/**
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is chosen by
* the service.
*/
public static CopyJobInfo of(TableId destinationTable, List<TableId> sourceTables) {
return builder(destinationTable, sourceTables).build();
}
/**
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is set to the
* provided value.
*/
public static CopyJobInfo of(JobId jobId, TableId destinationTable, TableId sourceTable) {
return builder(destinationTable, sourceTable).jobId(jobId).build();
}
/**
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is set to the
* provided value.
*/
public static CopyJobInfo of(JobId jobId, TableId destinationTable, List<TableId> sourceTables) {
return builder(destinationTable, sourceTables).jobId(jobId).build();
}
@SuppressWarnings("unchecked")
static CopyJobInfo fromPb(Job jobPb) {
return new Builder(jobPb).build();
}
}