使用 Tablestore Go SDK 为新建或已有多元索引设置数据生命周期,并按需调整数据表生命周期。
前提条件
安装Tablestore Go SDK并初始化客户端。
注意事项
多元索引生命周期取值为 -1 或不小于 86400 的 int32 整数,单位为秒。-1 表示数据永不过期。设置非 -1 值时,必须禁止通过 UpdateRow 更新数据,且多元索引生命周期不能大于数据表生命周期。生命周期到期的数据由后台异步清理。
操作步骤
完整操作包括禁止通过 UpdateRow 更新数据、设置多元索引生命周期,以及在需要时调整数据表生命周期。调整顺序取决于目标值:如果要将多元索引生命周期调大到超过当前数据表生命周期,必须先调大数据表生命周期;其他情况下可在设置多元索引后按需调整数据表。
1. 禁止通过 UpdateRow 更新数据
更新数据表配置,将 AllowUpdate 设置为 false。具体操作请参见更新表配置。
2. 设置多元索引生命周期
新建多元索引
ttl := int32(7 * 24 * 60 * 60)
request := &tablestore.CreateSearchIndexRequest{
TableName: "example_table",
IndexName: "example_index",
TimeToLive: &ttl,
IndexSchema: &tablestore.IndexSchema{
FieldSchemas: []*tablestore.FieldSchema{
{
FieldName: proto.String("category"),
FieldType: tablestore.FieldType_KEYWORD,
Index: proto.Bool(true),
},
},
},
}
_, err := client.CreateSearchIndex(request)
if err != nil {
log.Fatal(err)
}
已有多元索引
ttl := int32(7 * 24 * 60 * 60)
_, err := client.UpdateSearchIndex(&tablestore.UpdateSearchIndexRequest{
TableName: "example_table",
IndexName: "example_index",
TimeToLive: &ttl,
})
if err != nil {
log.Fatal(err)
}
3. 按需调整数据表生命周期
如果业务需要同步修改数据表生命周期,请确保其值不小于多元索引生命周期。具体操作请参见更新表配置。
参数说明
|
名称 |
类型 |
说明 |
|
TimeToLive(可选) |
*int32 |
多元索引数据生命周期,单位为秒。取值为 -1 或不小于 86400 的 int32 整数,-1 表示数据永不过期。创建多元索引时默认值为 -1。 |
该文章对您有帮助吗?