-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove JobInfo hierarchy, add JobConfiguration hierarchy #584
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84402ac
Remove JobInfo hierarchy, add JobConfiguration hierarchy
mziccard a2c8b39
Fix comments. Make JobConfiguration an abstract class
mziccard 70ee77c
Move setProjectId from BigQueryImpl to entity objects
mziccard 05d6c57
Update README and package-info to use JobConfiguration
mziccard 68eed9e
Minor style fixes
mziccard 73250ea
Fix access-related issues
mziccard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CopyJobConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package com.google.gcloud.bigquery; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.api.services.bigquery.model.JobConfigurationTableCopy; | ||
import com.google.common.base.MoreObjects; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Google BigQuery Copy Job configuration. A Copy Job copies an existing table to another new or | ||
* existing table. | ||
*/ | ||
public final class CopyJobConfiguration implements JobConfiguration, Serializable { | ||
|
||
private static final long serialVersionUID = 1140509641399762967L; | ||
|
||
private final List<TableId> sourceTables; | ||
private final TableId destinationTable; | ||
private final JobInfo.CreateDisposition createDisposition; | ||
private final JobInfo.WriteDisposition writeDisposition; | ||
|
||
public static final class Builder { | ||
|
||
private List<TableId> sourceTables; | ||
private TableId destinationTable; | ||
private JobInfo.CreateDisposition createDisposition; | ||
private JobInfo.WriteDisposition writeDisposition; | ||
|
||
private Builder() {} | ||
|
||
private Builder(CopyJobConfiguration jobConfiguration) { | ||
this.sourceTables = jobConfiguration.sourceTables; | ||
this.destinationTable = jobConfiguration.destinationTable; | ||
this.createDisposition = jobConfiguration.createDisposition; | ||
this.writeDisposition = jobConfiguration.writeDisposition; | ||
} | ||
|
||
private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) { | ||
JobConfigurationTableCopy copyConfigurationPb = configurationPb.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 = | ||
JobInfo.CreateDisposition.valueOf(copyConfigurationPb.getCreateDisposition()); | ||
} | ||
if (copyConfigurationPb.getWriteDisposition() != null) { | ||
this.writeDisposition = JobInfo.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 this; | ||
} | ||
|
||
/** | ||
* Sets the destination table of the copy job. | ||
*/ | ||
public Builder destinationTable(TableId destinationTable) { | ||
this.destinationTable = destinationTable; | ||
return this; | ||
} | ||
|
||
/** | ||
* 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(JobInfo.CreateDisposition createDisposition) { | ||
this.createDisposition = createDisposition; | ||
return this; | ||
} | ||
|
||
/** | ||
* 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(JobInfo.WriteDisposition writeDisposition) { | ||
this.writeDisposition = writeDisposition; | ||
return this; | ||
} | ||
|
||
public CopyJobConfiguration build() { | ||
return new CopyJobConfiguration(this); | ||
} | ||
} | ||
|
||
private CopyJobConfiguration(Builder builder) { | ||
this.sourceTables = checkNotNull(builder.sourceTables); | ||
this.destinationTable = checkNotNull(builder.destinationTable); | ||
this.createDisposition = builder.createDisposition; | ||
this.writeDisposition = builder.writeDisposition; | ||
} | ||
|
||
@Override | ||
public Type type() { | ||
return Type.COPY; | ||
} | ||
|
||
/** | ||
* 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 JobInfo.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 JobInfo.WriteDisposition writeDisposition() { | ||
return writeDisposition; | ||
} | ||
|
||
public Builder toBuilder() { | ||
return new Builder(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("sourceTables", sourceTables) | ||
.add("destinationTable", destinationTable) | ||
.add("createDisposition", createDisposition) | ||
.add("writeDisposition", writeDisposition) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof CopyJobConfiguration | ||
&& Objects.equals(toPb(), ((CopyJobConfiguration) obj).toPb()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(sourceTables, destinationTable, createDisposition, writeDisposition); | ||
} | ||
|
||
com.google.api.services.bigquery.model.JobConfiguration toPb() { | ||
JobConfigurationTableCopy configurationPb = new JobConfigurationTableCopy(); | ||
configurationPb.setDestinationTable(destinationTable.toPb()); | ||
if (sourceTables.size() == 1) { | ||
configurationPb.setSourceTable(sourceTables.get(0).toPb()); | ||
} else { | ||
configurationPb.setSourceTables(Lists.transform(sourceTables, TableId.TO_PB_FUNCTION)); | ||
} | ||
if (createDisposition != null) { | ||
configurationPb.setCreateDisposition(createDisposition.toString()); | ||
} | ||
if (writeDisposition != null) { | ||
configurationPb.setWriteDisposition(writeDisposition.toString()); | ||
} | ||
return new com.google.api.services.bigquery.model.JobConfiguration().setCopy(configurationPb); | ||
} | ||
|
||
/** | ||
* Creates a builder for a BigQuery Copy Job configuration 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 configuration 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 configuration for the given destination and source table. | ||
*/ | ||
public static CopyJobConfiguration of(TableId destinationTable, TableId sourceTable) { | ||
return builder(destinationTable, sourceTable).build(); | ||
} | ||
|
||
/** | ||
* Returns a BigQuery Copy Job configuration for the given destination and source tables. | ||
*/ | ||
public static CopyJobConfiguration of(TableId destinationTable, List<TableId> sourceTables) { | ||
return builder(destinationTable, sourceTables).build(); | ||
} | ||
|
||
static CopyJobConfiguration fromPb( | ||
com.google.api.services.bigquery.model.JobConfiguration jobPb) { | ||
return new Builder(jobPb).build(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.