Skip to content

Commit b87cecf

Browse files
author
Praful Makani
authored
docs(samples): add list jobs (#554)
* docs(samples): add list jobs * docs(samples): update code
1 parent a0d7bea commit b87cecf

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_list_jobs]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Job;
25+
26+
// Sample to get list of jobs
27+
public class ListJobs {
28+
29+
public static void runListJobs() {
30+
listJobs();
31+
}
32+
33+
public static void listJobs() {
34+
try {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
Page<Job> jobs = bigquery.listJobs(BigQuery.JobListOption.pageSize(10));
40+
if (jobs == null) {
41+
System.out.println("Dataset does not contain any jobs.");
42+
return;
43+
}
44+
jobs.getValues().forEach(job -> System.out.printf("Success! Job ID: %s", job.getJobId()));
45+
} catch (BigQueryException e) {
46+
System.out.println("Jobs not listed in dataset due to error: \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_list_jobs]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 ListJobsIT {
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+
// Create a new job
39+
String query =
40+
"SELECT name"
41+
+ " FROM `bigquery-public-data.usa_names.usa_1910_2013`"
42+
+ " WHERE state = 'TX'"
43+
+ " LIMIT 100;";
44+
CreateJob.createJob(query);
45+
46+
bout = new ByteArrayOutputStream();
47+
out = new PrintStream(bout);
48+
System.setOut(out);
49+
}
50+
51+
@After
52+
public void tearDown() {
53+
// Clean up
54+
System.setOut(null);
55+
}
56+
57+
@Test
58+
public void testListJobs() {
59+
ListJobs.listJobs();
60+
assertThat(bout.toString()).contains("Success! Job ID");
61+
}
62+
}

0 commit comments

Comments
 (0)