Skip to content

Commit

Permalink
feat: add support for jobs.delete (#1387)
Browse files Browse the repository at this point in the history
* feat: add support for jobs.delete

* update IT

* lint

* updates
  • Loading branch information
stephaniewang526 committed Jun 24, 2021
1 parent ac854c4 commit 95f1a6c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
12 changes: 6 additions & 6 deletions google-cloud-bigquery/clirr-ignored-differences.xml
Expand Up @@ -3,13 +3,13 @@
<differences>
<!-- TODO: REMOVE AFTER RELEASE -->
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/ExternalTableDefinition</className>
<method>com.google.common.collect.ImmutableList getDecimalTargetTypes()</method>
<differenceType>7012</differenceType>
<className>com/google/cloud/bigquery/BigQuery</className>
<method>boolean delete(com.google.cloud.bigquery.JobId)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/bigquery/ExternalTableDefinition$Builder</className>
<method> com.google.cloud.bigquery.ExternalTableDefinition$Builder setDecimalTargetTypes(java.util.List)</method>
<differenceType>7012</differenceType>
<className>com/google/cloud/bigquery/spi/v2/BigQueryRpc</className>
<method>boolean deleteJob(java.lang.String, java.lang.String, java.lang.String)</method>
</difference>
</differences>
Expand Up @@ -982,6 +982,14 @@ public int hashCode() {
*/
boolean delete(RoutineId routineId);

/**
* Deletes the requested job.
*
* @return {@code true} if job was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(JobId jobId);

/**
* Updates dataset information.
*
Expand Down
Expand Up @@ -585,6 +585,30 @@ public Boolean call() {
}
}

@Override
public boolean delete(JobId jobId) {
final JobId completeJobId =
jobId.setProjectId(
Strings.isNullOrEmpty(jobId.getProject())
? getOptions().getProjectId()
: jobId.getProject());
try {
return runWithRetries(
new Callable<Boolean>() {
@Override
public Boolean call() {
return bigQueryRpc.deleteJob(
completeJobId.getProject(), completeJobId.getJob(), completeJobId.getLocation());
}
},
getOptions().getRetrySettings(),
EXCEPTION_HANDLER,
getOptions().getClock());
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
}

@Override
public Dataset update(DatasetInfo datasetInfo, DatasetOption... options) {
final com.google.api.services.bigquery.model.Dataset datasetPb =
Expand Down
Expand Up @@ -270,6 +270,14 @@ TableDataList listTableData(
*/
boolean cancel(String projectId, String jobId, String location);

/**
* Sends a job delete request.
*
* @return {@code true} if delete was successful, {@code false} if the job was not found
* @throws BigQueryException upon failure
*/
boolean deleteJob(String projectId, String jobName, String location);

/**
* Returns results of the query associated with the provided job.
*
Expand Down
Expand Up @@ -607,6 +607,21 @@ public boolean cancel(String projectId, String jobId, String location) {
}
}

@Override
public boolean deleteJob(String projectId, String jobName, String location) {
try {
bigquery
.jobs()
.delete(projectId, jobName)
.setLocation(location)
.setPrettyPrint(false)
.execute();
return true;
} catch (IOException ex) {
throw translate(ex);
}
}

@Override
public GetQueryResultsResponse getQueryResults(
String projectId, String jobId, String location, Map<Option, ?> options) {
Expand Down
Expand Up @@ -1243,6 +1243,20 @@ public void testDeleteNonExistingTable() {
assertFalse(bigquery.delete("test_delete_non_existing_table"));
}

@Test
public void testDeleteJob() {
String query = "SELECT 17 as foo";
QueryJobConfiguration config = QueryJobConfiguration.of(query);
String jobName = "jobId_" + UUID.randomUUID().toString();
JobId jobId =
JobId.newBuilder().setLocation("us-east1").setJob(jobName).setProject(PROJECT_ID).build();
Job createdJob = bigquery.create(JobInfo.of(jobId, config));
Job remoteJob = bigquery.getJob(createdJob.getJobId());
assertEquals(createdJob.getJobId(), remoteJob.getJobId());
assertTrue(bigquery.delete(jobId));
assertNull(bigquery.getJob(jobId));
}

@Test
public void testInsertAll() throws IOException {
String tableName = "test_insert_all_table";
Expand Down

0 comments on commit 95f1a6c

Please sign in to comment.