Skip to content

Commit fdea80e

Browse files
author
Praful Makani
authored
docs(samples): add cancel job (#487)
1 parent 75c1379 commit fdea80e

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.example.bigquery;
18+
19+
// [START bigquery_cancel_job]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Job;
24+
import com.google.cloud.bigquery.JobId;
25+
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.QueryJobConfiguration;
27+
import java.util.UUID;
28+
29+
// Sample to cancel a job
30+
public class CancelJob {
31+
32+
public static void runCancelJob() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
35+
cancelJob(query);
36+
}
37+
38+
public static void cancelJob(String query) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
// Specify a job configuration to set optional job resource properties.
45+
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
46+
47+
// The location and job name are optional,
48+
// if both are not specified then client will auto-create.
49+
String jobName = "jobId_" + UUID.randomUUID().toString();
50+
JobId jobId = JobId.newBuilder().setLocation("us").setJob(jobName).build();
51+
52+
// Create a job with job ID
53+
bigquery.create(JobInfo.of(jobId, queryConfig));
54+
55+
// Get a job that was just created
56+
Job job = bigquery.getJob(jobId);
57+
if (job.cancel()) {
58+
System.out.println("Job canceled successfully");
59+
} else {
60+
System.out.println("Job was not canceled");
61+
}
62+
} catch (BigQueryException e) {
63+
System.out.println("Job was not canceled.\n" + e.toString());
64+
}
65+
}
66+
}
67+
// [END bigquery_cancel_job]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class CancelJobIT {
28+
29+
private ByteArrayOutputStream bout;
30+
private PrintStream out;
31+
32+
@Before
33+
public void setUp() {
34+
bout = new ByteArrayOutputStream();
35+
out = new PrintStream(bout);
36+
System.setOut(out);
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
System.setOut(null);
42+
}
43+
44+
@Test
45+
public void testCreateJob() {
46+
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
47+
48+
CancelJob.cancelJob(query);
49+
assertThat(bout.toString()).contains("Job canceled successfully");
50+
}
51+
}

0 commit comments

Comments
 (0)