Skip to content

Commit e63f06e

Browse files
author
Praful Makani
authored
docs(samples): add create job (#470)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary)
1 parent ca508cf commit e63f06e

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_create_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 com.google.common.collect.ImmutableMap;
28+
import java.util.UUID;
29+
30+
// Sample to create a job
31+
public class CreateJob {
32+
33+
public static void runCreateJob() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
36+
createJob(query);
37+
}
38+
39+
public static void createJob(String query) {
40+
try {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
44+
45+
// Specify a job configuration to set optional job resource properties.
46+
QueryJobConfiguration queryConfig =
47+
QueryJobConfiguration.newBuilder(query)
48+
.setLabels(ImmutableMap.of("example-label", "example-value"))
49+
.build();
50+
51+
// The location and job name are optional,
52+
// if both are not specified then client will auto-create.
53+
String jobName = "jobId_" + UUID.randomUUID().toString();
54+
JobId jobId = JobId.newBuilder().setLocation("us").setJob(jobName).build();
55+
56+
// Create a job with job ID
57+
bigquery.create(JobInfo.of(jobId, queryConfig));
58+
59+
// Get a job that was just created
60+
Job job = bigquery.getJob(jobId);
61+
if (job.getJobId().getJob().equals(jobId.getJob())) {
62+
System.out.println("Job created successfully");
63+
} else {
64+
System.out.println("Job was not created");
65+
}
66+
} catch (BigQueryException e) {
67+
System.out.println("Job was not created. \n" + e.toString());
68+
}
69+
}
70+
}
71+
// [END bigquery_create_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 CreateJobIT {
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+
CreateJob.createJob(query);
49+
assertThat(bout.toString()).contains("Job created successfully");
50+
}
51+
}

0 commit comments

Comments
 (0)