Skip to content

Commit 2aeedac

Browse files
committed
Add BigQuery snippets for Job.isDone().
Runs google-java-format against the BigQuery snippets, too.
1 parent 3c62510 commit 2aeedac

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ public static void main(String... args) throws InterruptedException {
7070
firstRow.put("StringField", "value1");
7171
secondRow.put("StringField", "value2");
7272
// Create an insert request
73-
InsertAllRequest insertRequest = InsertAllRequest.builder(tableId)
74-
.addRow(firstRow)
75-
.addRow(secondRow)
76-
.build();
73+
InsertAllRequest insertRequest =
74+
InsertAllRequest.builder(tableId).addRow(firstRow).addRow(secondRow).build();
7775
// Insert rows
7876
InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
7977
// Check if errors occurred
@@ -82,10 +80,11 @@ public static void main(String... args) throws InterruptedException {
8280
}
8381

8482
// Create a query request
85-
QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
86-
.maxWaitTime(60000L)
87-
.pageSize(1000L)
88-
.build();
83+
QueryRequest queryRequest =
84+
QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
85+
.maxWaitTime(60000L)
86+
.pageSize(1000L)
87+
.build();
8988
// Request query to be executed and wait for results
9089
QueryResponse queryResponse = bigquery.query(queryRequest);
9190
while (!queryResponse.jobCompleted()) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Job's javadoc. Any change to this file should be reflected in
20+
* Job's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.bigquery.snippets;
24+
25+
import com.google.cloud.bigquery.Job;
26+
27+
public class JobSnippets {
28+
29+
private final Job job;
30+
31+
public JobSnippets(Job job) {
32+
this.job = job;
33+
}
34+
35+
/**
36+
* Example of waiting for a job until it reports that it is done.
37+
*/
38+
// [TARGET isDone()]
39+
public boolean isDone() {
40+
try {
41+
// [START isDone]
42+
while (!job.isDone()) {
43+
Thread.sleep(1000L);
44+
}
45+
// [END isDone]
46+
} catch (InterruptedException e) {
47+
return false;
48+
}
49+
return true;
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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.google.cloud.examples.bigquery.snippets;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.Job;
23+
import com.google.cloud.bigquery.JobConfiguration;
24+
import com.google.cloud.bigquery.JobInfo;
25+
import com.google.cloud.bigquery.QueryJobConfiguration;
26+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
27+
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class ITJobSnippets {
32+
33+
private static BigQuery bigquery;
34+
35+
@BeforeClass
36+
public static void beforeClass() {
37+
bigquery = RemoteBigQueryHelper.create().options().service();
38+
}
39+
40+
@Test
41+
public void testIsDone() throws Exception {
42+
JobConfiguration jobConfig =
43+
QueryJobConfiguration.builder(
44+
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;")
45+
.useLegacySql(false)
46+
.build();
47+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
48+
Job job = bigquery.create(jobInfo);
49+
JobSnippets jobSnippets = new JobSnippets(job);
50+
boolean result = jobSnippets.isDone();
51+
assertTrue(result);
52+
}
53+
}

0 commit comments

Comments
 (0)