本文介紹如何通過 Java SDK 在Table Store的資料表中寫入單行資料。
前提條件
方法說明
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());