com.google.cloud.bigquery.ViewDefinition

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

1. ITBigQueryTest#testCreateViewTable()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@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.table());
    TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
    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);
    assertEquals(createdTable.tableId(), remoteTable.tableId());
    assertTrue(remoteTable.definition() instanceof ViewDefinition);
    Schema expectedSchema = Schema.builder().addField(Field.builder("TimestampField", Field.Type.timestamp()).mode(Field.Mode.NULLABLE).build()).addField(Field.builder("StringField", Field.Type.string()).mode(Field.Mode.NULLABLE).build()).addField(Field.builder("BooleanField", Field.Type.bool()).mode(Field.Mode.NULLABLE).build()).build();
    assertEquals(expectedSchema, remoteTable.definition().schema());
    QueryRequest request = QueryRequest.builder("SELECT * FROM " + 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);
    }
    int rowCount = 0;
    for (List<FieldValue> row : response.result().values()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
        assertEquals(1408452095220000L, timestampCell.timestampValue());
        assertEquals("stringValue", stringCell.stringValue());
        assertEquals(false, booleanCell.booleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(remoteTable.delete());
}