com.google.cloud.bigquery.ExternalTableDefinition

Here are the examples of the java api class com.google.cloud.bigquery.ExternalTableDefinition taken from open source projects.

1. ITBigQueryTest#testCreateExternalTable()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@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.tableId().dataset());
    assertEquals(tableName, createdTable.tableId().table());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertTrue(remoteTable.definition() instanceof ExternalTableDefinition);
    assertEquals(createdTable.tableId(), remoteTable.tableId());
    assertEquals(TABLE_SCHEMA, remoteTable.definition().schema());
    QueryRequest request = QueryRequest.builder("SELECT TimestampField, StringField, IntegerField, BooleanField FROM " + DATASET + "." + tableName).defaultDataset(DatasetId.of(DATASET)).maxWaitTime(60000L).pageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.jobId());
        Thread.sleep(1000);
    }
    long integerValue = 0;
    int rowCount = 0;
    for (List<FieldValue> row : response.result().values()) {
        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.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
        assertEquals(1408452095220000L, timestampCell.timestampValue());
        assertEquals("stringValue", stringCell.stringValue());
        assertEquals(integerValue, integerCell.longValue());
        assertEquals(false, booleanCell.booleanValue());
        integerValue = ~integerValue & 0x1;
        rowCount++;
    }
    assertEquals(4, rowCount);
    assertTrue(remoteTable.delete());
}