use of com.google.cloud.bigquery.InsertAllRequest.RowToInsert in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImplTest method testInsertAll.
@Test
public void testInsertAll() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
List<RowToInsert> rows = ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2));
InsertAllRequest request = InsertAllRequest.newBuilder(TABLE_ID).setRows(rows).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix").build();
TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows(Lists.transform(rows, new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {
@Override
public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
}
})).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix");
TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors(ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb)).andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
InsertAllResponse response = bigquery.insertAll(request);
assertNotNull(response.getErrorsFor(0L));
assertNull(response.getErrorsFor(1L));
assertEquals(1, response.getErrorsFor(0L).size());
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}
use of com.google.cloud.bigquery.InsertAllRequest.RowToInsert 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.InsertAllRequest.RowToInsert in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImplTest method testInsertAllWithProject.
@Test
public void testInsertAllWithProject() {
Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
List<RowToInsert> rows = ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2));
TableId tableId = TableId.of(OTHER_PROJECT, DATASET, TABLE);
InsertAllRequest request = InsertAllRequest.newBuilder(tableId).setRows(rows).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix").build();
TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows(Lists.transform(rows, new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {
@Override
public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
}
})).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix");
TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors(ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
EasyMock.expect(bigqueryRpcMock.insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb)).andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
InsertAllResponse response = bigquery.insertAll(request);
assertNotNull(response.getErrorsFor(0L));
assertNull(response.getErrorsFor(1L));
assertEquals(1, response.getErrorsFor(0L).size());
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}
use of com.google.cloud.bigquery.InsertAllRequest.RowToInsert in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImpl method insertAll.
@Override
public InsertAllResponse insertAll(InsertAllRequest request) {
final TableId tableId = request.getTable().setProjectId(getOptions().getProjectId());
final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest();
requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues());
requestPb.setSkipInvalidRows(request.skipInvalidRows());
requestPb.setTemplateSuffix(request.getTemplateSuffix());
List<Rows> rowsPb = Lists.transform(request.getRows(), new Function<RowToInsert, Rows>() {
@Override
public Rows apply(RowToInsert rowToInsert) {
return new Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
}
});
requestPb.setRows(rowsPb);
return InsertAllResponse.fromPb(bigQueryRpc.insertAll(tableId.getProject(), tableId.getDataset(), tableId.getTable(), requestPb));
}
use of com.google.cloud.bigquery.InsertAllRequest.RowToInsert in project google-cloud-java by GoogleCloudPlatform.
the class TableSnippets method insert.
/**
* Example of inserting rows into the table.
*/
// [TARGET insert(Iterable)]
// [VARIABLE "rowId1"]
// [VARIABLE "rowId2"]
public InsertAllResponse insert(String rowId1, String rowId2) {
// [START insert]
List<RowToInsert> rows = new ArrayList<>();
Map<String, Object> row1 = new HashMap<>();
row1.put("stringField", "value1");
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);
// [END insert]
return response;
}
Aggregations