生命周期管理
數(shù)據(jù)生命周期(Time To Live,簡(jiǎn)稱TTL)是多元索引的一個(gè)屬性,即數(shù)據(jù)的保存時(shí)間。多元索引會(huì)自動(dòng)清理超過保存時(shí)間的數(shù)據(jù),減少用戶的數(shù)據(jù)存儲(chǔ)空間,降低存儲(chǔ)成本。
前提條件
已初始化Client。具體操作,請(qǐng)參見初始化OTSClient。
已創(chuàng)建數(shù)據(jù)表。具體操作,請(qǐng)參見創(chuàng)建數(shù)據(jù)表。
注意事項(xiàng)
使用生命周期管理功能,必須禁用數(shù)據(jù)表的UpdateRow更新寫入功能,避免一些語義上的問題:
由于數(shù)據(jù)表TTL是屬性列級(jí)別生效的,而多元索引TTL是整行生效的,如果存在UpdateRow寫入操作,當(dāng)系統(tǒng)清理數(shù)據(jù)表中數(shù)據(jù)時(shí),數(shù)據(jù)表中部分字段值已刪除而部分字段值未刪除,但是多元索引中整行數(shù)據(jù)均未刪除,則會(huì)造成數(shù)據(jù)表和多元索引中的數(shù)據(jù)不一致。
如果業(yè)務(wù)有UpdateRow更新寫入操作,請(qǐng)查看是否能改為PutRow覆蓋寫入操作。
多元索引的TTL取值范圍為-1或者int32的正整數(shù)(單位為秒),其中-1表示永久存儲(chǔ),int32最大值換算為年大約為68年。
多元索引的TTL和數(shù)據(jù)表的TTL是獨(dú)立的,多元索引的TTL值必須小于或等于數(shù)據(jù)表的TTL值。當(dāng)需要同時(shí)調(diào)小多元索引TTL和數(shù)據(jù)表TTL時(shí),請(qǐng)先調(diào)整多元索引TTL,再調(diào)整數(shù)據(jù)表TTL。
多元索引每天會(huì)自動(dòng)清理已過期的數(shù)據(jù),過期數(shù)據(jù)的清理粒度為“天”,因此您仍然可以查詢到某一時(shí)刻已過期但是還未及時(shí)清理的數(shù)據(jù),多元索引會(huì)在下一次清理過期數(shù)據(jù)時(shí)自動(dòng)清理這些過期數(shù)據(jù)。
數(shù)據(jù)表和多元索引的TTL更新后,系統(tǒng)會(huì)在下一次清理過期數(shù)據(jù)時(shí)自動(dòng)清理數(shù)據(jù)表和多元索引中的存量過期數(shù)據(jù)。
使用流程
禁用數(shù)據(jù)表UpdateRow更新寫入操作。
以下示例用于禁用數(shù)據(jù)表的UpdateRow更新寫入操作。
func disableTableUpdate(client *tablestore.TableStoreClient) { request := &tablestore.UpdateTableRequest{ TableName: "TableName", TableOption: &tablestore.TableOption{ TimeToAlive: -1, // 數(shù)據(jù)表生命周期保持默認(rèn),默認(rèn)值為-1。 MaxVersion: 1, // 最大版本數(shù)保持默認(rèn),默認(rèn)值為1。 DeviationCellVersionInSec: 86400, // 有效版本偏差保持默認(rèn),默認(rèn)值為86400。單位為秒。 // 禁用數(shù)據(jù)表UpdateRow更新寫入操作,請(qǐng)確保數(shù)據(jù)表無UpdateRow寫入操作,避免影響業(yè)務(wù)。注意多元索引存在TTL時(shí)不能修改為允許更新。 AllowUpdate: proto.Bool(false), }, } resp, err := client.UpdateTable(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("UpdateTable finished, requestId:", resp.ResponseInfo.RequestId) }
設(shè)置多元索引生命周期。
禁用數(shù)據(jù)表UpdateRow更新寫入操作后,您可以在創(chuàng)建多元索引時(shí)指定TTL或者為已有多元索引指定TTL。
創(chuàng)建多元索引時(shí)指定TTL
以下示例用于創(chuàng)建一個(gè)多元索引,多元索引包含col1和col2兩列,類型分別設(shè)置為字符串(String)和整型(Long)。同時(shí)指定多元索引生命周期為7天。
func createIndexWithTTL(client *tablestore.TableStoreClient) { request := &tablestore.CreateSearchIndexRequest{} request.TableName = "<TABLE_NAME>" request.IndexName = "<SEARCH_INDEX_NAME>" schemas := []*tablestore.FieldSchema{} field1 := &tablestore.FieldSchema{ FieldName: proto.String("col1"), //設(shè)置字段名,使用proto.String用于獲取字符串指針。 FieldType: tablestore.FieldType_KEYWORD, //設(shè)置字段類型。 Index: proto.Bool(true), //設(shè)置開啟索引。 EnableSortAndAgg: proto.Bool(true), //設(shè)置開啟排序與統(tǒng)計(jì)聚合功能。 } field2 := &tablestore.FieldSchema{ FieldName: proto.String("col2"), FieldType: tablestore.FieldType_LONG, Index: proto.Bool(true), EnableSortAndAgg: proto.Bool(true), } schemas = append(schemas, field1, field2) request.IndexSchema = &tablestore.IndexSchema{ FieldSchemas: schemas, //設(shè)置多元索引包含的字段。 } request.TimeToLive = proto.Int32(3600 * 24 * 7) // 設(shè)置多元索引TTL為7天過期。 resp, err := client.CreateSearchIndex(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId) }
為已有多元索引指定TTL
以下示例用于指定已有多元索引的生命周期為7天。
func updateIndexWithTTL(client *tablestore.TableStoreClient) { request := &tablestore.UpdateSearchIndexRequest{} request.TableName = "TableName" request.IndexName = "IndexName" request.TimeToLive = proto.Int32(3600 * 24 * 7) // 設(shè)置多元索引TTL為7天過期。 resp, err := client.UpdateSearchIndex(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("updateIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId) }
多元索引的TTL和數(shù)據(jù)表的TTL是獨(dú)立的。如果需要使用數(shù)據(jù)表TTL,請(qǐng)為數(shù)據(jù)表設(shè)置TTL。
以下示例用于指定數(shù)據(jù)表的生命周期為7天。
// 設(shè)置數(shù)據(jù)表的TTL為7天過期。 func updateTableTTL(client *tablestore.TableStoreClient) { request := &tablestore.UpdateTableRequest{ TableName: "TableName", TableOption: &tablestore.TableOption{ TimeToAlive: 3600 * 24 * 7, // 設(shè)置數(shù)據(jù)表的TTL為7天過期,請(qǐng)確保數(shù)據(jù)表的TTL大于等于多元索引TTL。 MaxVersion: 1, // 最大版本數(shù)保持默認(rèn),默認(rèn)值為1。 DeviationCellVersionInSec: 86400, // 有效版本偏差保持默認(rèn), 默認(rèn)值為86400。單位為秒。 // 禁用數(shù)據(jù)表Update更新寫入操作,請(qǐng)確保數(shù)據(jù)表無Update寫入操作,避免影響業(yè)務(wù)。注意多元索引存在TTL時(shí)不能修改為允許更新。 AllowUpdate: proto.Bool(false), }, } resp, err := client.UpdateTable(request) if err != nil { fmt.Println("error :", err) return } fmt.Println("UpdateTable finished, requestId:", resp.ResponseInfo.RequestId) }
常見問題
修改數(shù)據(jù)表生命周期時(shí)報(bào)錯(cuò)[table ttl] must be bigger than or equal search index ttl
相關(guān)文檔
如果要獲取某個(gè)數(shù)據(jù)表關(guān)聯(lián)的所有多元索引的列表信息,您可以使用列出多元索引列表功能實(shí)現(xiàn)。具體操作,請(qǐng)參見列出多元索引列表。
如果要查詢多元索引的描述信息,包括多元索引的字段信息和索引配置等,您可以使用查詢多元索引描述信息功能實(shí)現(xiàn)。具體操作,請(qǐng)參見查詢多元索引描述信息。
如果要在多元索引中新增、更新或者刪除索引列,您可以使用動(dòng)態(tài)修改schema功能實(shí)現(xiàn)。具體操作,請(qǐng)參見動(dòng)態(tài)修改schema。
如果不再需要使用多元索引,您可以刪除多元索引。具體操作,請(qǐng)參見刪除多元索引。