8000 BigQuery : Fix setProjectId for insertAll. by pmakani · Pull Request #4196 · googleapis/google-cloud-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,13 @@ public Table apply(com.google.api.services.bigquery.model.Table table) {

@Override
public InsertAllResponse insertAll(InsertAllRequest request) {
final TableId tableId = request.getTable().setProjectId(getOptions().getProjectId());
final TableId tableId =
request
.getTable()
.setProjectId(
Strings.isNullOrEmpty(request.getTable().getProject())
? getOptions().getProjectId()
: request.getTable().getProject());
final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest();
requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues());
requestPb.setSkipInvalidRows(request.skipInvalidRows());
Expand Down
6D39
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,57 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}

@Test
public void testInsertAllWithProjectInTable() {
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("project-different-from-option", 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("project-different-from-option", DATASET, TABLE, requestPb))
.andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
BigQueryOptions bigQueryOptions =
createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock);
bigquery = bigQueryOptions.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());
}

@Test
public void testListTableData() {
EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))
Expand Down
0