本文主要为您介绍如何通过表格存储的.NET SDK创建全局二级索引,读取全局二级索引表中的数据,以及删除全局二级索引。

创建全局二级索引

  • 创建主表同时创建全局二级索引
    示例如下:
            public static void CreateTableWithGlobalIndex()
            {
                //建主表,两列Pk:Pk1、Pk2。 预定义列:Col1、Col2。
                //建索引表,索引表中Col1放Pk0
                OTSClient otsClient = Config.GetClient();
    
                Console.WriteLine("Start create table with globalIndex...");
                PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
                    {
                        { Pk1, ColumnValueType.String },
                        { Pk2, ColumnValueType.String }
                    };
                TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);
                tableMeta.DefinedColumnSchema = new DefinedColumnSchema {
                       { Col1, DefinedColumnType.STRING},
                       { Col2,DefinedColumnType.STRING}
                    };
    
                IndexMeta indexMeta = new IndexMeta(IndexName);
                indexMeta.PrimaryKey = new List<string>() { Col1 };
                indexMeta.DefinedColumns = new List<string>() { Col2 };
                //indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX;
                //indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX;
    
                List<IndexMeta> indexMetas = new List<IndexMeta>() { };
                indexMetas.Add(indexMeta);
    
                CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
                CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas);
                otsClient.CreateTable(request);
    
                Console.WriteLine("Table is created: " + TableName);
            }
  • 单独创建全局二级索引
    您可以在一张已经存在的主表上创建全局二级索引表,示例如下:
            public static void CreateGlobalIndex()
            {
                OTSClient otsClient = Config.GetClient();
    
                Console.WriteLine("Start create globalIndex...");
    
                IndexMeta indexMeta = new IndexMeta(IndexName2);
                indexMeta.PrimaryKey = new List<string>() { Col2 };
                indexMeta.DefinedColumns = new List<string>() { Pk1 };
    
    
                CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
                CreateGlobalIndexRequest request = new CreateGlobalIndexRequest(TableName, indexMeta);
                otsClient.CreateGlobalIndex(request);
    
                Console.WriteLine("Global Index is created,tableName: " + TableName + ",IndexName:" + IndexName2);
    
            }

删除全局二级索引

您可以删除主表上的全局二级索引表,示例如下:
        public static void DeleteGlobalIndex()
        {
            OTSClient otsClient = Config.GetClient();

            Console.WriteLine("Start delete globalIndex...");

            DeleteGlobalIndexRequest request = new DeleteGlobalIndexRequest(TableName, IndexName);
            otsClient.DeleteGlobalIndex(request);

            DeleteGlobalIndexRequest request2 = new DeleteGlobalIndexRequest(TableName, IndexName2);
            otsClient.DeleteGlobalIndex(request2);

            Console.WriteLine("Global Index is deleted,tableName: " + TableName + ",IndexName:" + IndexName + "," + IndexName2);

        }

读取二级索引表中的数据

示例如下:
        public static void GetRangeFromIndexTable()
        {
            Console.WriteLine("Start getRange from index...");
            OTSClient otsClient = Config.GetClient();
            // 指定第一主键Col1的值,进行扫描
            PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey
            {
                { Col1, new ColumnValue("Col1Value") },
                { Pk1,  ColumnValue.INF_MIN },
                { Pk2, ColumnValue.INF_MIN }
            };

            PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey
            {
                { Col1, new ColumnValue("Col1Value") },
                { Pk1,  ColumnValue.INF_MAX },
                { Pk2, ColumnValue.INF_MAX }
            };

            GetRangeRequest request = new GetRangeRequest(IndexName, GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);

            GetRangeResponse response = otsClient.GetRange(request);
            IList<Row> rows = response.RowDataList;
            PrimaryKey nextStartPrimaryKey = response.NextPrimaryKey;
            while (nextStartPrimaryKey != null)
            {
                request = new GetRangeRequest(TableName, GetRangeDirection.Forward, nextStartPrimaryKey, exclusiveEndPrimaryKey);
                response = otsClient.GetRange(request);
                nextStartPrimaryKey = response.NextPrimaryKey;
                foreach (var row in response.RowDataList)
                {
                    rows.Add(row);
                }
            }

            foreach (var row in rows)
            {
                PrintRow(row);
            }

            Console.WriteLine("TotalRowsRead: " + rows.Count);
        }

        private static void PrintRow(Row row)
        {
            Console.WriteLine("-----------------");

            foreach (KeyValuePair<string, ColumnValue> entry in row.GetPrimaryKey())
            {
                Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
            }

            foreach (Column entry in row.GetColumns())
            {
                Console.WriteLine(entry.Name + ":" + PrintColumnValue(entry.Value));
            }

            Console.WriteLine("-----------------");
        }