Search in sources :

Example 16 with Job

use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.

the class TableSnippets method loadList.

/**
   * Example loading data from a list of Google Cloud Storage files.
   */
// [TARGET load(FormatOptions, List, JobOption...)]
// [VARIABLE "gs://my_bucket/filename1.csv"]
// [VARIABLE "gs://my_bucket/filename2.csv"]
public Job loadList(String gcsUrl1, String gcsUrl2) {
    // [START loadList]
    List<String> sourceUris = new ArrayList<>();
    sourceUris.add(gcsUrl1);
    sourceUris.add(gcsUrl2);
    Job job = table.load(FormatOptions.csv(), sourceUris);
    // Wait for the job to complete
    try {
        Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(3, TimeUnit.MINUTES));
        if (completedJob != null && completedJob.getStatus().getError() == null) {
        // Job completed successfully
        } else {
        // Handle error case
        }
    } catch (InterruptedException | TimeoutException e) {
    // Handle interrupted wait
    }
    // [END loadList]
    return job;
}
Also used : ArrayList(java.util.ArrayList) Job(com.google.cloud.bigquery.Job) TimeoutException(java.util.concurrent.TimeoutException)

Example 17 with Job

use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.

the class TableSnippets method extractList.

/**
   * Example of partitioning data to a list of Google Cloud Storage files.
   */
// [TARGET extract(String, List, JobOption...)]
// [VARIABLE "CSV"]
// [VARIABLE "gs://my_bucket/PartitionA_*.csv"]
// [VARIABLE "gs://my_bucket/PartitionB_*.csv"]
public Job extractList(String format, String gcsUrl1, String gcsUrl2) {
    // [START extractList]
    List<String> destinationUris = new ArrayList<>();
    destinationUris.add(gcsUrl1);
    destinationUris.add(gcsUrl2);
    Job job = table.extract(format, destinationUris);
    // Wait for the job to complete
    try {
        Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(3, TimeUnit.MINUTES));
        if (completedJob != null && completedJob.getStatus().getError() == null) {
        // Job completed successfully
        } else {
        // Handle error case
        }
    } catch (InterruptedException | TimeoutException e) {
    // Handle interrupted wait
    }
    // [END extractList]
    return job;
}
Also used : ArrayList(java.util.ArrayList) Job(com.google.cloud.bigquery.Job) TimeoutException(java.util.concurrent.TimeoutException)

Example 18 with Job

use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testExtractJob.

@Test
public void testExtractJob() throws InterruptedException, TimeoutException {
    String tableName = "test_export_job_table";
    TableId destinationTable = TableId.of(DATASET, tableName);
    LoadJobConfiguration configuration = LoadJobConfiguration.newBuilder(destinationTable, "gs://" + BUCKET + "/" + LOAD_FILE).setSchema(SIMPLE_SCHEMA).build();
    Job remoteLoadJob = bigquery.create(JobInfo.of(configuration));
    remoteLoadJob = remoteLoadJob.waitFor();
    assertNull(remoteLoadJob.getStatus().getError());
    ExtractJobConfiguration extractConfiguration = ExtractJobConfiguration.newBuilder(destinationTable, "gs://" + BUCKET + "/" + EXTRACT_FILE).setPrintHeader(false).build();
    Job remoteExtractJob = bigquery.create(JobInfo.of(extractConfiguration));
    remoteExtractJob = remoteExtractJob.waitFor();
    assertNull(remoteExtractJob.getStatus().getError());
    assertEquals(CSV_CONTENT, new String(storage.readAllBytes(BUCKET, EXTRACT_FILE), StandardCharsets.UTF_8));
    assertTrue(bigquery.delete(DATASET, tableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) LoadJobConfiguration(com.google.cloud.bigquery.LoadJobConfiguration) Job(com.google.cloud.bigquery.Job) ExtractJobConfiguration(com.google.cloud.bigquery.ExtractJobConfiguration) Test(org.junit.Test)

Example 19 with Job

use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testListJobsWithSelectedFields.

@Test
public void testListJobsWithSelectedFields() {
    Page<Job> jobs = bigquery.listJobs(JobListOption.fields(JobField.USER_EMAIL));
    for (Job job : jobs.getValues()) {
        assertNotNull(job.getJobId());
        assertNotNull(job.getStatus());
        assertNotNull(job.getUserEmail());
        assertNull(job.getStatistics());
        assertNull(job.getGeneratedId());
    }
}
Also used : Job(com.google.cloud.bigquery.Job) Test(org.junit.Test)

Example 20 with Job

use of com.google.cloud.bigquery.Job in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQuerySnippets method testJob.

@Test
public void testJob() throws ExecutionException, InterruptedException {
    Job job1 = bigquerySnippets.createJob(QUERY);
    Job job2 = bigquerySnippets.createJob(QUERY);
    assertNotNull(job1);
    assertNotNull(job2);
    assertEquals(job1.getJobId(), bigquerySnippets.getJob(job1.getJobId().getJob()).getJobId());
    assertEquals(job2.getJobId(), bigquerySnippets.getJobFromId(job2.getJobId().getJob()).getJobId());
    Set<JobId> jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION));
    while (!jobs.contains(job1.getJobId()) || !jobs.contains(job2.getJobId())) {
        Thread.sleep(500);
        jobs = Sets.newHashSet(Iterators.transform(bigquerySnippets.listJobs().iterateAll().iterator(), TO_JOB_ID_FUNCTION));
    }
    assertTrue(bigquerySnippets.cancelJob(job1.getJobId().getJob()));
    assertTrue(bigquerySnippets.cancelJobFromId(job2.getJobId().getJob()));
}
Also used : Job(com.google.cloud.bigquery.Job) JobId(com.google.cloud.bigquery.JobId) Test(org.junit.Test)

Aggregations

Job (com.google.cloud.bigquery.Job)30 Test (org.junit.Test)21 TableId (com.google.cloud.bigquery.TableId)11 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)10 JobConfiguration (com.google.cloud.bigquery.JobConfiguration)8 JobInfo (com.google.cloud.bigquery.JobInfo)8 Table (com.google.cloud.bigquery.Table)4 CopyJobConfiguration (com.google.cloud.bigquery.CopyJobConfiguration)3 FieldValue (com.google.cloud.bigquery.FieldValue)3 LoadStatistics (com.google.cloud.bigquery.JobStatistics.LoadStatistics)3 LoadJobConfiguration (com.google.cloud.bigquery.LoadJobConfiguration)3 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)3 TableDataWriteChannel (com.google.cloud.bigquery.TableDataWriteChannel)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 WriteChannelConfiguration (com.google.cloud.bigquery.WriteChannelConfiguration)3 TimeoutException (java.util.concurrent.TimeoutException)3 JobId (com.google.cloud.bigquery.JobId)2 JobStatistics (com.google.cloud.bigquery.JobStatistics)2 JobStatus (com.google.cloud.bigquery.JobStatus)2 QueryResponse (com.google.cloud.bigquery.QueryResponse)2