|
| 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] |
0 commit comments