設定非分區鍵的主鍵列為自增列後,在寫入資料時,無需為自增列設定具體值,Tablestore會自動產生自增列的值。該值在分區鍵層級唯一且嚴格遞增。

前提條件

已初始化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);
        }
    }
}