设置非分区键的主键列为自增列后,在写入数据时,无需为自增列设置具体值,表格存储会自动生成自增列的值。该值在分区键级别唯一且严格递增。

前提条件

已初始化Client。具体操作,请参见初始化

使用方法

  1. 创建表时,将非分区键的主键列设置为自增列。

    只有整型的主键列才能设置为自增列,系统自动生成的自增列值为64位的有符号长整型。

  2. 写入数据时,无需为自增列设置具体值,只需将自增列的值设置为占位符。

    如果需要获取写入数据后系统自动生成的自增列的值,将ReturnType设置为RT_PK,可以在数据写入成功后返回自增列的值。

    查询数据时,需要完整的主键值。通过设置PutRow、UpdateRow或者BatchWriteRow中的ReturnType为RT_PK可以获取完整的主键值。

    说明 如果要更新已存在的行,请先通过GetRange接口获取要更新的行主键信息,然后再进行数据更新。

示例

主键自增列功能主要涉及创建表(CreateTable)和写数据(PutRow、UpdateRow和BatchWriteRow)两类接口。
  • 创建表时,只需将自增的主键属性设置为AUTO_INCREMENT。
  • 写入数据时,无需为自增列设置具体值,只需将自增列的值设置为占位符AUTO_INCREMENT。
using System;
using System.Collections.Generic;
using Aliyun.OTS.DataModel;
using Aliyun.OTS.Request;
using Aliyun.OTS.Response;

namespace Aliyun.OTS.Samples.Samples
{
    /// <summary>
    /// 主键列自增示例。
    /// </summary>
    public class AutoIncrementSample
    {
        private static readonly string TableName = "AutoIncrementSample";

        private static readonly string Pk1 = "Pk1";
        private static readonly string Pk2 = "Pk2_AutoIncrement";


        static void Main(string[] args)
        {
            Console.WriteLine("AutoIncrementSample");

            //创建一个带自增列的表。
            CreateTableWithAutoIncrementPk();

            //写入10行数据。
            for (int i = 0; i < 10; i++)
            {  
                PutRow(i.ToString());
            }

            Console.ReadLine();

        }

        /// <summary>
        /// 创建一个带自增列的表。
        /// </summary>
        private static void CreateTableWithAutoIncrementPk()
        {

            OTSClient otsClient = Config.GetClient();

            IList<string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;
            if (tables.Contains(TableName))
            {
                return;
            }

            PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
            {
                { Pk1, ColumnValueType.String },
                //指定Pk2为自增主键。
                { Pk2, ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT}
            };
            TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);

            CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
            CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
            otsClient.CreateTable(request);

        }

        public static void PutRow(string pk1Value)
        {
            Console.WriteLine("Start put row...");
            OTSClient otsClient = Config.GetClient();

            //定义行的主键,必须与创建表时的TableMeta中定义的一致。
            PrimaryKey primaryKey = new PrimaryKey
            {
                { Pk1, new ColumnValue(pk1Value) },
                { Pk2,  ColumnValue.AUTO_INCREMENT }
            };

            //定义要写入该行的属性列。
            AttributeColumns attribute = new AttributeColumns
            {
                { "Col1", new ColumnValue(0) }
            };
            PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);
            request.RowPutChange.ReturnType = ReturnType.RT_PK;

            var response =  otsClient.PutRow(request);
            Console.WriteLine("Put row succeed,autoIncrement Pk value:"+ response.Row.GetPrimaryKey()[Pk2].IntegerValue);
        }

    }
}