You can specify a primary key column that is not the partition key as the auto-increment primary key column. If you write data to a table that contains an auto-increment primary key column, you do not need to specify values for the auto-increment primary key column. Tablestore automatically generates values for the auto-increment primary key column. The values in the auto-increment primary key column are unique and increase monotonically within a partition that shares the same partition key value.
Characteristics
The auto-increment primary key column feature has the following characteristics:
- The values of an auto-increment primary key column are unique and increase monotonically but not always continuously within a partition that shares the same partition key value.
- The values of an auto-increment primary key column are 64-bit signed integers of the LONG data type.
- You can create an auto-increment primary key column for a data table. An instance can include data tables that contain auto-increment primary key columns and tables that do not contain auto-increment primary key columns.
Limits
The auto-increment primary key column feature has the following limits:
- You can specify at most one auto-increment primary key column for a data table. You cannot specify the partition key as the auto-increment primary key column.
- You can specify an auto-increment primary key column only when you create a data table. You cannot specify an auto-increment primary key column for an existing data table.
- You can specify a primary key column only of the INTEGER type as the auto-increment primary key column. Each value generated for an auto-increment primary key column is a 64-bit signed integer of the LONG data type.
- You cannot specify an attribute column as the auto-increment primary key column.
API operations
API operation | Description |
---|---|
CreateTable | When you create a data table, you can specify a primary key column that is not the partition key as the auto-increment primary key column. If you specify the partition key as the auto-increment primary key column, values in the column cannot be automatically generated. |
UpdateTable | After a data table is created, you cannot call the UpdateTable operation to specify a primary key column as the auto-increment primary key column. |
PutRow | If you write data to a data table that contains an auto-increment primary key column, you do not need to specify values for the auto-increment primary key column. Tablestore automatically generates values for the auto-increment primary key column. You can set ReturnType to RT_PK to obtain values of all primary key columns and use the values in GetRow to query data. Note If you want to update an existing row, call the GetRange operation to obtain the primary key information about the row before you update the data. |
UpdateRow | |
BatchWriteRow | |
GetRow | You must use values of all primary key columns when you call GetRow. To obtain values of all primary key columns, you can set ReturnType to RT_PK in PutRow, UpdateRow, or BatchWriteRow. |
BatchGetRow |
Examples
You can use the auto-increment primary key column feature when you call the CreateTable, PutRow, UpdateRow, or BatchWriteRow operation.
- Create a table
To create an auto-increment primary key column when you create a table, you need to only set the attribute of the primary key column to AUTO_INCREMENT.
private static void createTable(SyncClient client) { TableMeta tableMeta = new TableMeta("table_name"); // Create the first primary key column. The first primary key column is the partition key. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_1", PrimaryKeyType.STRING)); // Create the second primary key column whose type is INTEGER, and specify the second primary key column as the auto-increment primary key column by setting the attribute of the second primary key column to AUTO_INCREMENT. tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT)); int timeToLive = -1; // Specify that data never expires. int maxVersions = 1; // Specify that only one version of data is retained. TableOptions tableOptions = new TableOptions(timeToLive, maxVersions); CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions); client.createTable(request); }
- Write data to a table
When you write data to a table, you do not need to specify values for the auto-increment primary key column. You need to only set the values of the auto-increment primary key column to AUTO_INCREMENT.
private static void putRow(SyncClient client, String receive_id) { // Construct the primary key. PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder(); // Set the values in the first primary key column to the first four digits of md5(receive_id). primaryKeyBuilder.addPrimaryKeyColumn("PK_1", PrimaryKeyValue.fromString("Hangzhou"); // The second primary key column is an auto-increment primary key column, and you do not need to specify values for the second primary key column. You need to only set the values of the auto-increment primary key column to AUTO_INCREMENT. Tablestore automatically generates values for the auto-increment primary key column. primaryKeyBuilder.addPrimaryKeyColumn("PK_2", PrimaryKeyValue.AUTO_INCREMENT); PrimaryKey primaryKey = primaryKeyBuilder.build(); RowPutChange rowPutChange = new RowPutChange("table_name", primaryKey); // Set ReturnType to RT_PK to include the primary key value in the response. If you do not set ReturnType to RT_PK, the primary key value is not included in the response. rowPutChange.setReturnType(ReturnType.RT_PK); // Add attribute columns. rowPutChange.addColumn(new Column("content", ColumnValue.fromString(content))); // Write data to the table. PutRowResponse response = client.putRow(new PutRowRequest(rowPutChange)); // Display the returned primary key value. Row returnRow = response.getRow(); if (returnRow != null) { System.out.println("PrimaryKey:" + returnRow.getPrimaryKey().toString()); } // Display the consumed capacity units (CUs). CapacityUnit cu = response.getConsumedCapacity().getCapacityUnit(); System.out.println("Read CapacityUnit:" + cu.getReadCapacityUnit()); System.out.println("Write CapacityUnit:" + cu.getWriteCapacityUnit()); }
Use Tablestore SDKs
You can use the following Tablestore SDKs to implement the auto-increment primary key column feature:
- Tablestore SDK for Java: Configure an auto-increment primary key column
- Tablestore SDK for Go: Configure an auto-increment primary key column
- Tablestore SDK for Python: Configure an auto-increment primary key column
- Tablestore SDK for Node.js: Configure an auto-increment primary key column
- Tablestore SDK for .NET: Configure an auto-increment primary key column
- Tablestore SDK for PHP: Configure an auto-increment primary key column
Billing
When you use the auto-increment primary key column feature, the existing billing rules are not affected. The primary key values that are returned do not consume additional read CUs.