This topic describes how to write a single row of data to a Tablestore table by using Tablestore SDK for Java.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Method
public PutRowResponse putRow(PutRowRequest putRowRequest) throws TableStoreException, ClientExceptionSample code
The following sample code writes a row of data to the test_table table. The primary key value of this row is row1.
public static void putRowExample(SyncClient client) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Construct the row data to write.
RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey);
// Call the putRow method to write the row data.
PutRowRequest putRowRequest = new PutRowRequest(rowPutChange);
PutRowResponse putRowResponse = client.putRow(putRowRequest);
// Return the result.
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());
}Add attribute columns.
rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));Specify the version number. You can specify a separate version number for each attribute column.
// Use the current timestamp as the version number. rowPutChange.addColumn("col1", ColumnValue.fromString("val1"), System.currentTimeMillis());