Todos os produtos
Search
Central de documentação

Tablestore:Read a range of data

Última atualização: Jun 30, 2026

Use o Tablestore .NET SDK para ler dados em um intervalo de chave primária específico.

Pré-requisitos

Inicializar o cliente do Tablestore

Métodos

public GetRangeResponse GetRange(GetRangeRequest request)

Método assíncrono:

public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)

Parâmetros de GetRangeRequest

Parâmetro

Tipo

Descrição

tableName (Obrigatório)

string

Nome da tabela.

inclusiveStartPrimaryKey (Obrigatório)

PrimaryKey

Chave primária inicial do intervalo. Inclui nomes e valores das colunas de chave primária.

  • Os dados retornados incluem a chave primária inicial.

  • A quantidade e os tipos de chaves primárias devem corresponder aos da tabela.

  • Na leitura progressiva, a chave primária inicial deve ser menor que a final.

  • Na leitura regressiva, a chave primária inicial deve ser maior que a final.

  • ColumnValue.INF_MIN representa infinito negativo e ColumnValue.INF_MAX representa infinito positivo.

exclusiveEndPrimaryKey (Obrigatório)

PrimaryKey

Chave primária final do intervalo. Inclui nomes e valores das colunas de chave primária.

  • Os dados retornados excluem a chave primária final.

  • A quantidade e os tipos de chaves primárias devem corresponder aos da tabela.

  • ColumnValue.INF_MIN representa infinito negativo e ColumnValue.INF_MAX representa infinito positivo.

direction (Obrigatório)

GetRangeDirection

Direção da leitura.

  • GetRangeDirection.Forward: leitura progressiva.

  • GetRangeDirection.Backward: leitura regressiva.

limit (Opcional)

int

Número máximo de linhas a retornar. Deve ser maior que 0. Se houver mais linhas correspondentes, a resposta incluirá um valor NextPrimaryKey para paginação.

columnsToGet (Opcional)

HashSet<string>

Colunas a ler. Aceita colunas de chave primária e de atributo.

  • Se omitido, a operação retorna a linha inteira.

  • Se especificado, retorna apenas as colunas de chave primária e as colunas de atributo indicadas.

condition (Opcional)

IColumnCondition

Filtro de linhas. Filtro.

  • Quando columnsToGet e condition são definidos simultaneamente, o Tablestore filtra as linhas por condition primeiro e depois retorna apenas as colunas listadas em columnsToGet.

Exemplos de código

Leia todas as linhas de test_table cuja chave primária seja maior que row1:

try
{
    // Set the start primary key.
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    // Set the end primary key. The result set excludes the end primary key.
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", ColumnValue.INF_MAX }
    };

    // Call the GetRange method to read rows.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);
    GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);

    // Process the response.
    Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine("Row Data: ");
    foreach (Row row in getRangeResponse.RowDataList)
    {
        Console.WriteLine(row);
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}

Uma única solicitação GetRange retorna até 5.000 linhas ou 4 MB de dados. Caso existam mais dados correspondentes, a resposta conterá um valor NextPrimaryKey. Use-o como inclusiveStartPrimaryKey nas solicitações seguintes para paginar. O exemplo abaixo percorre todas as linhas correspondentes:

while (true)
{
    // Call the GetRange method to read rows.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);
    GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);

    // Process the response.
    Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
    Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
    Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
    Console.WriteLine("Row Data: ");
    foreach (Row row in getRangeResponse.RowDataList)
    {
        Console.WriteLine(row);
    }

    if (getRangeResponse.NextPrimaryKey != null)
    {
        inclusiveStartPrimaryKey = getRangeResponse.NextPrimaryKey;
    }
    else
    {
        break;
    }
}

Opções adicionais de consulta:

  • Ler regressivamente.

    // Set the start primary key. For a backward read, the start primary key must be greater than the end primary key.
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
    {
        { "id", ColumnValue.INF_MAX }
    };
    // Set the end primary key. The result set excludes the end primary key.
    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
    {
        { "id", new ColumnValue("row1") }
    };
    
    // Call the GetRange method to read rows backward.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Backward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);
  • Retornar colunas específicas.

    HashSet<string> columnsToGet = new HashSet<string> { "col2" };
    
    // Call the GetRange method to read rows.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, columnsToGet, null);
  • Limitar a quantidade de linhas.

    int limit = 10;
    
    // Call the GetRange method to read rows.
    GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, limit);

Tópicos relacionados