全部產品
Search
文件中心

Tablestore:更新表配置

更新時間:Jun 20, 2025

本文介紹如何通過 Go SDK 更新表的配置資訊。

前提條件

初始化Tablestore Client

方法說明

func (tableStoreClient *TableStoreClient) UpdateTable(request *UpdateTableRequest) (*UpdateTableResponse, error)

UpdateTableRequest參數說明

  • TableName(必選)string:資料表名稱。

  • TableOption(可選)*TableOption:表配置資訊,包含以下參數。

    名稱

    類型

    說明

    TimeToAlive(必選)

    int

    資料生命週期,單位為秒。

    • 設定為-1時表示資料永不到期,否則取值最低為86400(1天),超出生命週期的資料將會被自動清除。

    • 如果要使用多元索引或二級索引功能,必須將資料生命週期設定為-1,或者將AllowUpdate參數設定為false。

    MaxVersion(必選)

    int

    最大版本數。

    • 如果要使用多元索引或二級索引,最大版本數必須設定為1。

    DeviationCellVersionInSec(可選)

    int64

    有效版本偏差,單位為秒。

    • 寫入資料的時間戳記與系統目前時間的差值必須在有效版本偏差範圍內,否則寫入資料將會失敗。

    • 屬性列資料的有效版本範圍為[max(資料寫入時間-有效版本偏差, 資料寫入時間-資料生命週期), 資料寫入時間+有效版本偏差)

    AllowUpdate(可選)

    *bool

    是否允許更新。

    • 設定為false時,無法通過UpdateRow()方法更新資料。

  • StreamSpec(可選)*StreamSpecification:Stream配置資訊,包含以下參數。

    名稱

    類型

    說明

    EnableStream(可選)

    bool

    是否開啟Stream,預設值為false。

    ExpirationTime(可選)

    int32

    Stream到期時間,表示增量日誌到期時間長度。單位為小時,最大值為168(7天)。

    • EnableStream設定為true時,必須設定expirationTime。

  • ReservedThroughput(可選)*ReservedThroughput預留讀寫輸送量,單位為CU,預設值為0,僅CU模式的高效能型執行個體可以設定為非零值且有效。

說明
  • 調用UpdateTable()方法時,必須設定TableOption、StreamSpec、ReservedThroughput中的至少一項。

  • 設定 TableOption 時,必須設定 TimeToAlive 和 MaxVersion 參數。

範例程式碼

以下範例程式碼用於修改test_table表的配置資訊。

func UpdateTableSample(client *tablestore.TableStoreClient) {
    updateTableReq := new(tablestore.UpdateTableRequest)
    updateTableReq.TableName = "test_table"
    
    // 表配置資訊
    updateTableReq.TableOption = new(tablestore.TableOption)
    // 設定資料生命週期,單位為秒
    updateTableReq.TableOption.TimeToAlive = 86400
    // 設定最大版本數
    updateTableReq.TableOption.MaxVersion = 3
    // 設定有效版本偏差,單位為秒
    updateTableReq.TableOption.DeviationCellVersionInSec = 86400
    // 設定是否允許更新
    updateTableReq.TableOption.AllowUpdate = proto.Bool(false)

    // 開啟Stream資訊,並設定Stream到期時間為7天
    updateTableReq.StreamSpec = new(tablestore.StreamSpecification)
    updateTableReq.StreamSpec.EnableStream = true
    updateTableReq.StreamSpec.ExpirationTime = 168
    
    // 設定預留讀為0CU,預留寫為0CU(僅CU模式高效能執行個體支援設定資料表的預留讀寫輸送量為非零值)
    updateTableReq.ReservedThroughput = new(tablestore.ReservedThroughput)
    updateTableReq.ReservedThroughput.Readcap = 0
    updateTableReq.ReservedThroughput.Writecap = 0

    // 調用UpdateTable方法修改表配置
    _, err := client.UpdateTable(updateTableReq)
    if (err != nil) {
        fmt.Println("Failed to update table with error:", err)
    } else {
        fmt.Println("Update table finished.")
    }
}

相關文檔

更新時序表配置