Skip to content

Commit

Permalink
fix: update TableInsertRows.java (#2999)
Browse files Browse the repository at this point in the history
* fix: update TableInsertRows.java

Make the example set row id in `addRow`. If row id is missed, it disable the retry b/280865468, which I believe an unexpected behavior to users.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
baeminbo and gcf-owl-bot[bot] committed Nov 28, 2023
1 parent 19b7c3a commit ff4a086
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -53,7 +53,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.26.0')
implementation platform('com.google.cloud:libraries-bom:26.27.0')
implementation 'com.google.cloud:google-cloud-bigquery'
```
Expand Down
Expand Up @@ -39,12 +39,13 @@ public static void main(String[] args) {
Map<String, Object> rowContent = new HashMap<>();
rowContent.put("booleanField", true);
rowContent.put("numericField", "3.14");

tableInsertRows(datasetName, tableName, rowContent);
// TODO(developer): Replace the row id with a unique value for each row.
String rowId = "ROW_ID";
tableInsertRows(datasetName, tableName, rowId, rowContent);
}

public static void tableInsertRows(
String datasetName, String tableName, Map<String, Object> rowContent) {
String datasetName, String tableName, String rowId, Map<String, Object> rowContent) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
Expand All @@ -58,9 +59,8 @@ public static void tableInsertRows(
bigquery.insertAll(
InsertAllRequest.newBuilder(tableId)
// More rows can be added in the same RPC by invoking .addRow() on the builder.
// You can also supply optional unique row keys to support de-duplication
// scenarios.
.addRow(rowContent)
// You can omit the unique row ids to disable de-duplication.
.addRow(rowId, rowContent)
.build());

if (response.hasErrors()) {
Expand Down
Expand Up @@ -54,6 +54,8 @@ public static void tableInsertRowsWithoutRowIds(String datasetName, String table
InsertAllResponse response =
bigquery.insertAll(
InsertAllRequest.newBuilder(TableId.of(datasetName, tableName))
// No row ids disable de-duplication, and also disable the retries in the Java
// library.
.setRows(
ImmutableList.of(
InsertAllRequest.RowToInsert.of(rowContent1),
Expand Down
Expand Up @@ -88,8 +88,9 @@ public void testTableInsertRows() {
Map<String, Object> rowContent = new HashMap<>();
rowContent.put("booleanField", true);
rowContent.put("numericField", "3.14");
String rowId = "ROW_ID";
// Testing
TableInsertRows.tableInsertRows(BIGQUERY_DATASET_NAME, tableName, rowContent);
TableInsertRows.tableInsertRows(BIGQUERY_DATASET_NAME, tableName, rowId, rowContent);
assertThat(bout.toString()).contains("Rows successfully inserted into table");
}
}

0 comments on commit ff4a086

Please sign in to comment.