Search in sources :

Example 11 with Job

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

the class ITBigQueryTest method testInsertFromFile.

@Test
public void testInsertFromFile() throws InterruptedException, IOException, TimeoutException {
    String destinationTableName = "test_insert_from_file_table";
    TableId tableId = TableId.of(DATASET, destinationTableName);
    WriteChannelConfiguration configuration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.json()).setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED).setSchema(TABLE_SCHEMA).build();
    TableDataWriteChannel channel = bigquery.writer(configuration);
    try {
        channel.write(ByteBuffer.wrap(JSON_CONTENT.getBytes(StandardCharsets.UTF_8)));
    } finally {
        channel.close();
    }
    Job job = channel.getJob().waitFor();
    LoadStatistics statistics = job.getStatistics();
    assertEquals(1L, statistics.getInputFiles().longValue());
    assertEquals(2L, statistics.getOutputRows().longValue());
    LoadJobConfiguration jobConfiguration = job.getConfiguration();
    assertEquals(TABLE_SCHEMA, jobConfiguration.getSchema());
    assertNull(jobConfiguration.getSourceUris());
    assertNull(job.getStatus().getError());
    Page<List<FieldValue>> rows = bigquery.listTableData(tableId);
    int rowCount = 0;
    for (List<FieldValue> row : rows.getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue integerArrayCell = row.get(2);
        FieldValue booleanCell = row.get(3);
        FieldValue bytesCell = row.get(4);
        FieldValue recordCell = row.get(5);
        FieldValue integerCell = row.get(6);
        FieldValue floatCell = row.get(7);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.REPEATED, integerArrayCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, bytesCell.getAttribute());
        assertEquals(FieldValue.Attribute.RECORD, recordCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, floatCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(0, integerArrayCell.getRepeatedValue().get(0).getLongValue());
        assertEquals(1, integerArrayCell.getRepeatedValue().get(1).getLongValue());
        assertEquals(false, booleanCell.getBooleanValue());
        assertArrayEquals(BYTES, bytesCell.getBytesValue());
        assertEquals(-14182916000000L, recordCell.getRecordValue().get(0).getTimestampValue());
        assertTrue(recordCell.getRecordValue().get(1).isNull());
        assertEquals(1, recordCell.getRecordValue().get(2).getRepeatedValue().get(0).getLongValue());
        assertEquals(0, recordCell.getRecordValue().get(2).getRepeatedValue().get(1).getLongValue());
        assertEquals(true, recordCell.getRecordValue().get(3).getBooleanValue());
        assertEquals(3, integerCell.getLongValue());
        assertEquals(1.2, floatCell.getDoubleValue(), 0.0001);
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(bigquery.delete(DATASET, destinationTableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) LoadStatistics(com.google.cloud.bigquery.JobStatistics.LoadStatistics) TableDataWriteChannel(com.google.cloud.bigquery.TableDataWriteChannel) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LoadJobConfiguration(com.google.cloud.bigquery.LoadJobConfiguration) FieldValue(com.google.cloud.bigquery.FieldValue) Job(com.google.cloud.bigquery.Job) WriteChannelConfiguration(com.google.cloud.bigquery.WriteChannelConfiguration) Test(org.junit.Test)

Example 12 with Job

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

the class BigQuerySnippets method getJobFromId.

/**
   * Example of getting a job.
   */
// [TARGET getJob(JobId, JobOption...)]
// [VARIABLE "my_job_name"]
public Job getJobFromId(String jobName) {
    // [START getJobFromId]
    JobId jobIdObject = JobId.of(jobName);
    Job job = bigquery.getJob(jobIdObject);
    if (job == null) {
    // job was not found
    }
    // [END getJobFromId]
    return job;
}
Also used : Job(com.google.cloud.bigquery.Job) JobId(com.google.cloud.bigquery.JobId)

Example 13 with Job

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

the class CreateTableAndLoadData method main.

public static void main(String... args) throws InterruptedException, TimeoutException {
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    TableId tableId = TableId.of("dataset", "table");
    Table table = bigquery.getTable(tableId);
    if (table == null) {
        System.out.println("Creating table " + tableId);
        Field integerField = Field.of("fieldName", Field.Type.integer());
        Schema schema = Schema.of(integerField);
        table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
    }
    System.out.println("Loading data into table " + tableId);
    Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
    loadJob = loadJob.waitFor();
    if (loadJob.getStatus().getError() != null) {
        System.out.println("Job completed with errors");
    } else {
        System.out.println("Job succeeded");
    }
}
Also used : TableId(com.google.cloud.bigquery.TableId) Field(com.google.cloud.bigquery.Field) BigQuery(com.google.cloud.bigquery.BigQuery) Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) Job(com.google.cloud.bigquery.Job)

Example 14 with Job

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

the class BigQuerySnippets method createJob.

/**
   * Example of creating a query job.
   */
// [TARGET create(JobInfo, JobOption...)]
// [VARIABLE "SELECT field FROM my_dataset_name.my_table_name"]
public Job createJob(String query) {
    // [START createJob]
    Job job = null;
    JobConfiguration jobConfiguration = QueryJobConfiguration.of(query);
    JobInfo jobInfo = JobInfo.of(jobConfiguration);
    try {
        job = bigquery.create(jobInfo);
    } catch (BigQueryException e) {
    // the job was not created
    }
    // [END createJob]
    return job;
}
Also used : JobInfo(com.google.cloud.bigquery.JobInfo) BigQueryException(com.google.cloud.bigquery.BigQueryException) Job(com.google.cloud.bigquery.Job) JobConfiguration(com.google.cloud.bigquery.JobConfiguration) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration)

Example 15 with Job

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

the class BigQuerySnippets method writeToTable.

/**
   * Example of creating a channel with which to write to a table.
   */
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "StringValue1\nStringValue2\n"]
public long writeToTable(String datasetName, String tableName, String csvData) throws IOException, InterruptedException, TimeoutException {
    // [START writeToTable]
    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 {
        writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
    } finally {
        writer.close();
    }
    // Get load job
    Job job = writer.getJob();
    job = job.waitFor();
    LoadStatistics stats = job.getStatistics();
    return stats.getOutputRows();
// [END writeToTable]
}
Also used : TableId(com.google.cloud.bigquery.TableId) LoadStatistics(com.google.cloud.bigquery.JobStatistics.LoadStatistics) TableDataWriteChannel(com.google.cloud.bigquery.TableDataWriteChannel) Job(com.google.cloud.bigquery.Job) WriteChannelConfiguration(com.google.cloud.bigquery.WriteChannelConfiguration)

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