Search in sources :

Example 21 with Job

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

the class ITJobSnippets method testWaitForWithOptions.

@Test
public void testWaitForWithOptions() throws Exception {
    JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
    JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
    Job job = bigquery.create(jobInfo);
    JobSnippets jobSnippets = new JobSnippets(job);
    boolean result = jobSnippets.waitForWithOptions();
    assertTrue(result);
}
Also used : JobInfo(com.google.cloud.bigquery.JobInfo) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobConfiguration(com.google.cloud.bigquery.JobConfiguration) Test(org.junit.Test)

Example 22 with Job

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

the class ITJobSnippets method testReloadStatus.

@Test
public void testReloadStatus() throws Exception {
    JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
    JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
    Job job = bigquery.create(jobInfo);
    JobSnippets jobSnippets = new JobSnippets(job);
    JobStatus.State result = jobSnippets.reloadStatus();
    assertEquals(JobStatus.State.DONE, result);
}
Also used : JobStatus(com.google.cloud.bigquery.JobStatus) JobInfo(com.google.cloud.bigquery.JobInfo) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobConfiguration(com.google.cloud.bigquery.JobConfiguration) Test(org.junit.Test)

Example 23 with Job

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

the class ITJobSnippets method testReload.

@Test
public void testReload() throws Exception {
    JobConfiguration jobConfig = QueryJobConfiguration.newBuilder(QUERY).setUseLegacySql(false).build();
    JobInfo jobInfo = JobInfo.newBuilder(jobConfig).build();
    Job job = bigquery.create(jobInfo);
    JobSnippets jobSnippets = new JobSnippets(job);
    JobStatus.State result = jobSnippets.reload();
    assertEquals(JobStatus.State.DONE, result);
}
Also used : JobStatus(com.google.cloud.bigquery.JobStatus) JobInfo(com.google.cloud.bigquery.JobInfo) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) JobConfiguration(com.google.cloud.bigquery.JobConfiguration) Test(org.junit.Test)

Example 24 with Job

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

the class BigQuerySnippets method writeFileToTable.

/**
   * Example of writing a local file to a table.
   */
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")]
public long writeFileToTable(String datasetName, String tableName, Path csvPath) throws IOException, InterruptedException, TimeoutException {
    // [START writeFileToTable]
    TableId tableId = TableId.of(datasetName, tableName);
    WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
    TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
    // Write data to writer
    try (OutputStream stream = Channels.newOutputStream(writer)) {
        Files.copy(csvPath, stream);
    }
    // Get load job
    Job job = writer.getJob();
    job = job.waitFor();
    LoadStatistics stats = job.getStatistics();
    return stats.getOutputRows();
// [END writeFileToTable]
}
Also used : TableId(com.google.cloud.bigquery.TableId) LoadStatistics(com.google.cloud.bigquery.JobStatistics.LoadStatistics) OutputStream(java.io.OutputStream) TableDataWriteChannel(com.google.cloud.bigquery.TableDataWriteChannel) Job(com.google.cloud.bigquery.Job) WriteChannelConfiguration(com.google.cloud.bigquery.WriteChannelConfiguration)

Example 25 with Job

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

the class TableSnippets method copyTableId.

/**
   * Example copying the table to a destination table.
   */
// [TARGET copy(TableId, JobOption...)]
// [VARIABLE "my_dataset"]
// [VARIABLE "my_destination_table"]
public Job copyTableId(String dataset, String tableName) throws BigQueryException {
    // [START copyTableId]
    TableId destinationId = TableId.of(dataset, tableName);
    JobOption options = JobOption.fields(JobField.STATUS, JobField.USER_EMAIL);
    Job job = table.copy(destinationId, options);
    // 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 copyTableId]
    return job;
}
Also used : TableId(com.google.cloud.bigquery.TableId) Job(com.google.cloud.bigquery.Job) JobOption(com.google.cloud.bigquery.BigQuery.JobOption) TimeoutException(java.util.concurrent.TimeoutException)

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