全部產品
Search
文件中心

Tablestore:批量讀取資料

更新時間:May 12, 2026

本文介紹如何通過 .NET SDK 批量讀取Table Store中的資料,支援查詢多個表的資料。

注意事項

單次批量讀取操作最多支援讀取 100 行資料。

前提條件

初始化Tablestore Client

方法說明

public BatchGetRowResponse BatchGetRow(BatchGetRowRequest request)

非同步方法呼叫:

public Task<BatchGetRowResponse> BatchGetRowAsync(BatchGetRowRequest request)

BatchGetRowRequest參數說明

  • rowQueryCriteriaDict(必選)IDictionary<string, MultiRowQueryCriteria>:批量讀取資料資訊,包含以下參數。

    名稱

    類型

    說明

    tableName(必選)

    string

    資料表名稱。

    primaryKeys(必選)

    List<PrimaryKey>

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

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

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

    columnsToGet(可選)

    HashSet<string>

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

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

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

    condition(可選)

    IColumnCondition

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

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

範例程式碼

以下範例程式碼用於讀取 test_table 表中主索引值為 row1 和 row2 的兩行資料。

try
{
    List<PrimaryKey> primaryKeys = new List<PrimaryKey>
    {
        // 添加第 1 行主鍵資訊
        new PrimaryKey {{ "id", new ColumnValue("row1") }},
        // 添加第 2 行主鍵資訊
        new PrimaryKey {{ "id", new ColumnValue("row2") }},
    };

    // 構造查詢條件
    BatchGetRowRequest batchGetRowRequest = new BatchGetRowRequest();
    batchGetRowRequest.Add("test_table", primaryKeys);

    // 調用 BatchGetRow 方法讀取行資料
    BatchGetRowResponse batchGetRowResponse = client.BatchGetRow(batchGetRowRequest);

    // 返回結果處理
    Console.WriteLine($"* RequestId: {batchGetRowResponse.RequestID}");
    Console.WriteLine($"* Is all succeeded: {batchGetRowResponse.IsAllSucceed}");
    foreach (var tableGroup in batchGetRowResponse.RowDataGroupByTable)
    {
        Console.WriteLine($"* Table: {tableGroup.Key}");
        foreach (var item in tableGroup.Value)
        {
            if (item.IsOK)
            {
                if (item.Row != null)
                {
                    Console.WriteLine($"Succeeded Row: {item.Row}");
                }
                else
                {
                    Console.WriteLine("Succeeded Row: This row is not exist.");
                }
            }
            else
            {
                Console.WriteLine($"Failed Row: {item.ErrorMessage}");
            }
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Batch get row failed, exception: {ex.Message}");
}

您可以在批量讀取資料時參考以下範例程式碼進行參數設定。

  • 讀取多張表的資料。批量讀取支援一次讀取多張表的資料,您需要為每張表指定一個MultiRowQueryCriteria。

    // 構造第 2 個表的查詢條件
    List<PrimaryKey> primaryKeys1 = new List<PrimaryKey>
    {
        new PrimaryKey {{ "order_id", new ColumnValue("90fb478c-1360-11f0-a34d-00163e30a2a9") }}
    };
    
    batchGetRowRequest.Add("orders_small", primaryKeys1);
  • 指定讀取的屬性列。

    HashSet<string> columnsToGet = new HashSet<string> { "col2" };
    
    batchGetRowRequest.Add("test_table", primaryKeys, columnsToGet);