|
17 | 17 | */ |
18 | 18 | package org.apache.beam.sdk.io.gcp.bigquery; |
19 | 19 |
|
| 20 | +import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference; |
20 | 21 | import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull; |
21 | 22 |
|
22 | 23 | import com.google.api.services.bigquery.model.JobStatistics; |
|
25 | 26 | import com.google.cloud.bigquery.storage.v1.DataFormat; |
26 | 27 | import java.io.IOException; |
27 | 28 | import java.io.ObjectInputStream; |
| 29 | +import java.util.Optional; |
28 | 30 | import java.util.concurrent.atomic.AtomicReference; |
29 | 31 | import org.apache.beam.sdk.coders.Coder; |
30 | 32 | import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead.QueryPriority; |
@@ -188,4 +190,24 @@ public long getEstimatedSizeBytes(PipelineOptions options) throws Exception { |
188 | 190 | protected @Nullable String getTargetTableId(BigQueryOptions options) throws Exception { |
189 | 191 | return null; |
190 | 192 | } |
| 193 | + |
| 194 | + void removeDestinationIfExists(BigQueryOptions options) throws Exception { |
| 195 | + DatasetService datasetService = bqServices.getDatasetService(options.as(BigQueryOptions.class)); |
| 196 | + String project = queryTempProject; |
| 197 | + if (project == null) { |
| 198 | + project = |
| 199 | + options.as(BigQueryOptions.class).getBigQueryProject() == null |
| 200 | + ? options.as(BigQueryOptions.class).getProject() |
| 201 | + : options.as(BigQueryOptions.class).getBigQueryProject(); |
| 202 | + } |
| 203 | + String tempTableID = |
| 204 | + BigQueryResourceNaming.createJobIdPrefix( |
| 205 | + options.getJobName(), stepUuid, BigQueryResourceNaming.JobType.QUERY); |
| 206 | + TableReference tempTableReference = |
| 207 | + createTempTableReference(project, tempTableID, Optional.ofNullable(queryTempDataset)); |
| 208 | + Table destTable = datasetService.getTable(tempTableReference); |
| 209 | + if (destTable != null) { |
| 210 | + datasetService.deleteTable(tempTableReference); |
| 211 | + } |
| 212 | + } |
191 | 213 | } |
0 commit comments