全部產品
Search
文件中心

Tablestore:更新表配置

更新時間:Jun 10, 2025

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

前提條件

初始化Tablestore Client

方法說明

public UpdateTableResponse updateTable(UpdateTableRequest request) throws TableStoreException, ClientException

UpdateTableRequest參數說明

  • tableName(必選)String:資料表名稱。

  • tableOptionsForUpdate(可選)TableOptions:配置資訊,TableOptions包含以下參數。

    名稱

    類型

    說明

    timeToLive(可選)

    OptionalValue<Integer>

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

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

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

    maxVersions(可選)

    OptionalValue<Integer>

    最大版本數。

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

    maxTimeDeviation(可選)

    OptionalValue<Long>

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

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

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

    allowUpdate(可選)

    OptionalValue<Boolean>

    是否允許更新。

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

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

    名稱

    類型

    說明

    enableStream(必選)

    boolean

    是否開啟Stream。

    expirationTime(可選)

    OptionalValue<Integer>

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

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

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

說明
  • 調用 updateTable() 方法時,必須設定tableOptionsForUpdate、streamSpecification、reservedThroughputForUpdate中的至少一項。

  • 設定 tableOptionsForUpdate 時,必須設定timeToLive、maxVersions、maxTimeDeviation、allowUpdate中的至少一項。

範例程式碼

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

public static void updateTableExample(SyncClient client) {
    UpdateTableRequest request = new UpdateTableRequest("test_table");
    // 設定預留讀為0CU,預留寫為0CU
    ReservedThroughput reservedThroughput = new ReservedThroughput(0, 0);
    request.setReservedThroughputForUpdate(reservedThroughput);

    TableOptions tableOptions = new TableOptions();
    // 設定最大版本數
    tableOptions.setMaxVersions(3);
    // 設定資料生命週期,單位為秒
    tableOptions.setTimeToLive(86400);
    // 設定有效版本偏差,單位為秒
    tableOptions.setMaxTimeDeviation(86400);
    // 設定是否允許更新
    tableOptions.setAllowUpdate(false);
    request.setTableOptionsForUpdate(tableOptions);

    // 開啟Stream資訊,並設定Stream到期時間為7天
    StreamSpecification streamSpecification = new StreamSpecification(true, 168);
    request.setStreamSpecification(streamSpecification);
    
    // 調用updateTable方法修改表配置
    client.updateTable(request);
}

相關文檔

更新時序表配置