This topic describes how to configure an auto-increment primary key column for a data table by using Tablestore SDK for Java. This topic also explains how to write data to the auto-increment primary key column and retrieve the values of the auto-increment primary key column.
Usage notes
The auto-incremental primary key column feature is supported by Tablestore SDK for Java V4.2.0 and later.
The values generated for an auto-increment primary key column are unique and increase monotonically within a partition. However, the values may not be consecutive.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
Configure an auto-increment primary key column
When you create a data table, you can specify a primary key column that is not the partition key as an auto-increment primary key column. You cannot specify an auto-increment primary key column for an existing data table.
A primary key column only of the Integer type can be specified as an auto-increment primary key column. A data table can have at most one auto-increment primary key column. The values generated for an auto-increment primary key column are 64-bit signed long integers.
Sample code
The following sample code provides an example on how to create a data table named test_table. In this example, the data table contains a partition key named id and an auto-increment primary key column named incr.
public static void createTableTest(SyncClient client) {
TableMeta tableMeta = new TableMeta("test_table");
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("id", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("incr", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
TableOptions tableOptions = new TableOptions();
tableOptions.setMaxVersions(1);
tableOptions.setTimeToLive(-1);
CreateTableRequest createTableRequest = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(createTableRequest);
}Write data
When you write data to a data table that contains an auto-increment primary key column, you only need to set the value of the auto-increment primary key column to a placeholder. If you want to obtain the generated value of the auto-increment primary key column for subsequent query and update operations, set the return type of rowPutChange to RT_PK.
Sample code
The following sample code provides an example on how to write a row of data to the test_table table, and obtain and print the primary key information of the row:
public static void putRowTest(SyncClient client) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
// Set the value of the auto-increment primary key column to a placeholder.
primaryKeyBuilder.addPrimaryKeyColumn("incr", PrimaryKeyValue.AUTO_INCREMENT);
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Construct the row data to write.
RowPutChange rowPutChange = new RowPutChange("test_table", primaryKey);
rowPutChange.addColumn("col1", ColumnValue.fromString("val1"));
// Set the return type to RT_PK to return the primary key information of the written row.
rowPutChange.setReturnType(ReturnType.RT_PK);
// Call the putRow method to write the row of data.
PutRowRequest putRowRequest = new PutRowRequest();
putRowRequest.setRowChange(rowPutChange);
PutRowResponse putRowResponse = client.putRow(putRowRequest);
// Display the request Id and read/write CU consumption.
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());
// Obtain and print the returned primary key information. If the return type is not set to RT_PK, the primary key information is not returned.
Row row = putRowResponse.getRow();
if(row != null)
System.out.println(row.getPrimaryKey().toString());
}