Skip to content
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.

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

docs(samples): add create job #470

Merged
merged 3 commits into from Jun 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions samples/snippets/src/main/java/com/example/bigquery/CreateJob.java
Expand Up @@ -27,16 +27,12 @@
import com.google.common.collect.ImmutableMap;
import java.util.UUID;

stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
// Sample to create a job
public class CreateJob {

public static void runCreateJob() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
// i.e. SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`
String query =
"SELECT country_name from `" + projectId + "." + datasetName + "." + tableName + "`";
String query = "SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";
createJob(query);
}

Expand All @@ -52,17 +48,21 @@ public static void createJob(String query) {
.setLabels(ImmutableMap.of("example-label", "example-value"))
.build();

// The client libraries automatically generate a job ID.
// Override the generated ID with either the job_id_prefix or job_id parameters.
String jobId = "code_sample_" + UUID.randomUUID().toString().substring(0, 8);
Job job = bigquery.create(JobInfo.of(JobId.of(jobId), queryConfig));
job = job.waitFor();
if (job.isDone()) {
// The location and JobName must be specified; other fields can be auto-detected.
String jobName = "jobId_" + UUID.randomUUID().toString();
JobId jobId = JobId.newBuilder().setLocation("us").setJob(jobName).build();

// Create a job with job ID
bigquery.create(JobInfo.of(jobId, queryConfig));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe jobId is optional right? we don't need to create our own. So I think we can link user to the documentation that explains this.


// Get a job that was just created
Job job = bigquery.getJob(jobId);
if (job.getJobId().getJob().equals(jobId.getJob())) {
System.out.println("Job created successfully");
} else {
System.out.println("Job was not created");
}
} catch (BigQueryException | InterruptedException e) {
} catch (BigQueryException e) {
System.out.println("Job was not created. \n" + e.toString());
}
}
Expand Down