Search in sources :

Example 1 with ExternalTableDefinition

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

the class ITBigQueryTest method testCreateExternalTable.

@Test
public void testCreateExternalTable() throws InterruptedException {
    String tableName = "test_create_external_table";
    TableId tableId = TableId.of(DATASET, tableName);
    ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of("gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json());
    TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
    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);
    assertTrue(remoteTable.getDefinition() instanceof ExternalTableDefinition);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
    QueryRequest request = QueryRequest.newBuilder("SELECT TimestampField, StringField, IntegerArrayField, BooleanField FROM " + DATASET + "." + 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);
    }
    long integerValue = 0;
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue integerCell = row.get(2);
        FieldValue booleanCell = row.get(3);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(integerValue, integerCell.getLongValue());
        assertEquals(false, booleanCell.getBooleanValue());
        integerValue = ~integerValue & 0x1;
        rowCount++;
    }
    assertEquals(4, rowCount);
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) ExternalTableDefinition(com.google.cloud.bigquery.ExternalTableDefinition) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) TableInfo(com.google.cloud.bigquery.TableInfo) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Aggregations

ExternalTableDefinition (com.google.cloud.bigquery.ExternalTableDefinition)1 FieldValue (com.google.cloud.bigquery.FieldValue)1 QueryRequest (com.google.cloud.bigquery.QueryRequest)1 QueryResponse (com.google.cloud.bigquery.QueryResponse)1 Table (com.google.cloud.bigquery.Table)1 TableId (com.google.cloud.bigquery.TableId)1 TableInfo (com.google.cloud.bigquery.TableInfo)1 Test (org.junit.Test)1