Use o Tablestore .NET SDK para ler dados em um intervalo de chave primária específico.
Pré-requisitos
Métodos
public GetRangeResponse GetRange(GetRangeRequest request)
Método assíncrono:
public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)
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);