在資料表上建立索引表後,可根據需要讀取索引表中的資料或者刪除資料表上指定的索引表。
前提條件
- 已初始化Client。具體操作,請參見初始化。
- 已建立資料表,且資料表的資料生命週期(TimeToLive)必須為-1,最大版本數(MaxVersions)必須為1。
- 資料表已設定預定義列。
建立索引表(CreateIndex)
使用CreateIndex介面在已存在的資料表上建立索引表。
说明 您也可以使用CreateTable介面在建立資料表的同時建立一個或者多個索引表。具體操作,請參見建立資料表。
- 參數
參數 說明 MainTableName 資料表名稱。 IndexMeta 索引表的結構資訊,包括如下內容: - IndexName:索引表名稱。
- PrimaryKey:索引表的索引列,索引列為資料表主鍵和預定義列的組合。
使用本地二級索引時,索引表的第一個主鍵列必須與資料表的第一個主鍵列相同。
- DefinedColumns:索引表的屬性列,索引表屬性列為資料表的預定義列的組合。
- IndexType:索引類型。可選值包括IT_GLOBAL_INDEX和IT_LOCAL_INDEX。
- 當不設定IndexType或者設定IndexType為IT_GLOBAL_INDEX時,表示使用全域二級索引。
使用全域二級索引時,Tablestore以非同步方式將資料表中被索引的列和主鍵列的資料自動同步到索引表中,正常情況下同步延遲達到毫秒層級。
- 當設定IndexType為IT_LOCAL_INDEX時,表示使用本地二級索引。
使用本地二級索引時,Tablestore以同步方式將資料表中被索引的列和主鍵列的資料自動同步到索引表中,當資料寫入資料表後,即可從索引表中查詢到資料。
- 當不設定IndexType或者設定IndexType為IT_GLOBAL_INDEX時,表示使用全域二級索引。
IncludeBaseData 索引表中是否包含資料表中已存在的資料。 當設定IncludeBaseData為true時,表示包含存量資料;設定IncludeBaseData為false時,表示不包含存量資料。
- 樣本
在主鍵為pk1、pk2的資料表上建立主鍵列為definedcol1,屬性列為definedcol2的索引表。
func CreateGlobalIndexSample(client *tablestore.TableStoreClient, tableName string) { indexMeta := new(tablestore.IndexMeta) //建立索引表Meta。 indexMeta.AddPrimaryKeyColumn("definedcol1") //設定資料表的definedcol1列作為索引表的主鍵。 indexMeta.AddDefinedColumn("definedcol2") //設定資料表的definedcol2列作為索引表的屬性列。 indexMeta.IndexName = "indexSample" indexReq := &tablestore.CreateIndexRequest{ MainTableName:tableName, //添加索引表到資料表。 IndexMeta: indexMeta, IncludeBaseData: true, //包含存量資料。 } /** 通過將IncludeBaseData參數設定為true,建立索引表後會開啟資料表中存量資料的同步,然後可以通過索引表查詢全部資料, 同步時間跟資料量的大小有一定的關係。 */ resp, err := client.CreateIndex(indexReq) if err != nil { fmt.Println("Failed to create table with error:", err) } else { fmt.Println("Create index finished", resp) } }
讀取索引表中資料
從索引表中單行或者範圍讀取資料,當返回的屬性列在索引表中時,可以直接讀取索引表擷取資料,否則請自行反查資料表擷取資料。
- 單行讀取索引表中資料
更多資訊,請參見讀取單行資料。
使用GetRow介面讀取索引表中資料時有如下注意事項:- TableName需要設定為索引表名稱。
- 由於系統會自動將未出現在索引列中的資料表主鍵補齊到索引表主鍵中,所以設定行的主鍵時,需要同時設定索引表索引列和補齊的資料表主鍵。
- 範圍讀索引表中資料
更多資訊,請參見範圍讀取資料。
- 參數使用GetRange介面讀取索引表中資料時有如下注意事項:
- TableName需要設定為索引表名稱。
- 由於系統會自動將未出現在索引列中的資料表主鍵補齊到索引表主鍵中,所以設定起始主鍵和結束主鍵時,需要同時設定索引表索引列和補齊的資料表主鍵。
- 樣本
func GetRangeFromIndex(client *tablestore.TableStoreClient, indexName string) { getRangeRequest := &tablestore.GetRangeRequest{} rangeRowQueryCriteria := &tablestore.RangeRowQueryCriteria{} rangeRowQueryCriteria.TableName = indexName startPK := new(tablestore.PrimaryKey) startPK.AddPrimaryKeyColumnWithMinValue("definedcol1") //索引表的第一主鍵列。 startPK.AddPrimaryKeyColumnWithMinValue("pk1") //索引表的第二主鍵列,此主鍵列為自動補齊的資料表主鍵列。 startPK.AddPrimaryKeyColumnWithMinValue("pk2") //索引表的第三主鍵列,此主鍵列為自動補齊的資料表主鍵列。 endPK := new(tablestore.PrimaryKey) endPK.AddPrimaryKeyColumnWithMaxValue("definedcol1") endPK.AddPrimaryKeyColumnWithMaxValue("pk1") endPK.AddPrimaryKeyColumnWithMaxValue("pk2") rangeRowQueryCriteria.StartPrimaryKey = startPK rangeRowQueryCriteria.EndPrimaryKey = endPK rangeRowQueryCriteria.Direction = tablestore.FORWARD rangeRowQueryCriteria.MaxVersion = 1 rangeRowQueryCriteria.Limit = 10 getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria getRangeResp, err := client.GetRange(getRangeRequest) fmt.Println("get range result is ", getRangeResp) for { if err != nil { fmt.Println("get range failed with error:", err) } if len(getRangeResp.Rows) > 0 { for _, row := range getRangeResp.Rows { fmt.Println("range get row with key", row.PrimaryKey.PrimaryKeys[0].Value, row.PrimaryKey.PrimaryKeys[1].Value, row.PrimaryKey.PrimaryKeys[2].Value) } if getRangeResp.NextStartPrimaryKey == nil { break } else { fmt.Println("next pk is :", getRangeResp.NextStartPrimaryKey.PrimaryKeys[0].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[1].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[2].Value) getRangeRequest.RangeRowQueryCriteria.StartPrimaryKey = getRangeResp.NextStartPrimaryKey getRangeResp, err = client.GetRange(getRangeRequest) } } else { break } fmt.Println("continue to query rows") } fmt.Println("putrow finished") }
- 參數
刪除索引表(DeleteIndex)
使用DeleteIndex介面刪除資料表上指定的索引表。
- 參數
參數 說明 MainTableName 資料表名稱。 IndexName 索引表名稱。 - 樣本
func DeleteIndex(client *tablestore.TableStoreClient, tableName string, indexName string) { deleteIndex := &tablestore.DeleteIndexRequest{ MainTableName:tableName, IndexName: indexName } resp, err := client.DeleteIndex(deleteIndex) if err != nil { fmt.Println("Failed to delete index:", err) } else { fmt.Println("drop index finished", resp) }