在資料表上建立索引表後,可根據需要讀取索引表中的資料或者刪除資料表上指定的索引表。

前提條件

  • 已初始化Client,詳情請參見初始化
  • 已建立資料表,且資料表的資料生命週期(timeToLive)必須為-1,最大版本數(maxVersions)必須為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);
    
    }