Skip to content

Commit 722e4a7

Browse files
committed
Add JobInfo hierarcy and related classes
1 parent 9df4005 commit 722e4a7

18 files changed

+3748
-1
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 Job Error. Objects of this class represent errors occurred during the
12+
* execution of a BigQuery Job.
13+
*/
14+
public class BigQueryError implements Serializable {
15+
16+
static final Function<ErrorProto, BigQueryError> FROM_PB_FUNCTION =
17+
new Function<ErrorProto, BigQueryError>() {
18+
@Override
19+
public BigQueryError apply(ErrorProto pb) {
20+
return BigQueryError.fromPb(pb);
21+
}
22+
};
23+
static final Function<BigQueryError, ErrorProto> TO_PB_FUNCTION =
24+
new Function<BigQueryError, ErrorProto>() {
25+
@Override
26+
public ErrorProto apply(BigQueryError error) {
27+
return error.toPb();
28+
}
29+
};
30+
private static final long serialVersionUID = -6566785320629096688L;
31+
32+
private final String reason;
33+
private final String location;
34+
private final String debugInfo;
35+
private final String message;
36+
37+
BigQueryError(String reason, String location, String message, String debugInfo) {
38+
this.reason = reason;
39+
this.location = location;
40+
this.debugInfo = debugInfo;
41+
this.message = message;
42+
}
43+
44+
BigQueryError(String reason, String location, String message) {
45+
this.reason = reason;
46+
this.location = location;
47+
this.message = message;
48+
this.debugInfo = null;
49+
}
50+
51+
/**
52+
* Returns short error code that summarizes the error.
53+
*
54+
* @see <a href="https://cloud.google.com/bigquery/troubleshooting-errors">Troubleshooting
55+
* Errors</a>
56+
*/
57+
public String reason() {
58+
return reason;
59+
}
60+
61+
/**
62+
* Returns where the error occurred, if present.
63+
*/
64+
public String location() {
65+
return location;
66+
}
67+
68+
String debugInfo() {
69+
return debugInfo;
70+
}
71+
72+
/**
73+
* Returns a human-readable description of the error.
74+
*/
75+
public String message() {
76+
return message;
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return Objects.hash(reason, location, message);
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return MoreObjects.toStringHelper(this)
87+
.add("reason", reason)
88+
.add("location", location)
89+
.add("message", message)
90+
.toString();
91+
}
92+
93+
@Override
94+
public boolean equals(Object obj) {
95+
return obj instanceof BigQueryError && Objects.equals(toPb(), ((BigQueryError) obj).toPb());
96+
}
97+
98+
ErrorProto toPb() {
99+
ErrorProto errorPb = new ErrorProto();
100+
if (reason != null) {
101+
errorPb.setReason(reason);
102+
}
103+
if (location != null) {
104+
errorPb.setLocation(location);
105+
}
106+
if (message != null) {
107+
errorPb.setMessage(message);
108+
}
109+
if (debugInfo != null) {
110+
errorPb.setDebugInfo(debugInfo);
111+
}
112+
return errorPb;
113+
}
114+
115+
static BigQueryError fromPb(ErrorProto errorPb) {
116+
return new BigQueryError(errorPb.getReason(), errorPb.getLocation(), errorPb.getMessage(),
117+
errorPb.getDebugInfo());
118+
}
119+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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.ToStringHelper;
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+
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 boolean equals(Object obj) {
181+
return obj instanceof CopyJobInfo && Objects.equals(toPb(), ((CopyJobInfo) obj).toPb());
182+
}
183+
184+
@Override
185+
public int hashCode() {
186+
return Objects.hash(super.hashCode(), sourceTables, destinationTable, createDisposition,
187+
writeDisposition);
188+
}
189+
190+
@Override
191+
Job toPb() {
192+
JobConfigurationTableCopy copyConfigurationPb = new JobConfigurationTableCopy();
193+
copyConfigurationPb.setDestinationTable(destinationTable.toPb());
194+
if (sourceTables.size() == 1) {
195+
copyConfigurationPb.setSourceTable(sourceTables.get(0).toPb());
196+
} else {
197+
copyConfigurationPb.setSourceTables(Lists.transform(sourceTables, TableId.TO_PB_FUNCTION));
198+
}
199+
if (createDisposition != null) {
200+
copyConfigurationPb.setCreateDisposition(createDisposition.toString());
201+
}
202+
if (writeDisposition != null) {
203+
copyConfigurationPb.setWriteDisposition(writeDisposition.toString());
204+
}
205+
return super.toPb().setConfiguration(new JobConfiguration().setCopy(copyConfigurationPb));
206+
}
207+
208+
/**
209+
* Creates a builder for a BigQuery Copy Job given destination and source table.
210+
*/
211+
public static Builder builder(TableId destinationTable, TableId sourceTable) {
212+
return builder(destinationTable, ImmutableList.of(checkNotNull(sourceTable)));
213+
}
214+
215+
/**
216+
* Creates a builder for a BigQuery Copy Job given destination and source tables.
217+
*/
218+
public static Builder builder(TableId destinationTable, List<TableId> sourceTables) {
219+
return new Builder().destinationTable(destinationTable).sourceTables(sourceTables);
220+
}
221+
222+
/**
223+
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is chosen by
224+
* the service.
225+
*/
226+
public static CopyJobInfo of(TableId destinationTable, TableId sourceTable) {
227+
return builder(destinationTable, sourceTable).build();
228+
}
229+
230+
/**
231+
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is chosen by
232+
* the service.
233+
*/
234+
public static CopyJobInfo of(TableId destinationTable, List<TableId> sourceTables) {
235+
return builder(destinationTable, sourceTables).build();
236+
}
237+
238+
/**
239+
* Returns a BigQuery Copy Job for the given destination and source table. Job's id is set to the
240+
* provided value.
241+
*/
242+
public static CopyJobInfo of(JobId jobId, TableId destinationTable, TableId sourceTable) {
243+
return builder(destinationTable, sourceTable).jobId(jobId).build();
244+
}
245+
246+
/**
247+
* Returns a BigQuery Copy Job for the given destination and source tables. Job's id is set to the
248+
* provided value.
249+
*/
250+
public static CopyJobInfo of(JobId jobId, TableId destinationTable, List<TableId> sourceTables) {
251+
return builder(destinationTable, sourceTables).jobId(jobId).build();
252+
}
253+
254+
@SuppressWarnings("unchecked")
255+
static CopyJobInfo fromPb(Job jobPb) {
256+
return new Builder(jobPb).build();
257+
}
258+
}

0 commit comments

Comments
 (0)