The auto-increment function of primary key column is a new feature launched by Table Store, and is available for Java SDK v4.2.0 and later versions.

The auto-increment function of primary key column means that if you specify a primary key column as the auto-increment column, Table Store generates a new value automatically in this column when you write data, and the generated value is the maximum value of this column under the same partition key. This feature mainly applies to system designing scenarios that need an auto-increment function applied to the primary key column, such as the item ID on e-commerce websites, user ID of large websites, post ID in forums and message ID in chat tools.


  • Table Store currently supports multiple primary keys, however, the first primary key is the partition key, to which auto-increment primary keys are not allowed.

  • Except for the partition key, any of the other primary keys can be set to the auto-increment column.

  • Because the automatic incrementation is generated on the basis of the partition key, the primary key column auto-increment function is at the partition key level.

  • Only one primary key column is allowed to be set to the auto-increment column in each table for the moment.

  • The automatically generated auto-increment column is of the 64-bit signed long integer type.


The primary key column auto-increment feature mainly involves two types of interfaces: creating a table and writing data.

  • Create a table

    When creating a table, you only need to set the attribute of the auto-increment primary key to PrimaryKeyOption.AUTO_INCREMENT.

    Related interface: CreateTable

    private static void createTable(SyncClient client) {
            TableMeta tableMeta = new TableMeta("table_name");
            // The first column is the partition key
            tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_1", PrimaryKeyType.STRING));
            // The second column is the auto-increment column with INTEGER type and the attribute is AUTO_INCREMENT
            tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("PK_2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
            int timeToLive = -1;  // Never expire
            int maxVersions = 1;  // Only one version is saved
            TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
            CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
    • The first primary key is the partition key and cannot be set as the auto-increment column.

    • Only INTEGER columns can be set as the auto-increment column.

    • Only one primary key auto-increment column is allowed in each table.

  • Write data

    When writing data, you only need to set the auto-increment column values as the placeholder PrimaryKeyValue.AUTO_INCREMENT.

    If you want to have the primary key value automatically generated after data is written to the Table Store, you can set the ReturnType to RT_PK so that the primary key value is returned when the data is written successfully.

    Related interfaces: PutRow/UpdateRow/BatchWriteRow

        private static void putRow(SyncClient client, String receive_id) {
            // Construct the primary key
            PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
            // The value in the first column is the first four digits of md5(receive_id)
            primaryKeyBuilder.addPrimaryKeyColumn("PK_1", PrimaryKeyValue.fromString("Hangzhou"));
            // The third column is the primary key auto-increment column. This value is generated by Table Store. Set the auto-increment column values as the placeholder AUTO_INCREMENT. You do not need to enter a true value here.
            primaryKeyBuilder.addPrimaryKeyColumn("PK_2", PrimaryKeyValue.AUTO_INCREMENT);
            PrimaryKey primaryKey =;
            RowPutChange rowPutChange = new RowPutChange("table_name", primaryKey);
            // Here the return type is set to RT_PK, indicating to include the PK column value in the returned result. If the ReturnType is not set, no result is returned by default.
            //Add the attribute column and message content
            rowPutChange.addColumn(new Column("content", ColumnValue.fromString("content")));
            //Write data to the Table Store
            PutRowResponse response = client.putRow(new PutRowRequest(rowPutChange));
            // Print the returned PK column
            Row returnRow = response.getRow();
            if (returnRow != null) {
                System.out.println("PrimaryKey:" + returnRow.getPrimaryKey().toString());
            // Print the consumed CU
            CapacityUnit  cu = response.getConsumedCapacity().getCapacityUnit();
            System.out.println("Read CapacityUnit:" + cu.getReadCapacityUnit());
            System.out.println("Write CapacityUnit:" + cu.getWriteCapacityUnit());