Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update TableInsertRows.java #2999

Merged
merged 2 commits into from Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}
}