更新表配置

本文介绍如何通过 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");
    // 设置预留读为50CU,预留写为20CU
    ReservedThroughput reservedThroughput = new ReservedThroughput(50, 20);
    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);
}

相关文档

更新时序表配置