全部产品
Search
文档中心

表格存储:批量数据操作

更新时间:Jun 25, 2026

表格存储 Java SDK 在单次请求内对一张或多张数据表批量执行写入、修改和删除操作,按行独立返回结果。适用于批量导入、跨表状态同步等场景。

前提条件

已安装表格存储 Java SDK并完成客户端初始化。

功能说明

public BatchWriteRowResponse batchWriteRow(BatchWriteRowRequest batchWriteRowRequest) throws TableStoreException, ClientException

在单次请求内对一张或多张数据表批量执行多种行操作。BatchWriteRowRequest 通过 addRowChange 添加 RowChangeRowChange 有以下三种实现类:

  • 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

批量请求容器,通过 addRowChange(RowChange) 添加待执行的行操作。同一请求支持跨表混合多种 RowChange

RowPutChange

class

写入(覆盖)整行。需指定数据表名和主键。属性列通过 addColumn 添加,行为与单行写入接口一致,详情请参见写入单行数据

RowUpdateChange

class

修改属性列。通过 put 新增或修改属性列,通过 deleteColumns 删除整个属性列,通过 deleteColumn 删除指定版本,详情请参见更新单行数据

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());
    }
}