Search in sources :

Example 1 with TableInfo

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

the class ITBigQueryTest method testCreateAndGetJob.

@Test
public void testCreateAndGetJob() throws InterruptedException, TimeoutException {
    String sourceTableName = "test_create_and_get_job_source_table";
    String destinationTableName = "test_create_and_get_job_destination_table";
    TableId sourceTable = TableId.of(DATASET, sourceTableName);
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(sourceTableName, createdTable.getTableId().getTable());
    TableId destinationTable = TableId.of(DATASET, destinationTableName);
    CopyJobConfiguration copyJobConfiguration = CopyJobConfiguration.of(destinationTable, sourceTable);
    Job createdJob = bigquery.create(JobInfo.of(copyJobConfiguration));
    Job remoteJob = bigquery.getJob(createdJob.getJobId());
    assertEquals(createdJob.getJobId(), remoteJob.getJobId());
    CopyJobConfiguration createdConfiguration = createdJob.getConfiguration();
    CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration();
    assertEquals(createdConfiguration.getSourceTables(), remoteConfiguration.getSourceTables());
    assertEquals(createdConfiguration.getDestinationTable(), remoteConfiguration.getDestinationTable());
    assertEquals(createdConfiguration.getCreateDisposition(), remoteConfiguration.getCreateDisposition());
    assertEquals(createdConfiguration.getWriteDisposition(), remoteConfiguration.getWriteDisposition());
    assertNotNull(remoteJob.getEtag());
    assertNotNull(remoteJob.getStatistics());
    assertNotNull(remoteJob.getStatus());
    assertEquals(createdJob.getSelfLink(), remoteJob.getSelfLink());
    assertEquals(createdJob.getUserEmail(), remoteJob.getUserEmail());
    assertTrue(createdTable.delete());
    Job completedJob = remoteJob.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(1, TimeUnit.MINUTES));
    assertNotNull(completedJob);
    assertNull(completedJob.getStatus().getError());
    assertTrue(bigquery.delete(DATASET, destinationTableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) CopyJobConfiguration(com.google.cloud.bigquery.CopyJobConfiguration) Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Job(com.google.cloud.bigquery.Job) Test(org.junit.Test)

Example 2 with TableInfo

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

the class ITBigQueryTest method testCreateViewTable.

@Test
public void testCreateViewTable() throws InterruptedException {
    String tableName = "test_create_view_table";
    TableId tableId = TableId.of(DATASET, tableName);
    ViewDefinition viewDefinition = ViewDefinition.of("SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + TABLE_ID.getTable());
    TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(tableName, createdTable.getTableId().getTable());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertTrue(remoteTable.getDefinition() instanceof ViewDefinition);
    Schema expectedSchema = Schema.newBuilder().addField(Field.newBuilder("TimestampField", Field.Type.timestamp()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("StringField", Field.Type.string()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("BooleanField", Field.Type.bool()).setMode(Field.Mode.NULLABLE).build()).build();
    assertEquals(expectedSchema, remoteTable.getDefinition().getSchema());
    QueryRequest request = QueryRequest.newBuilder("SELECT * FROM " + tableName).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.getJobId());
        Thread.sleep(1000);
    }
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(false, booleanCell.getBooleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) ViewDefinition(com.google.cloud.bigquery.ViewDefinition) Schema(com.google.cloud.bigquery.Schema) QueryResponse(com.google.cloud.bigquery.QueryResponse) TableInfo(com.google.cloud.bigquery.TableInfo) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 3 with TableInfo

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

the class ITBigQueryTest method testListTables.

@Test
public void testListTables() {
    String tableName = "test_list_tables";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    Page<Table> tables = bigquery.listTables(DATASET);
    boolean found = false;
    Iterator<Table> tableIterator = tables.getValues().iterator();
    while (tableIterator.hasNext() && !found) {
        if (tableIterator.next().getTableId().equals(createdTable.getTableId())) {
            found = true;
        }
    }
    assertTrue(found);
    assertTrue(createdTable.delete());
}
Also used : Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Test(org.junit.Test)

Example 4 with TableInfo

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

the class ITBigQueryTest method testUpdateTable.

@Test
public void testUpdateTable() {
    String tableName = "test_update_table";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    Table updatedTable = bigquery.update(tableInfo.toBuilder().setDescription("newDescription").build());
    assertEquals(DATASET, updatedTable.getTableId().getDataset());
    assertEquals(tableName, updatedTable.getTableId().getTable());
    assertEquals(TABLE_SCHEMA, updatedTable.getDefinition().getSchema());
    assertEquals("newDescription", updatedTable.getDescription());
    assertTrue(updatedTable.delete());
}
Also used : Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Test(org.junit.Test)

Example 5 with TableInfo

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

the class ITBigQueryTest method testUpdateNonExistingTable.

@Test
public void testUpdateNonExistingTable() {
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, "test_update_non_existing_table"), StandardTableDefinition.of(SIMPLE_SCHEMA));
    try {
        bigquery.update(tableInfo);
        fail("BigQueryException was expected");
    } catch (BigQueryException e) {
        BigQueryError error = e.getError();
        assertNotNull(error);
        assertEquals("notFound", error.getReason());
        assertNotNull(error.getMessage());
    }
}
Also used : BigQueryError(com.google.cloud.bigquery.BigQueryError) TableInfo(com.google.cloud.bigquery.TableInfo) BigQueryException(com.google.cloud.bigquery.BigQueryException) Test(org.junit.Test)

Aggregations

TableInfo (com.google.cloud.bigquery.TableInfo)20 Test (org.junit.Test)13 Table (com.google.cloud.bigquery.Table)11 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)10 TableId (com.google.cloud.bigquery.TableId)7 Schema (com.google.cloud.bigquery.Schema)5 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)4 CopyJobConfiguration (com.google.cloud.bigquery.CopyJobConfiguration)3 FieldValue (com.google.cloud.bigquery.FieldValue)3 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)3 Job (com.google.cloud.bigquery.Job)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 SchemaTableName (com.facebook.presto.spi.SchemaTableName)2 Field (com.google.cloud.bigquery.Field)2 QueryRequest (com.google.cloud.bigquery.QueryRequest)2 QueryResponse (com.google.cloud.bigquery.QueryResponse)2 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)1 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)1 BigQueryError (com.google.cloud.bigquery.BigQueryError)1 BigQueryException (com.google.cloud.bigquery.BigQueryException)1