This topic explains how to configure an auto-increment primary key column for a data table using the .NET SDK, as well as how to write data to the column and retrieve the generated values.
Usage notes
The auto-increment values generated for an auto-increment column are unique and strictly incremental at the partition key level. However, they are not guaranteed to be consecutive.
Prerequisites
Configure an auto-increment primary key column
You can configure a non-partition primary key column as an auto-increment column when you create a data table. For existing data tables, you cannot configure auto-increment columns.
Only integer non-partition primary key columns can be configured as auto-increment columns. A data table can have at most one auto-increment column. The values generated for an auto-increment column are 64-bit signed long integers.
Sample code
The following sample code creates a data table named test_table. The primary key of the table includes the partition key id and the auto-increment column incr.
public static void CreateSample(OTSClient client)
{
try
{
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
{
{ "id", ColumnValueType.String },
{ "incr", ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT}
};
TableMeta tableMeta = new TableMeta("test_table", primaryKeySchema);
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
client.CreateTable(request);
Console.WriteLine("Create table succeeded.");
}
catch (Exception ex)
{
Console.WriteLine($"Create table failed, exception:{ex.Message}");
}
}Write data
When writing data to an auto-increment column, set the column value to a placeholder. To retrieve the generated auto-increment value for queries and updates, set the return type of RowPutChange to ReturnType.RT_PK.
Sample code
The following sample code writes a row of data to the test_table table, and gets and prints the primary key information of the written row data.
public static void PutRowSample(OTSClient client)
{
try
{
// Construct the primary key
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") },
{ "incr", ColumnValue.AUTO_INCREMENT }
};
// Construct the row data to write
AttributeColumns attribute = new AttributeColumns
{
{ "col1", new ColumnValue("val1") }
};
PutRowRequest request = new PutRowRequest("test_table", new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
// Set the return type to ReturnType.RT_PK to return the primary key information of the written row data
request.RowPutChange.ReturnType = ReturnType.RT_PK;
// Call the PutRow method to write the row data
PutRowResponse response = client.PutRow(request);
// RequestId and consumed CU
Console.WriteLine("RequestId: " + response.RequestID);
Console.WriteLine("Read CU Cost: " + response.ConsumedCapacityUnit.Read);
Console.WriteLine("Write CU Cost: " + response.ConsumedCapacityUnit.Write);
// Get and print the returned primary key information. If the return type is not set to ReturnType.RT_PK, the primary key information is not returned by default
Console.WriteLine(response.Row.PrimaryKey.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"Put Row failed, exception:{ex.Message}");
}
}