在数据表上创建索引表后,可根据需要读取索引表中的数据或者删除数据表上指定的索引表。
前提条件
- 已初始化Client。具体操作,请参见初始化。
- 已创建数据表,且数据表的最大版本数(maxVersions)必须为1,数据生命周期(timeToLive)必须满足如下条件中的任意一个。
- 数据表的数据生命周期为-1(数据永不过期)。
- 数据表的数据生命周期不为-1时,数据表为禁止更新状态。
- 数据表已设置预定义列。
创建索引表(CreateGlobalIndex)
使用CreateIndex接口在已存在的数据表上创建索引表。
说明 您也可以使用CreateTable接口在创建数据表的同时创建一个或者多个索引表,详情请参见创建数据表。
- 参数
参数 说明 mainTableName 数据表名称。 indexMeta 索引表的结构信息,包括如下内容: - indexName:索引表名称。
- primaryKey:索引表的索引列,索引列为数据表主键和预定义列的任意组合。
- definedColumns:索引表的属性列,索引表属性列为数据表的预定义列的组合。
- indexUpdateMode:索引表更新模式,当前只支持IUM_ASYNC_INDEX。
- indexType:索引表类型,当前只支持IT_GLOBAL_INDEX。
includeBaseData 索引表中是否包含数据表中已存在的数据。 当设置includeBaseData为true时,表示包含存量数据;设置includeBaseData为false时,表示不包含存量数据。
- 示例
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); }
读取索引表中数据
从索引表中单行或者范围读取数据,当返回的属性列在索引表中时,可以直接读取索引表获取数据,否则请自行反查数据表获取数据。
- 单行读取索引表中数据
详情请参见读取单行数据。
使用GetRow接口读取索引表中数据时有如下注意事项:- tableName需要设置为索引表名称。
- 由于系统会自动将未出现在索引列中的数据表主键补齐到索引表主键中,所以设置行的主键时,需要同时设置索引表索引列和补齐的数据表主键。
- 范围读索引表中数据
详情请参见范围读取数据。
- 参数使用GetRange接口读取索引表中数据时有如下注意事项:
- tableName需要设置为索引表名称。
- 由于系统会自动将未出现在索引列中的数据表主键补齐到索引表主键中,所以设置起始主键和结束主键时,需要同时设置索引表索引列和补齐的数据表主键。
- 示例
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("-----------------"); }
- 参数
删除索引表(DeleteGlobalIndex)
使用DeleteIndex接口删除数据表上指定的索引表。
- 参数
参数 说明 mainTableName 数据表名称。 indexName 索引表名称。 - 示例
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); }