本文介绍如何通过 Java SDK 在表格存储的数据表中写入单行数据。
前提条件
方法说明
public PutRowResponse putRow(PutRowRequest putRowRequest) throws TableStoreException, ClientException
示例代码
以下示例代码在 test_table 表中写入一行数据,该行数据的主键值为 row1。
public static void putRowExample(SyncClient client) {
// 构造主键
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// 构造写入行数据
RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey);
// 调用 putRow 方法写入行数据
PutRowRequest putRowRequest = new PutRowRequest(rowPutChange);
PutRowResponse putRowResponse = client.putRow(putRowRequest);
// 返回结果处理
System.out.println("* RequestId: " + putRowResponse.getRequestId());
System.out.println("* Read CU Cost: " + putRowResponse.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit());
System.out.println("* Write CU Cost: " + putRowResponse.getConsumedCapacity().getCapacityUnit().getWriteCapacityUnit());
}
添加属性列。
rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));
指定数据版本号,您可以为每个属性列指定单独的版本号。
// 将当前时间戳作为版本号 rowPutChange.addColumn("col1", ColumnValue.fromString("val1"), System.currentTimeMillis());
相关文档
该文章对您有帮助吗?