Skip to content

Commit 610dec5

Browse files
committed
Add JobInfo hierarcy and related classes
1 parent 9df4005 commit 610dec5

17 files changed

Lines changed: 3481 additions & 0 deletions
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.google.gcloud.bigquery;
2+
3+
import com.google.api.services.bigquery.model.ErrorProto;
4+
import com.google.common.base.Function;
5+
import com.google.common.base.MoreObjects;
6+
7+
import java.io.Serializable;
8+
import java.util.Objects;
9+
10+
/**
11+
* Google Cloud BigQuery error.
12+
*/
13+
public class BigQueryError implements Serializable {
14+
15+
static final Function<ErrorProto, BigQueryError> FROM_PB_FUNCTION =
16+
new Function<ErrorProto, BigQueryError>() {
17+
@Override
18+
public BigQueryError apply(ErrorProto pb) {
19+
return BigQueryError.fromPb(pb);
20+
}
21+
};
22+
static final Function<BigQueryError, ErrorProto> TO_PB_FUNCTION =
23+
new Function<BigQueryError, ErrorProto>() {
24+
@Override
25+
public ErrorProto apply(BigQueryError error) {
26+
return error.toPb();
27+
}
28+
};
29+
private static final long serialVersionUID = -6566785320629096688L;
30+
31+
private final String reason;
32+
private final String location;
33+
private final String debugInfo;
34+
private final String message;
35+
36+
BigQueryError(String reason, String location, String message, String debugInfo) {
37+
this.reason = reason;
38+
this.location = location;
39+
this.debugInfo = debugInfo;
40+
this.message = message;
41+
}
42+
43+
BigQueryError(String reason, String location, String message) {
44+
this.reason = reason;
45+
this.location = location;
46+
this.message = message;
47+
this.debugInfo = null;
48+
}
49+
50+
/**
51+
* Returns short error code that summarizes the error.
52+
*
53+
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">
54+
* Troubleshooting Errors</a>
55+
*/
56+
public String reason() {
57+
return reason;
58+
}
59+
60+
/**
61+
* Returns where the error occurred, if present.
62+
*/
63+
public String location() {
64+
return location;
65+
}
66+
67+
String debugInfo() {
68+
return debugInfo;
69+
}
70+
71+
/**
72+
* Returns a human-readable description of the error.
73+
*/
74+
public String message() {
75+
return message;
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(reason, location, message);
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return MoreObjects.toStringHelper(this)
86+
.add("reason", reason)
87+
.add("location", location)
88+
.add("message", message)
89+
.toString();
90+
}
91+
92+
@Override
93+
public boolean equals(Object obj) {
94+
return obj instanceof BigQueryError && Objects.equals(toPb(), ((BigQueryError) obj).toPb());
95+
}
96+
97+
ErrorProto toPb() {
98+
ErrorProto errorPb = new ErrorProto();
99+
if (reason != null) {
100+
errorPb.setReason(reason);
101+
}
102+
if (location != null) {
103+
errorPb.setLocation(location);
104+
}
105+
if (message != null) {
106+
errorPb.setMessage(message);
107+
}
108+
if (debugInfo != null) {
109+
errorPb.setDebugInfo(debugInfo);
110+
}
111+
return errorPb;
112+
}
113+
114+
static BigQueryError fromPb(ErrorProto errorPb) {
115+
return new BigQueryError(errorPb.getReason(), errorPb.getLocation(), errorPb.getMessage(),
116+
errorPb.getDebugInfo());
117+
}
118+
}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.bigquery;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.api.services.bigquery.model.Job;
22+
import com.google.api.services.bigquery.model.JobConfiguration;
23+
import com.google.api.services.bigquery.model.JobConfigurationTableCopy;
24+
import com.google.common.base.MoreObjects;
25+
import com.google.common.collect.ImmutableList;
26+
import com.google.common.collect.Lists;
27+
28+
import java.util.List;
29+
import java.util.Objects;
30+
31+
/**
32+
* Google BigQuery Copy Job. A Copy Job copies an existing table to another new or existing table.
33+
*/
34+
public class CopyJobInfo extends JobInfo<JobStatistics> {
35+
36+
private static final long serialVersionUID = 7830335512951916299L;
37+
38+
private final List<TableId> sourceTables;
39+
private final TableId destinationTable;
40+
private final CreateDisposition createDisposition;
41+
private final WriteDisposition writeDisposition;
42+
43+
public static final class Builder extends JobInfo.Builder<CopyJobInfo, JobStatistics, Builder> {
44+
45+
private List<TableId> sourceTables;
46+
private TableId destinationTable;
47+
private CreateDisposition createDisposition;
48+
private WriteDisposition writeDisposition;
49+
50+
private Builder() {}
51+
52+
private Builder(CopyJobInfo jobInfo) {
53+
super(jobInfo);
54+
this.sourceTables = jobInfo.sourceTables;
55+
this.destinationTable = jobInfo.destinationTable;
56+
this.createDisposition = jobInfo.createDisposition;
57+
this.writeDisposition = jobInfo.writeDisposition;
58+
}
59+
60+
private Builder(Job jobPb) {
61+
super(jobPb);
62+
JobConfigurationTableCopy copyConfigurationPb = jobPb.getConfiguration().getCopy();
63+
this.destinationTable = TableId.fromPb(copyConfigurationPb.getDestinationTable());
64+
if (copyConfigurationPb.getSourceTables() != null) {
65+
this.sourceTables =
66+
Lists.transform(copyConfigurationPb.getSourceTables(), TableId.FROM_PB_FUNCTION);
67+
} else {
68+
this.sourceTables = ImmutableList.of(TableId.fromPb(copyConfigurationPb.getSourceTable()));
69+
}
70+
if (copyConfigurationPb.getCreateDisposition() != null) {
71+
this.createDisposition =
72+
CreateDisposition.valueOf(copyConfigurationPb.getCreateDisposition());
73+
}
74+
if (copyConfigurationPb.getWriteDisposition() != null) {
75+
this.writeDisposition = WriteDisposition.valueOf(copyConfigurationPb.getWriteDisposition());
76+
}
77+
}
78+
79+
/**
80+
* Sets the source tables to copy.
81+
*/
82+
public Builder sourceTables(List<TableId> sourceTables) {
83+
this.sourceTables = sourceTables != null ? ImmutableList.copyOf(sourceTables) : null;
84+
return self();
85+
}
86+
87+
/**
88+
* Sets the destination table of the copy job.
89+
*/
90+
public Builder destinationTable(TableId destinationTable) {
91+
this.destinationTable = destinationTable;
92+
return self();
93+
}
94+
95+
/**
96+
* Sets whether the job is allowed to create new tables.
97+
*
98+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
99+
* Jobs: Link Configuration</a>
100+
*/
101+
public Builder createDisposition(CreateDisposition createDisposition) {
102+
this.createDisposition = createDisposition;
103+
return self();
104+
}
105+
106+
/**
107+
* Sets the action that should occur if the destination table already exists.
108+
*
109+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.link">
110+
* Jobs: Link Configuration</a>
111+
*/
112+
public Builder writeDisposition(WriteDisposition writeDisposition) {
113+
this.writeDisposition = writeDisposition;
114+
return self();
115+
}
116+
117+
@Override
118+
public CopyJobInfo build() {
119+
return new CopyJobInfo(this);
120+
}
121+
}
122+
123+
private CopyJobInfo(Builder builder) {
124+
super(builder);
125+
this.sourceTables = checkNotNull(builder.sourceTables);
126+
this.destinationTable = checkNotNull(builder.destinationTable);
127+
this.createDisposition = builder.createDisposition;
128+
this.writeDisposition = builder.writeDisposition;
129+
}
130+
131+
/**
132+
* Returns the source tables to copy.
133+
*/
134+
public List<TableId> sourceTables() {
135+
return sourceTables;
136+
}
137+
138+
/**
139+
* Returns the destination table to load the data into.
140+
*/
141+
public TableId destinationTable() {
142+
return destinationTable;
143+
}
144+
145+
/**
146+
* Returns whether the job is allowed to create new tables.
147+
*
148+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
149+
* Jobs: Copy Configuration</a>
150+
*/
151+
public CreateDisposition createDisposition() {
152+
return this.createDisposition;
153+
}
154+
155+
/**
156+
* Returns the action that should occur if the destination table already exists.
157+
*
158+
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy">
159+
* Jobs: Copy Configuration</a>
160+
*/
161+
public WriteDisposition writeDisposition() {
162+
return writeDisposition;
163+
}
164+
165+
@Override
166+
public Builder toBuilder() {
167+
return new Builder(this);
168+
}
169+
170+
@Override
171+
public MoreObjects.ToStringHelper toStringHelper() {
172+
return super.toStringHelper()
173+
.add("sourceTables", sourceTables)
174+
.add("destinationTable", destinationTable)
175+
.add("createDisposition", createDisposition)
176+
.add("writeDisposition", writeDisposition);
177+
}
178+
179+
@Override
180+
public int hashCode() {
181+
return Objects.hash(super.hashCode(), sourceTables, destinationTable, createDisposition,
182+
writeDisposition);
183+
}
184+
185+
@Override
186+
Job toPb() {
187+
Job jobPb = super.toPb();
188+
JobConfiguration configurationPb = jobPb.getConfiguration();
189+
JobConfigurationTableCopy copyConfigurationPb = new JobConfigurationTableCopy();
190+
copyConfigurationPb.setDestinationTable(destinationTable.toPb());
191+
if (sourceTables.size() == 1) {
192+
copyConfigurationPb.setSourceTable(sourceTables.get(0).toPb());
193+
} else {
194+
copyConfigurationPb.setSourceTables(Lists.transform(sourceTables, TableId.TO_PB_FUNCTION));
195+
}
196+
if (createDisposition != null) {
197+
copyConfigurationPb.setCreateDisposition(createDisposition.toString());
198+
}
199+
if (writeDisposition != null) {
200+
copyConfigurationPb.setWriteDisposition(writeDisposition.toString());
201+
}
202+
return jobPb.setConfiguration(configurationPb.setCopy(copyConfigurationPb));
203+
}
204+
205+
/**
206+
* Creates a builder for a BigQuery Copy Job given destination and source table.
207+
*/
208+
public static Builder builder(TableId destinationTable, TableId sourceTable) {
209+
return builder(destinationTable, ImmutableList.of(checkNotNull(sourceTable)));
210+
}
211+
212+
/**
213+
* Creates a builder for a BigQuery Copy Job given destination and source tables.
214+
*/
215+
public static Builder builder(TableId destinationTable, List<TableId> sourceTables) {
216+
return new Builder().destinationTable(destinationTable).sourceTables(sourceTables);
217+
}
218+
219+
/**
220+
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is chosen by
221+
* the service.
222+
*/
223+
public static CopyJobInfo of(TableId destinationTable, TableId sourceTable) {
224+
return builder(destinationTable, sourceTable).build();
225+
}
226+
227+
/**
228+
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is chosen by
229+
* the service.
230+
*/
231+
public static CopyJobInfo of(TableId destinationTable, List<TableId> sourceTables) {
232+
return builder(destinationTable, sourceTables).build();
233+
}
234+
235+
/**
236+
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is set to the
237+
* provided value.
238+
*/
239+
public static CopyJobInfo of(JobId jobId, TableId destinationTable, TableId sourceTable) {
240+
return builder(destinationTable, sourceTable).jobId(jobId).build();
241+
}
242+
243+
/**
244+
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is set to the
245+
* provided value.
246+
*/
247+
public static CopyJobInfo of(JobId jobId, TableId destinationTable, List<TableId> sourceTables) {
248+
return builder(destinationTable, sourceTables).jobId(jobId).build();
249+
}
250+
251+
@SuppressWarnings("unchecked")
252+
static CopyJobInfo fromPb(Job jobPb) {
253+
return new Builder(jobPb).build();
254+
}
255+
}

0 commit comments

Comments
 (0)