com.google.cloud.bigquery.InsertAllResponse

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

1. InsertDataAndQueryTable#main()

View license
public static void main(String... args) throws InterruptedException {
    // Create a service instance
    BigQuery bigquery = BigQueryOptions.defaultInstance().service();
    // Create a dataset
    String datasetId = "my_dataset_id";
    bigquery.create(DatasetInfo.builder(datasetId).build());
    TableId tableId = TableId.of(datasetId, "my_table_id");
    // Table field definition
    Field stringField = Field.of("StringField", Field.Type.string());
    // Table schema definition
    Schema schema = Schema.of(stringField);
    // Create a table
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
    bigquery.create(TableInfo.of(tableId, tableDefinition));
    // Define rows to insert
    Map<String, Object> firstRow = new HashMap<>();
    Map<String, Object> secondRow = new HashMap<>();
    firstRow.put("StringField", "value1");
    secondRow.put("StringField", "value2");
    // Create an insert request
    InsertAllRequest insertRequest = InsertAllRequest.builder(tableId).addRow(firstRow).addRow(secondRow).build();
    // Insert rows
    InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
    // Check if errors occurred
    if (insertResponse.hasErrors()) {
        System.out.println("Errors occurred while inserting rows");
    }
    // Create a query request
    QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id").maxWaitTime(60000L).pageSize(1000L).build();
    // Request query to be executed and wait for results
    QueryResponse queryResponse = bigquery.query(queryRequest);
    while (!queryResponse.jobCompleted()) {
        Thread.sleep(1000L);
        queryResponse = bigquery.getQueryResults(queryResponse.jobId());
    }
    // Read rows
    Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll();
    System.out.println("Table rows:");
    while (rowIterator.hasNext()) {
        System.out.println(rowIterator.next());
    }
}

2. ITBigQueryTest#testInsertAll()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@Test
public void testInsertAll() throws IOException {
    String tableName = "test_insert_all_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("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
    builder2.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder2.put("StringField", "stringValue");
    builder2.put("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()).addRow(builder1.build()).addRow(builder2.build()).build();
    InsertAllResponse response = bigquery.insertAll(request);
    assertFalse(response.hasErrors());
    assertEquals(0, response.insertErrors().size());
    assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
}

3. ITBigQueryTest#testInsertAllWithSuffix()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@Test
public void testInsertAllWithSuffix() throws InterruptedException {
    String tableName = "test_insert_all_with_suffix_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("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
    builder2.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder2.put("StringField", "stringValue");
    builder2.put("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()).addRow(builder1.build()).addRow(builder2.build()).templateSuffix("_suffix").build();
    InsertAllResponse response = bigquery.insertAll(request);
    assertFalse(response.hasErrors());
    assertEquals(0, response.insertErrors().size());
    String newTableName = tableName + "_suffix";
    Table suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
    // wait until the new table is created. If the table is never created the test will time-out
    while (suffixTable == null) {
        Thread.sleep(1000L);
        suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
    }
    assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
    assertTrue(suffixTable.delete());
}

4. ITBigQueryTest#testInsertAllWithErrors()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@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("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
    builder2.put("TimestampField", "invalidDate");
    builder2.put("StringField", "stringValue");
    builder2.put("IntegerField", 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", "IntegerField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    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("IntegerField", ImmutableList.of(0, 1));
    builder3.put("BooleanField", false);
    builder3.put("BytesField", BYTES_BASE64);
    InsertAllRequest request = InsertAllRequest.builder(tableInfo.tableId()).addRow(builder1.build()).addRow(builder2.build()).addRow(builder3.build()).skipInvalidRows(true).build();
    InsertAllResponse response = bigquery.insertAll(request);
    assertTrue(response.hasErrors());
    assertEquals(2, response.insertErrors().size());
    assertNotNull(response.errorsFor(1L));
    assertNotNull(response.errorsFor(2L));
    assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
}