Skip to content

Commit e2dc825

Browse files
committed
Add more BigQuery Job snippets.
This should be the last of the snippets needed for BigQuery Job.
1 parent 0db14b7 commit e2dc825

File tree

3 files changed

+136
-2
lines changed
  • google-cloud-bigquery/src/main/java/com/google/cloud/bigquery
  • google-cloud-examples/src

3 files changed

+136
-2
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ public Job build() {
127127
/**
128128
* Checks if this job exists.
129129
*
130+
* <p>Example of checking that a job exists.
131+
* <pre> {@code
132+
* if (!job.exists()) {
133+
* // job doesn't exist
134+
* }
135+
* }</pre>
136+
*
130137
* @return {@code true} if this job exists, {@code false} otherwise
131138
* @throws BigQueryException upon failure
132139
*/
@@ -214,6 +221,22 @@ public Job waitFor(WaitForOption... waitOptions) throws InterruptedException, Ti
214221
/**
215222
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
216223
*
224+
* <p>Example of reloading all fields until job status is DONE.
225+
* <pre> {@code
226+
* while (job.status().state() != JobStatus.State.DONE) {
227+
* Thread.sleep(1000L);
228+
* job = job.reload();
229+
* }
230+
* }</pre>
231+
*
232+
* <p>Example of reloading status field until job status is DONE.
233+
* <pre> {@code
234+
* while (job.status().state() != JobStatus.State.DONE) {
235+
* Thread.sleep(1000L);
236+
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
237+
* }
238+
* }</pre>
239+
*
217240
* @param options job options
218241
* @return a {@code Job} object with latest information or {@code null} if not found
219242
* @throws BigQueryException upon failure
@@ -225,6 +248,15 @@ public Job reload(JobOption... options) {
225248
/**
226249
* Sends a job cancel request.
227250
*
251+
* <p>Example of cancelling a job.
252+
* <pre> {@code
253+
* if (job.cancel()) {
254+
* return true; // job successfully cancelled
255+
* } else {
256+
* // job not found
257+
* }
258+
* }</pre>
259+
*
228260
* @return {@code true} if cancel request was sent successfully, {@code false} if job was not
229261
* found
230262
* @throws BigQueryException upon failure

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
package com.google.cloud.examples.bigquery.snippets;
2424

2525
import com.google.cloud.WaitForOption;
26+
import com.google.cloud.bigquery.BigQuery;
2627
import com.google.cloud.bigquery.Job;
28+
import com.google.cloud.bigquery.JobStatus;
2729

2830
import java.util.concurrent.TimeUnit;
2931
import java.util.concurrent.TimeoutException;
@@ -36,6 +38,19 @@ public JobSnippets(Job job) {
3638
this.job = job;
3739
}
3840

41+
/**
42+
* Example of checking that a job exists.
43+
*/
44+
// [TARGET exists()]
45+
public boolean exists() throws InterruptedException {
46+
// [START exists]
47+
if (!job.exists()) {
48+
// job doesn't exist
49+
}
50+
// [END exists]
51+
return job.exists();
52+
}
53+
3954
/**
4055
* Example of waiting for a job until it reports that it is done.
4156
*/
@@ -77,7 +92,7 @@ public boolean waitFor() throws InterruptedException {
7792
// [TARGET waitFor(WaitForOption...)]
7893
public boolean waitForWithOptions() throws InterruptedException {
7994
try {
80-
// [START waitFor]
95+
// [START waitForWithOptions]
8196
Job completedJob =
8297
job.waitFor(
8398
WaitForOption.checkEvery(1, TimeUnit.SECONDS),
@@ -89,10 +104,55 @@ public boolean waitForWithOptions() throws InterruptedException {
89104
} else {
90105
// job completed successfully
91106
}
92-
// [END waitFor]
107+
// [END waitForWithOptions]
93108
} catch (TimeoutException e) {
94109
return true;
95110
}
96111
return true;
97112
}
113+
114+
/**
115+
* Example of reloading all fields until job status is DONE.
116+
*/
117+
// [TARGET reload(JobOption...)]
118+
public JobStatus.State reload() throws InterruptedException {
119+
Job job = this.job;
120+
// [START reload]
121+
while (job.status().state() != JobStatus.State.DONE) {
122+
Thread.sleep(1000L);
123+
job = job.reload();
124+
}
125+
// [END reload]
126+
return job.status().state();
127+
}
128+
129+
/**
130+
* Example of reloading status field until job status is DONE.
131+
*/
132+
// [TARGET reload(JobOption...)]
133+
public JobStatus.State reloadStatus() throws InterruptedException {
134+
Job job = this.job;
135+
// [START reloadStatus]
136+
while (job.status().state() != JobStatus.State.DONE) {
137+
Thread.sleep(1000L);
138+
job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
139+
}
140+
// [END reloadStatus]
141+
return job.status().state();
142+
}
143+
144+
/**
145+
* Example of cancelling a job.
146+
*/
147+
// [TARGET cancel()]
148+
public boolean cancel() {
149+
// [START cancel]
150+
if (job.cancel()) {
151+
return true; // job successfully cancelled
152+
} else {
153+
// job not found
154+
}
155+
// [END cancel]
156+
return false;
157+
}
98158
}

google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package com.google.cloud.examples.bigquery.snippets;
1818

19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertTrue;
2021

2122
import com.google.cloud.bigquery.BigQuery;
2223
import com.google.cloud.bigquery.Job;
2324
import com.google.cloud.bigquery.JobConfiguration;
2425
import com.google.cloud.bigquery.JobInfo;
26+
import com.google.cloud.bigquery.JobStatus;
2527
import com.google.cloud.bigquery.QueryJobConfiguration;
2628
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
2729

@@ -40,6 +42,16 @@ public static void beforeClass() {
4042
bigquery = RemoteBigQueryHelper.create().options().service();
4143
}
4244

45+
@Test
46+
public void testExists() throws Exception {
47+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
48+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
49+
Job job = bigquery.create(jobInfo);
50+
JobSnippets jobSnippets = new JobSnippets(job);
51+
boolean result = jobSnippets.exists();
52+
assertTrue(result);
53+
}
54+
4355
@Test
4456
public void testIsDone() throws Exception {
4557
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
@@ -69,4 +81,34 @@ public void testWaitForWithOptions() throws Exception {
6981
boolean result = jobSnippets.waitForWithOptions();
7082
assertTrue(result);
7183
}
84+
85+
@Test
86+
public void testReload() throws Exception {
87+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
88+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
89+
Job job = bigquery.create(jobInfo);
90+
JobSnippets jobSnippets = new JobSnippets(job);
91+
JobStatus.State result = jobSnippets.reload();
92+
assertEquals(JobStatus.State.DONE, result);
93+
}
94+
95+
@Test
96+
public void testReloadStatus() throws Exception {
97+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
98+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
99+
Job job = bigquery.create(jobInfo);
100+
JobSnippets jobSnippets = new JobSnippets(job);
101+
JobStatus.State result = jobSnippets.reloadStatus();
102+
assertEquals(JobStatus.State.DONE, result);
103+
}
104+
105+
@Test
106+
public void testCancel() throws Exception {
107+
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
108+
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
109+
Job job = bigquery.create(jobInfo);
110+
JobSnippets jobSnippets = new JobSnippets(job);
111+
boolean result = jobSnippets.cancel();
112+
assertTrue(result);
113+
}
72114
}

0 commit comments

Comments
 (0)