|
24 | 24 | import com.google.api.services.bigquery.model.GetQueryResultsResponse; |
25 | 25 | import com.google.api.services.bigquery.model.TableDataInsertAllRequest; |
26 | 26 | import com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows; |
| 27 | +import com.google.api.services.bigquery.model.TableDataInsertAllResponse; |
27 | 28 | import com.google.api.services.bigquery.model.TableRow; |
28 | 29 | import com.google.api.services.bigquery.model.TableSchema; |
29 | 30 | import com.google.cloud.BaseService; |
30 | 31 | import com.google.cloud.PageImpl; |
31 | 32 | import com.google.cloud.PageImpl.NextPageFetcher; |
32 | 33 | import com.google.cloud.RetryHelper; |
| 34 | +import com.google.cloud.RetryHelper.RetryHelperException; |
33 | 35 | import com.google.cloud.RetryOption; |
34 | 36 | import com.google.cloud.Tuple; |
35 | 37 | import com.google.cloud.bigquery.InsertAllRequest.RowToInsert; |
@@ -430,16 +432,39 @@ public InsertAllResponse insertAll(InsertAllRequest request) { |
430 | 432 | requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues()); |
431 | 433 | requestPb.setSkipInvalidRows(request.skipInvalidRows()); |
432 | 434 | requestPb.setTemplateSuffix(request.getTemplateSuffix()); |
| 435 | + // Using an array of size 1 here to have a mutable boolean variable, which can be modified in |
| 436 | + // an anonymous inner class. |
| 437 | + final boolean[] allInsertIdsSet = {true}; |
433 | 438 | List<Rows> rowsPb = Lists.transform(request.getRows(), new Function<RowToInsert, Rows>() { |
434 | 439 | @Override |
435 | 440 | public Rows apply(RowToInsert rowToInsert) { |
| 441 | + allInsertIdsSet[0] &= rowToInsert.getId() != null; |
436 | 442 | return new Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent()); |
437 | 443 | } |
438 | 444 | }); |
439 | 445 | requestPb.setRows(rowsPb); |
440 | | - return InsertAllResponse.fromPb( |
441 | | - bigQueryRpc.insertAll(tableId.getProject(), tableId.getDataset(), tableId.getTable(), |
442 | | - requestPb)); |
| 446 | + |
| 447 | + TableDataInsertAllResponse responsePb; |
| 448 | + if (allInsertIdsSet[0]) { |
| 449 | + // allowing retries only if all row insertIds are set (used for deduplication) |
| 450 | + try { |
| 451 | + responsePb = runWithRetries( |
| 452 | + new Callable<TableDataInsertAllResponse>() { |
| 453 | + @Override |
| 454 | + public TableDataInsertAllResponse call() throws Exception { |
| 455 | + return bigQueryRpc.insertAll(tableId.getProject(), tableId.getDataset(), |
| 456 | + tableId.getTable(), requestPb); |
| 457 | + } |
| 458 | + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); |
| 459 | + } catch (RetryHelperException e) { |
| 460 | + throw BigQueryException.translateAndThrow(e); |
| 461 | + } |
| 462 | + } else { |
| 463 | + responsePb = bigQueryRpc.insertAll(tableId.getProject(), tableId.getDataset(), |
| 464 | + tableId.getTable(), requestPb); |
| 465 | + } |
| 466 | + |
| 467 | + return InsertAllResponse.fromPb(responsePb); |
443 | 468 | } |
444 | 469 |
|
445 | 470 | @Override |
|
0 commit comments