Skip to content

Commit 3c1884b

Browse files
author
Praful Makani
authored
docs(samples): add get job (#503)
1 parent c8c3b70 commit 3c1884b

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

samples/snippets/src/main/java/com/example/bigquery/CreateJob.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static void createJob(String query) {
5959
// Get a job that was just created
6060
Job job = bigquery.getJob(jobId);
6161
if (job.getJobId().getJob().equals(jobId.getJob())) {
62-
System.out.println("Job created successfully");
62+
System.out.print("Job created successfully." + job.getJobId().getJob());
6363
} else {
64-
System.out.println("Job was not created");
64+
System.out.print("Job was not created");
6565
}
6666
} catch (BigQueryException e) {
67-
System.out.println("Job was not created. \n" + e.toString());
67+
System.out.print("Job was not created. \n" + e.toString());
6868
}
6969
}
7070
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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_get_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+
26+
// Sample to get a job
27+
public class GetJob {
28+
29+
public static void runGetJob() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String jobName = "MY_JOB_NAME";
32+
getJob(jobName);
33+
}
34+
35+
public static void getJob(String jobName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
JobId jobId = JobId.of(jobName);
42+
Job job = bigquery.getJob(jobId);
43+
System.out.println("Job retrieved successfully");
44+
} catch (BigQueryException e) {
45+
System.out.println("Job not retrieved. \n" + e.toString());
46+
}
47+
}
48+
}
49+
// [END bigquery_get_job]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 GetJobIT {
28+
29+
private String jobName;
30+
private ByteArrayOutputStream bout;
31+
private PrintStream out;
32+
33+
@Before
34+
public void setUp() {
35+
bout = new ByteArrayOutputStream();
36+
out = new PrintStream(bout);
37+
System.setOut(out);
38+
39+
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
40+
CreateJob.createJob(query);
41+
String result = bout.toString();
42+
jobName = result.substring(result.lastIndexOf(".") + 1);
43+
44+
bout = new ByteArrayOutputStream();
45+
out = new PrintStream(bout);
46+
System.setOut(out);
47+
}
48+
49+
@After
50+
public void tearDown() {
51+
System.setOut(null);
52+
}
53+
54+
@Test
55+
public void testGetJob() {
56+
GetJob.getJob(jobName);
57+
assertThat(bout.toString()).contains("Job retrieved successfully");
58+
}
59+
}

0 commit comments

Comments
 (0)