This topic describes how to use the .NET SDK of Table Store to create, query, or delete a global secondary index.

Create a global secondary index

  • Create a global secondary index when you create a primary table
    Example:
            public static void CreateTableWithGlobalIndex()
            {
                //Create a primary table that includes the two primary key columns Pk1 and Pk2. Additionally, the table has the two pre-defined columns Col1 and Col2.
                //Create a global secondary index. Specify Col1 as the first primary key column of the index.
                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);
            }
  • Create a global secondary index for an existing table
    You can create a global secondary index for an existing primary table. Example:
            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);
    
            }

Delete a global secondary index

You can delete a global secondary index from a primary table. Example:
        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);

        }

Query a global secondary index

Example:
        public static void GetRangeFromIndexTable()
        {
            Console.WriteLine("Start getRange from index...") ;
            OTSClient otsClient = Config.GetClient();
            // Specify the value of the first primary key Col1 and read the data.
            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("-----------------");
        }