更新表配置

本文介绍如何通过 .NET SDK 更新表的配置信息。

前提条件

初始化Tablestore Client

方法说明

public UpdateTableResponse UpdateTable(UpdateTableRequest request)

异步方法:

public Task<UpdateTableResponse> UpdateTableAsync(UpdateTableRequest request)

UpdateTableRequest参数说明

  • TableName(必选)string:数据表名称。

  • TableOptions(可选)TableOptions:表配置信息,包含以下参数。

    名称

    类型

    说明

    TimeToLive(可选)

    int

    数据生命周期,单位为秒,默认值为-1。

    • 设置为-1时表示数据永不过期,否则取值最低为86400(1天),超出生命周期的数据将会被自动清除。

    • 如果要使用多元索引或二级索引功能,必须将数据生命周期设置为-1,或者将AllowUpdate参数设置为false。

    MaxVersions(可选)

    int

    最大版本数,默认值为1。

    • 如果要使用多元索引或二级索引,最大版本数必须设置为1。

    DeviationCellVersionInSec(可选)

    long

    有效版本偏差,单位为秒。

    • 写入数据的时间戳与系统当前时间的差值必须在有效版本偏差范围内,否则写入数据将会失败。

    • 属性列数据的有效版本范围为[max(数据写入时间-有效版本偏差, 数据写入时间-数据生命周期), 数据写入时间+有效版本偏差)

    AllowUpdate(可选)

    bool

    是否允许更新。

    • 设置为false时,无法通过UpdateRow()方法更新数据。

  • StreamSpecification(可选)StreamSpecification:Stream配置信息,包含以下参数。

    名称

    类型

    说明

    EnableStream(必选)

    bool

    是否开启Stream。

    ExpirationTime(可选)

    int

    Stream过期时间,表示增量日志过期时长。单位为小时,最大值为168(7天)。

    • EnableStream设置为true时,必须设置ExpirationTime。

  • ReservedThroughput(可选)CapacityUnit预留读写吞吐量,单位为CU。仅CU模式的高性能型实例可以设置为非零值且有效。

说明

调用UpdateTable()方法时,必须设置TableOptions、StreamSpecification、ReservedThroughput中的至少一项。

示例代码

try
{
    UpdateTableRequest request = new UpdateTableRequest("test_table");

    // 表配置信息
    TableOptions tableOptions = new TableOptions();
    // 设置数据生命周期,单位为秒
    tableOptions.TimeToLive = 86400;
    // 设置最大版本数
    tableOptions.MaxVersions = 3;
    // 设置有效版本偏差,单位为秒
    tableOptions.DeviationCellVersionInSec = 86400;
    // 设置是否允许更新
    tableOptions.AllowUpdate = false;
    request.TableOptions = tableOptions;

    // 开启Stream信息,并设置Stream过期时间为7天
    StreamSpecification streamSpecification = new StreamSpecification(true);
    streamSpecification.ExpirationTime = 168;
    request.StreamSpecification = streamSpecification;

    // 设置预留读为0CU,预留写为0CU(仅CU模式高性能实例支持设置数据表的预留读写吞吐量为非零值)
    CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
    request.ReservedThroughput = reservedThroughput;

    // 调用UpdateTable方法修改表配置
    client.UpdateTable(request);
    Console.WriteLine("Update table succeeded.");
}
catch (Exception ex)
{
    Console.WriteLine($"Update table failed, exception:{ex.Message}");
}