全部產品
Search
文件中心

Tablestore:讀取單行資料

更新時間:May 12, 2026

本文介紹如何通過 .NET SDK 讀取Table Store資料表的單行資料。

注意事項

讀取資料時需要提供包含自增主鍵列值在內的完整主索引值。

前提條件

初始化Tablestore Client

方法說明

public GetRowResponse GetRow(GetRowRequest request)

非同步方法呼叫:

public Task<GetRowResponse> GetRowAsync(GetRowRequest request)

GetRowRequest參數說明

名稱

類型

說明

tableName(必選)

string

資料表名稱。

primaryKey(必選)

PrimaryKey

主鍵資訊,包括主鍵列名稱和主索引值。

  • 主鍵列資料類型包括 STRING、INTEGER 和 BINARY。

  • 主鍵個數和類型必須與資料表的主鍵保持一致。

maxVersion(可選)

int

最大版本數,預設值為1。

  • 最大版本數和版本範圍不可同時設定。

  • 如果符合查詢條件的資料版本數量超過設定的最大版本數,按從新到舊的順序返回指定版本數量的資料。

timeRange(可選)

TimeRange

資料版本範圍。

  • 最大版本數和版本範圍不可同時設定。

  • Table Store資料表的每個屬性列可以有不同的資料版本,設定版本範圍後,僅返回版本範圍內的資料。

columnsToGet(可選)

HashSet<string>

指定讀取的資料列,可以是主鍵列或屬性列。

  • 不設定columnsToGet時,返回整行資料。

  • 設定columnsToGet時,如果讀取的行資料不包含任何指定的資料列,將返回 null。

condition(可選)

IColumnCondition

過濾條件,詳情請參見過濾器

  • 如果同時設定columnsToGet和condition,先按columnsToGet篩選合格資料行,再按condition條件過濾資料。

範例程式碼

以下範例程式碼讀取了主索引值為 row1 的單行資料。

try
{
    // 構造主鍵
    PrimaryKey primaryKey = new PrimaryKey
    {
        { "id", new ColumnValue("row1") }
    };

    // 調用 GetRow 方法讀取行資料
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey);
    GetRowResponse getRowResponse = client.GetRow(getRowRequest);

    Console.WriteLine($"RequestId: {getRowResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRowResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRowResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine($"Row Data: {getRowResponse.Row}");
}
catch (Exception ex)
{
    Console.WriteLine($"Get row failed, exception: {ex.Message}");
}
  • 設定讀取的資料版本範圍,結果只返回版本範圍內的資料。

    TimeRange timeRange = new TimeRange
    {
        StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - 86400 * 1000,
        EndTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
    };
    
    // 調用 GetRow 方法讀取行資料
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, null, null, timeRange);
  • 指定讀取的屬性列。

    HashSet<string> columnsToGet = new HashSet<string> { "col2" };
    
    // 調用 GetRow 方法讀取行資料
    GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, columnsToGet);