GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add more BigQuery Job snippets. by tswast · Pull Request #1255 · googleapis/google-cloud-java

Add more BigQuery Job snippets. #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
mziccard merged 1 commit into googleapis:master from tswast:tswast-jobsnippets
Sep 19, 2016

Uh oh!

There was an error while loading. Please reload this page.

Merged

Add more BigQuery Job snippets. #1255

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
781cabb
Add more BigQuery Job snippets.
tswast Sep 16, 2016
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

32 changes: 32 additions & 0 deletions 32 google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ public Job build() {
/**
* Checks if this job exists.
*
* <p>Example of checking that a job exists.
* <pre> {@code
* if (!job.exists()) {
* // job doesn't exist
* }
* }</pre>
*
* @return {@code true} if this job exists, {@code false} otherwise
* @throws BigQueryException upon failure
*/
Expand Down Expand Up @@ -214,6 +221,22 @@ public Job waitFor(WaitForOption... waitOptions) throws InterruptedException, Ti
/**
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
*
* <p>Example of reloading all fields until job status is DONE.
* <pre> {@code
* while (job.status().state() != JobStatus.State.DONE) {
* Thread.sleep(1000L);
* job = job.reload();
* }
* }</pre>
*
* <p>Example of reloading status field until job status is DONE.
* <pre> {@code
* while (job.status().state() != JobStatus.State.DONE) {
* Thread.sleep(1000L);
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
* }
* }</pre>
*
* @param options job options
* @return a {@code Job} object with latest information or {@code null} if not found
* @throws BigQueryException upon failure
Expand All @@ -225,6 +248,15 @@ public Job reload(JobOption... options) {
/**
* Sends a job cancel request.
*
* <p>Example of cancelling a job.
* <pre> {@code
* if (job.cancel()) {
* return true; // job successfully cancelled
* } else {
* // job not found
* }
* }</pre>
*
* @return {@code true} if cancel request was sent successfully, {@code false} if job was not
* found
* @throws BigQueryException upon failure
Expand Down
64 changes: 61 additions & 3 deletions 64 ...cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,34 @@
package com.google.cloud.examples.bigquery.snippets;

import com.google.cloud.WaitForOption;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobStatus;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class JobSnippets {

private final Job job;
private Job job;

public JobSnippets(Job job) {
this.job = job;
}

/**
* Example of checking that a job exists.
*/
// [TARGET exists()]
public boolean exists() throws InterruptedException {
// [START exists]
if (!job.exists()) {
// job doesn't exist
}
// [END exists]
return job.exists();
}

/**
* Example of waiting for a job until it reports that it is done.
*/
Expand Down Expand Up @@ -77,7 +92,7 @@ public boolean waitFor() throws InterruptedException {
// [TARGET waitFor(WaitForOption...)]
public boolean waitForWithOptions() throws InterruptedException {
try {
// [START waitFor]
// [START waitForWithOptions]
Job completedJob =
job.waitFor(
WaitForOption.checkEvery(1, TimeUnit.SECONDS),
Expand All @@ -89,10 +104,53 @@ public boolean waitForWithOptions() throws InterruptedException {
} else {
// job completed successfully
}
// [END waitFor]
// [END waitForWithOptions]
} catch (TimeoutException e) {
return true;
}
return true;
}

/**
* Example of reloading all fields until job status is DONE.
*/
// [TARGET reload(JobOption...)]
public JobStatus.State reload() throws InterruptedException {
// [START reload]
while (job.status().state() != JobStatus.State.DONE) {
Thread.sleep(1000L);
job = job.reload();
}
// [END reload]
return job.status().state();
}

/**
* Example of reloading status field until job status is DONE.
*/
// [TARGET reload(JobOption...)]
public JobStatus.State reloadStatus() throws InterruptedException {
// [START reloadStatus]
while (job.status().state() != JobStatus.State.DONE) {
Thread.sleep(1000L);
job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
}
// [END reloadStatus]
return job.status().state();
}

/**
* Example of cancelling a job.
*/
// [TARGET cancel()]
public boolean cancel() {
// [START cancel]
if (job.cancel()) {
return true; // job successfully cancelled
} else {
// job not found
}
// [END cancel]
return false;
}
}
42 changes: 42 additions & 0 deletions 42 ...oud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobConfiguration;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatus;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;

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

@Test
public void testExists() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
boolean result = jobSnippets.exists();
assertTrue(result);
}

@Test
public void testIsDone() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
Expand Down Expand Up @@ -69,4 +81,34 @@ public void testWaitForWithOptions() throws Exception {
boolean result = jobSnippets.waitForWithOptions();
assertTrue(result);
}

@Test
public void testReload() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
JobStatus.State result = jobSnippets.reload();
assertEquals(JobStatus.State.DONE, result);
}

@Test
public void testReloadStatus() throws Exception {
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
JobStatus.State result = jobSnippets.reloadStatus();
assertEquals(JobStatus.State.DONE, result);
}

@Test
public void testCancel() {
JobConfiguration jobConfig = QueryJobConfiguration.builder(QUERY).useLegacySql(false).build();
JobInfo jobInfo = JobInfo.builder(jobConfig).build();
Job job = bigquery.create(jobInfo);
JobSnippets jobSnippets = new JobSnippets(job);
boolean result = jobSnippets.cancel();
assertTrue(result);
}
}
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.