Table Store Java SDK 在單次請求內對一張或多張資料表批量執行寫入、修改和刪除操作,按行獨立返回結果。適用於大量匯入、跨表狀態同步等情境。
前提條件
已安裝Table Store Java SDK並完成用戶端初始化。
功能說明
public BatchWriteRowResponse batchWriteRow(BatchWriteRowRequest batchWriteRowRequest) throws TableStoreException, ClientException在單次請求內對一張或多張資料表批量執行多種行操作。BatchWriteRowRequest 通過 addRowChange 添加 RowChange。RowChange 有以下三種實作類別:
RowPutChange:寫入(覆蓋)整行。RowUpdateChange:修改屬性列,可新增、修改或刪除指定屬性列。RowDeleteChange:刪除整行。
服務端按行獨立處理,單行失敗不影響其他行。調用 isAllSucceed 判斷整體是否全部成功,調用 getFailedRows 擷取失敗行的詳情。
若請求中部分操作存在參數錯誤,服務端將拋出參數錯誤異常,整批操作均不執行。
以下樣本展示最簡用法:向資料表 batch_write_demo 寫入一行資料(主鍵為 row1),並列印整體執行狀態。
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"))
.build();
RowPutChange rowPutChange = new RowPutChange(tableName, primaryKey);
rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));
request.addRowChange(rowPutChange);
BatchWriteRowResponse response = client.batchWriteRow(request);
System.out.println("RequestId: " + response.getRequestId());
System.out.println("All Succeeded: " + response.isAllSucceed());
if (!response.isAllSucceed()) {
for (BatchWriteRowResponse.RowResult fail : response.getFailedRows()) {
System.out.println("Failed: table=" + fail.getTableName()
+ " index=" + fail.getIndex()
+ " error=" + fail.getError());
}
}參數說明
名稱 | 類型 | 說明 |
BatchWriteRowRequest(必選) | class | 批量請求容器,通過 |
RowPutChange | class | 寫入(覆蓋)整行。需指定資料表名和主鍵。屬性列通過 |
RowUpdateChange | class | 修改屬性列。通過 |
RowDeleteChange | class | 刪除整行。需指定資料表名和主鍵,詳情請參見刪除單行資料。 |
情境樣本
批次更新行資料
通過 RowUpdateChange 批量修改屬性列。
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row_for_update"))
.build();
RowUpdateChange rowUpdateChange = new RowUpdateChange(tableName, primaryKey);
// 修改或新增屬性列
rowUpdateChange.put("col1", ColumnValue.fromString("new_val1"));
rowUpdateChange.put("col2", ColumnValue.fromLong(100));
// 刪除整個屬性列
rowUpdateChange.deleteColumns("obsolete_col");
request.addRowChange(rowUpdateChange);
client.batchWriteRow(request);大量刪除行資料
通過 RowDeleteChange 大量刪除整行。
String tableName = "batch_write_demo";
BatchWriteRowRequest request = new BatchWriteRowRequest();
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row_for_delete"))
.build();
RowDeleteChange rowDeleteChange = new RowDeleteChange(tableName, primaryKey);
request.addRowChange(rowDeleteChange);
client.batchWriteRow(request);跨表混合多種操作
單次請求內對多張資料表混合執行寫入、修改、刪除等操作,結果按行獨立返回。
String tableA = "batch_write_demo";
String tableB = "batch_write_demo_2";
BatchWriteRowRequest request = new BatchWriteRowRequest();
// 表 A 寫入新行
PrimaryKey pkA = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowA_new"))
.build();
RowPutChange putA = new RowPutChange(tableA, pkA);
putA.addColumn("col1", ColumnValue.fromString("valA"));
request.addRowChange(putA);
// 表 B 更新已有行
PrimaryKey pkB = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowB_existing"))
.build();
RowUpdateChange updateB = new RowUpdateChange(tableB, pkB);
updateB.put("status", ColumnValue.fromString("done"));
request.addRowChange(updateB);
// 表 A 刪除一行
PrimaryKey pkADel = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("rowA_obsolete"))
.build();
RowDeleteChange deleteA = new RowDeleteChange(tableA, pkADel);
request.addRowChange(deleteA);
BatchWriteRowResponse response = client.batchWriteRow(request);
if (!response.isAllSucceed()) {
System.out.println("Failed rows: " + response.getFailedRows().size());
for (BatchWriteRowResponse.RowResult fail : response.getFailedRows()) {
System.out.println(" - table=" + fail.getTableName()
+ " index=" + fail.getIndex()
+ " error=" + fail.getError().getMessage());
}
}