Search in sources :

Example 6 with Schema

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

the class ITBigQuerySnippets method testInsertAllAndListTableData.

@Test
public void testInsertAllAndListTableData() throws IOException, InterruptedException {
    String tableName = "test_insert_all_and_list_table_data";
    String fieldName1 = "booleanField";
    String fieldName2 = "bytesField";
    String fieldName3 = "recordField";
    String fieldName4 = "stringField";
    TableId tableId = TableId.of(DATASET, tableName);
    Schema schema = Schema.of(Field.of(fieldName1, Type.bool()), Field.of(fieldName2, Type.bytes()), Field.of(fieldName3, Type.record(Field.of(fieldName4, Type.string()))));
    TableInfo table = TableInfo.of(tableId, StandardTableDefinition.of(schema));
    assertNotNull(bigquery.create(table));
    InsertAllResponse response = bigquerySnippets.insertAll(DATASET, tableName);
    assertFalse(response.hasErrors());
    assertTrue(response.getInsertErrors().isEmpty());
    Page<List<FieldValue>> listPage = bigquerySnippets.listTableDataFromId(DATASET, tableName);
    while (Iterators.size(listPage.iterateAll().iterator()) < 1) {
        Thread.sleep(500);
        listPage = bigquerySnippets.listTableDataFromId(DATASET, tableName);
    }
    List<FieldValue> row = listPage.getValues().iterator().next();
    assertEquals(true, row.get(0).getBooleanValue());
    assertArrayEquals(new byte[] { 0xA, 0xD, 0xD, 0xE, 0xD }, row.get(1).getBytesValue());
    assertEquals("Hello, World!", row.get(2).getRecordValue().get(0).getStringValue());
    assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) Schema(com.google.cloud.bigquery.Schema) TableInfo(com.google.cloud.bigquery.TableInfo) List(java.util.List) FieldValue(com.google.cloud.bigquery.FieldValue) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Test(org.junit.Test)

Aggregations

Schema (com.google.cloud.bigquery.Schema)6 TableId (com.google.cloud.bigquery.TableId)5 Table (com.google.cloud.bigquery.Table)4 Field (com.google.cloud.bigquery.Field)3 FieldValue (com.google.cloud.bigquery.FieldValue)3 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 BigQuery (com.google.cloud.bigquery.BigQuery)2 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)2 QueryRequest (com.google.cloud.bigquery.QueryRequest)2 QueryResponse (com.google.cloud.bigquery.QueryResponse)2 Test (org.junit.Test)2 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 Job (com.google.cloud.bigquery.Job)1 TableDefinition (com.google.cloud.bigquery.TableDefinition)1 ViewDefinition (com.google.cloud.bigquery.ViewDefinition)1 HashMap (java.util.HashMap)1 List (java.util.List)1