Search in sources :

Example 1 with InsertAllResponse

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

the class ITTableSnippets method testInsert.

@Test
public void testInsert() throws InterruptedException {
    InsertAllResponse response = tableSnippets.insert("row1", "row2");
    assertFalse(response.hasErrors());
    verifyTestRows(table);
}
Also used : InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Test(org.junit.Test)

Example 2 with InsertAllResponse

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

the class ITTableSnippets method testInsertParams.

@Test
public void testInsertParams() throws InterruptedException {
    InsertAllResponse response = tableSnippets.insertWithParams("row1", "row2");
    assertTrue(response.hasErrors());
    List<List<FieldValue>> rows = ImmutableList.copyOf(tableSnippets.list().getValues());
    while (rows.isEmpty()) {
        Thread.sleep(500);
        rows = ImmutableList.copyOf(tableSnippets.list().getValues());
    }
    Set<List<?>> values = FluentIterable.from(rows).transform(new Function<List<FieldValue>, List<?>>() {

        @Override
        public List<?> apply(List<FieldValue> row) {
            return ImmutableList.of(row.get(0).getStringValue(), row.get(1).getBooleanValue());
        }
    }).toSet();
    assertEquals(ImmutableSet.of(ROW2), values);
}
Also used : Function(com.google.common.base.Function) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) FieldValue(com.google.cloud.bigquery.FieldValue) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Test(org.junit.Test)

Example 3 with InsertAllResponse

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

the class TableSnippets method insertWithParams.

/**
   * Example of inserting rows into the table, ignoring invalid rows.
   */
// [TARGET insert(Iterable, boolean, boolean)]
// [VARIABLE "rowId1"]
// [VARIABLE "rowId2"]
public InsertAllResponse insertWithParams(String rowId1, String rowId2) {
    // [START insertWithParams]
    List<RowToInsert> rows = new ArrayList<>();
    Map<String, Object> row1 = new HashMap<>();
    row1.put("stringField", 1);
    row1.put("booleanField", true);
    Map<String, Object> row2 = new HashMap<>();
    row2.put("stringField", "value2");
    row2.put("booleanField", false);
    rows.add(RowToInsert.of(rowId1, row1));
    rows.add(RowToInsert.of(rowId2, row2));
    InsertAllResponse response = table.insert(rows, true, true);
    // [END insertWithParams]
    return response;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RowToInsert(com.google.cloud.bigquery.InsertAllRequest.RowToInsert) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse)

Example 4 with InsertAllResponse

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

the class BigQuerySnippets method insertAll.

/**
   * Example of inserting rows into a table without running a load job.
   */
// [TARGET insertAll(InsertAllRequest)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
public InsertAllResponse insertAll(String datasetName, String tableName) {
    // [START insertAll]
    TableId tableId = TableId.of(datasetName, tableName);
    // Values of the row to insert
    Map<String, Object> rowContent = new HashMap<>();
    rowContent.put("booleanField", true);
    // Bytes are passed in base64
    // 0xA, 0xD, 0xD, 0xE, 0xD in base64
    rowContent.put("bytesField", "Cg0NDg0=");
    // Records are passed as a map
    Map<String, Object> recordsContent = new HashMap<>();
    recordsContent.put("stringField", "Hello, World!");
    rowContent.put("recordField", recordsContent);
    InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId).addRow("rowId", rowContent).build());
    if (response.hasErrors()) {
        // If any of the insertions failed, this lets you inspect the errors
        for (Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
        // inspect row error
        }
    }
    // [END insertAll]
    return response;
}
Also used : TableId(com.google.cloud.bigquery.TableId) HashMap(java.util.HashMap) List(java.util.List) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse)

Example 5 with InsertAllResponse

use of com.google.cloud.bigquery.InsertAllResponse in project beam by apache.

the class BigQueryClient method insertAll.

/**
 * Inserts multiple rows of the same schema to a BigQuery table.
 */
public void insertAll(Collection<Map<String, ?>> rows, String table) {
    TableId tableId = TableId.of(projectId, dataset, table);
    InsertAllRequest.Builder builder = InsertAllRequest.newBuilder(tableId);
    for (Map<String, ?> row : rows) {
        builder.addRow(row);
    }
    InsertAllResponse response = client.insertAll(builder.build());
    handleBigQueryResponseExceptions(response);
}
Also used : TableId(com.google.cloud.bigquery.TableId) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse)

Aggregations

InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)13 Test (org.junit.Test)7 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)6 List (java.util.List)5 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)4 TableId (com.google.cloud.bigquery.TableId)4 TableInfo (com.google.cloud.bigquery.TableInfo)4 HashMap (java.util.HashMap)4 FieldValue (com.google.cloud.bigquery.FieldValue)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ArrayList (java.util.ArrayList)3 RowToInsert (com.google.cloud.bigquery.InsertAllRequest.RowToInsert)2 Schema (com.google.cloud.bigquery.Schema)2 ImmutableList (com.google.common.collect.ImmutableList)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BigQuery (com.google.cloud.bigquery.BigQuery)1 Field (com.google.cloud.bigquery.Field)1 QueryRequest (com.google.cloud.bigquery.QueryRequest)1 QueryResponse (com.google.cloud.bigquery.QueryResponse)1