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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations