Search in sources :

Example 11 with TableInfo

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

the class ITBigQueryTest method testInsertAllWithErrors.

@Test
public void testInsertAllWithErrors() {
    String tableName = "test_insert_all_with_errors_table";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    assertNotNull(bigquery.create(tableInfo));
    ImmutableMap.Builder<String, Object> builder1 = ImmutableMap.builder();
    builder1.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder1.put("StringField", "stringValue");
    builder1.put("IntegerArrayField", ImmutableList.of(0, 1));
    builder1.put("BooleanField", false);
    builder1.put("BytesField", BYTES_BASE64);
    builder1.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    builder1.put("IntegerField", 5);
    builder1.put("FloatField", 1.2);
    ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
    builder2.put("TimestampField", "invalidDate");
    builder2.put("StringField", "stringValue");
    builder2.put("IntegerArrayField", ImmutableList.of(0, 1));
    builder2.put("BooleanField", false);
    builder2.put("BytesField", BYTES_BASE64);
    builder2.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    builder2.put("IntegerField", 5);
    builder2.put("FloatField", 1.2);
    ImmutableMap.Builder<String, Object> builder3 = ImmutableMap.builder();
    builder3.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder3.put("StringField", "stringValue");
    builder3.put("IntegerArrayField", ImmutableList.of(0, 1));
    builder3.put("BooleanField", false);
    builder3.put("BytesField", BYTES_BASE64);
    InsertAllRequest request = InsertAllRequest.newBuilder(tableInfo.getTableId()).addRow(builder1.build()).addRow(builder2.build()).addRow(builder3.build()).setSkipInvalidRows(true).build();
    InsertAllResponse response = bigquery.insertAll(request);
    assertTrue(response.hasErrors());
    assertEquals(2, response.getInsertErrors().size());
    assertNotNull(response.getErrorsFor(1L));
    assertNotNull(response.getErrorsFor(2L));
    assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
}
Also used : TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 12 with TableInfo

use of com.google.cloud.bigquery.TableInfo 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)

Example 13 with TableInfo

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

the class BigQuerySnippets method createTable.

/**
   * Example of creating a table.
   */
// [TARGET create(TableInfo, TableOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "string_field"]
public Table createTable(String datasetName, String tableName, String fieldName) {
    // [START createTable]
    TableId tableId = TableId.of(datasetName, tableName);
    // Table field definition
    Field field = Field.of(fieldName, Field.Type.string());
    // Table schema definition
    Schema schema = Schema.of(field);
    TableDefinition tableDefinition = StandardTableDefinition.of(schema);
    TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
    Table table = bigquery.create(tableInfo);
    // [END createTable]
    return table;
}
Also used : TableId(com.google.cloud.bigquery.TableId) Field(com.google.cloud.bigquery.Field) Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) TableDefinition(com.google.cloud.bigquery.TableDefinition) TableInfo(com.google.cloud.bigquery.TableInfo)

Example 14 with TableInfo

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

the class ITBigQueryTest method testUpdateTableWithSelectedFields.

@Test
public void testUpdateTableWithSelectedFields() {
    String tableName = "test_update_with_selected_fields_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("newDescr").build(), TableOption.fields(TableField.DESCRIPTION));
    assertTrue(updatedTable.getDefinition() instanceof StandardTableDefinition);
    assertEquals(DATASET, updatedTable.getTableId().getDataset());
    assertEquals(tableName, updatedTable.getTableId().getTable());
    assertEquals("newDescr", updatedTable.getDescription());
    assertNull(updatedTable.getDefinition().getSchema());
    assertNull(updatedTable.getLastModifiedTime());
    assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumBytes());
    assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumRows());
    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 15 with TableInfo

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

the class ITBigQueryTest method testCopyJob.

@Test
public void testCopyJob() throws InterruptedException, TimeoutException {
    String sourceTableName = "test_copy_job_source_table";
    String destinationTableName = "test_copy_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 configuration = CopyJobConfiguration.of(destinationTable, sourceTable);
    Job remoteJob = bigquery.create(JobInfo.of(configuration));
    remoteJob = remoteJob.waitFor();
    assertNull(remoteJob.getStatus().getError());
    Table remoteTable = bigquery.getTable(DATASET, destinationTableName);
    assertNotNull(remoteTable);
    assertEquals(destinationTable.getDataset(), remoteTable.getTableId().getDataset());
    assertEquals(destinationTableName, remoteTable.getTableId().getTable());
    assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
    assertTrue(createdTable.delete());
    assertTrue(remoteTable.delete());
}
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)

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