|
| 1 | +package com.google.gcloud.bigquery; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | + |
| 5 | +import com.google.api.services.bigquery.model.JobConfigurationTableCopy; |
| 6 | +import com.google.common.base.MoreObjects; |
| 7 | +import com.google.common.collect.ImmutableList; |
| 8 | +import com.google.common.collect.Lists; |
| 9 | + |
| 10 | +import java.io.Serializable; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | + |
| 14 | +/** |
| 15 | + * Google BigQuery Copy Job configuration. A Copy Job copies an existing table to another new or |
| 16 | + * existing table. |
| 17 | + */ |
| 18 | +public final class CopyJobConfiguration implements JobConfiguration, Serializable { |
| 19 | + |
| 20 | + private static final long serialVersionUID = 1140509641399762967L; |
| 21 | + |
| 22 | + private final List<TableId> sourceTables; |
| 23 | + private final TableId destinationTable; |
| 24 | + private final JobInfo.CreateDisposition createDisposition; |
| 25 | + private final JobInfo.WriteDisposition writeDisposition; |
| 26 | + |
| 27 | + public static final class Builder { |
| 28 | + |
| 29 | + private List<TableId> sourceTables; |
| 30 | + private TableId destinationTable; |
| 31 | + private JobInfo.CreateDisposition createDisposition; |
| 32 | + private JobInfo.WriteDisposition writeDisposition; |
| 33 | + |
| 34 | + private Builder() {} |
| 35 | + |
| 36 | + private Builder(CopyJobConfiguration jobConfiguration) { |
| 37 | + this.sourceTables = jobConfiguration.sourceTables; |
| 38 | + this.destinationTable = jobConfiguration.destinationTable; |
| 39 | + this.createDisposition = jobConfiguration.createDisposition; |
| 40 | + this.writeDisposition = jobConfiguration.writeDisposition; |
| 41 | + } |
| 42 | + |
| 43 | + private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) { |
| 44 | + JobConfigurationTableCopy copyConfigurationPb = configurationPb.getCopy(); |
| 45 | + this.destinationTable = TableId.fromPb(copyConfigurationPb.getDestinationTable()); |
| 46 | + if (copyConfigurationPb.getSourceTables() != null) { |
| 47 | + this.sourceTables = |
| 48 | + Lists.transform(copyConfigurationPb.getSourceTables(), TableId.FROM_PB_FUNCTION); |
| 49 | + } else { |
| 50 | + this.sourceTables = ImmutableList.of(TableId.fromPb(copyConfigurationPb.getSourceTable())); |
| 51 | + } |
| 52 | + if (copyConfigurationPb.getCreateDisposition() != null) { |
| 53 | + this.createDisposition = |
| 54 | + JobInfo.CreateDisposition.valueOf(copyConfigurationPb.getCreateDisposition()); |
| 55 | + } |
| 56 | + if (copyConfigurationPb.getWriteDisposition() != null) { |
| 57 | + this.writeDisposition = JobInfo.WriteDisposition.valueOf( |
| 58 | + copyConfigurationPb.getWriteDisposition()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the source tables to copy. |
| 64 | + */ |
| 65 | + public Builder sourceTables(List<TableId> sourceTables) { |
| 66 | + this.sourceTables = sourceTables != null ? ImmutableList.copyOf(sourceTables) : null; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Sets the destination table of the copy job. |
| 72 | + */ |
| 73 | + public Builder destinationTable(TableId destinationTable) { |
| 74 | + this.destinationTable = destinationTable; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets whether the job is allowed to create new tables. |
| 80 | + * |
| 81 | + * @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition"> |
| 82 | + * Create Disposition</a> |
| 83 | + */ |
| 84 | + public Builder createDisposition(JobInfo.CreateDisposition createDisposition) { |
| 85 | + this.createDisposition = createDisposition; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the action that should occur if the destination table already exists. |
| 91 | + * |
| 92 | + * @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition"> |
| 93 | + * Write Disposition</a> |
| 94 | + */ |
| 95 | + public Builder writeDisposition(JobInfo.WriteDisposition writeDisposition) { |
| 96 | + this.writeDisposition = writeDisposition; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public CopyJobConfiguration build() { |
| 101 | + return new CopyJobConfiguration(this); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private CopyJobConfiguration(Builder builder) { |
| 106 | + this.sourceTables = checkNotNull(builder.sourceTables); |
| 107 | + this.destinationTable = checkNotNull(builder.destinationTable); |
| 108 | + this.createDisposition = builder.createDisposition; |
| 109 | + this.writeDisposition = builder.writeDisposition; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Type type() { |
| 114 | + return Type.COPY; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns the source tables to copy. |
| 119 | + */ |
| 120 | + public List<TableId> sourceTables() { |
| 121 | + return sourceTables; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns the destination table to load the data into. |
| 126 | + */ |
| 127 | + public TableId destinationTable() { |
| 128 | + return destinationTable; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Returns whether the job is allowed to create new tables. |
| 133 | + * |
| 134 | + * @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.createDisposition"> |
| 135 | + * Create Disposition</a> |
| 136 | + */ |
| 137 | + public JobInfo.CreateDisposition createDisposition() { |
| 138 | + return this.createDisposition; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns the action that should occur if the destination table already exists. |
| 143 | + * |
| 144 | + * @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.copy.writeDisposition"> |
| 145 | + * Write Disposition</a> |
| 146 | + */ |
| 147 | + public JobInfo.WriteDisposition writeDisposition() { |
| 148 | + return writeDisposition; |
| 149 | + } |
| 150 | + |
| 151 | + public Builder toBuilder() { |
| 152 | + return new Builder(this); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public String toString() { |
| 157 | + return MoreObjects.toStringHelper(this) |
| 158 | + .add("sourceTables", sourceTables) |
| 159 | + .add("destinationTable", destinationTable) |
| 160 | + .add("createDisposition", createDisposition) |
| 161 | + .add("writeDisposition", writeDisposition) |
| 162 | + .toString(); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean equals(Object obj) { |
| 167 | + return obj instanceof CopyJobConfiguration |
| 168 | + && Objects.equals(toPb(), ((CopyJobConfiguration) obj).toPb()); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public int hashCode() { |
| 173 | + return Objects.hash(sourceTables, destinationTable, createDisposition, writeDisposition); |
| 174 | + } |
| 175 | + |
| 176 | + com.google.api.services.bigquery.model.JobConfiguration toPb() { |
| 177 | + JobConfigurationTableCopy configurationPb = new JobConfigurationTableCopy(); |
| 178 | + configurationPb.setDestinationTable(destinationTable.toPb()); |
| 179 | + if (sourceTables.size() == 1) { |
| 180 | + configurationPb.setSourceTable(sourceTables.get(0).toPb()); |
| 181 | + } else { |
| 182 | + configurationPb.setSourceTables(Lists.transform(sourceTables, TableId.TO_PB_FUNCTION)); |
| 183 | + } |
| 184 | + if (createDisposition != null) { |
| 185 | + configurationPb.setCreateDisposition(createDisposition.toString()); |
| 186 | + } |
| 187 | + if (writeDisposition != null) { |
| 188 | + configurationPb.setWriteDisposition(writeDisposition.toString()); |
| 189 | + } |
| 190 | + return new com.google.api.services.bigquery.model.JobConfiguration().setCopy(configurationPb); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Creates a builder for a BigQuery Copy Job configuration given destination and source table. |
| 195 | + */ |
| 196 | + public static Builder builder(TableId destinationTable, TableId sourceTable) { |
| 197 | + return builder(destinationTable, ImmutableList.of(checkNotNull(sourceTable))); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Creates a builder for a BigQuery Copy Job configuration given destination and source tables. |
| 202 | + */ |
| 203 | + public static Builder builder(TableId destinationTable, List<TableId> sourceTables) { |
| 204 | + return new Builder().destinationTable(destinationTable).sourceTables(sourceTables); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns a BigQuery Copy Job configuration for the given destination and source table. |
| 209 | + */ |
| 210 | + public static CopyJobConfiguration of(TableId destinationTable, TableId sourceTable) { |
| 211 | + return builder(destinationTable, sourceTable).build(); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Returns a BigQuery Copy Job configuration for the given destination and source tables. |
| 216 | + */ |
| 217 | + public static CopyJobConfiguration of(TableId destinationTable, List<TableId> sourceTables) { |
| 218 | + return builder(destinationTable, sourceTables).build(); |
| 219 | + } |
| 220 | + |
| 221 | + static CopyJobConfiguration fromPb( |
| 222 | + com.google.api.services.bigquery.model.JobConfiguration jobPb) { |
| 223 | + return new Builder(jobPb).build(); |
| 224 | + } |
| 225 | +} |
0 commit comments