Tablestore .NET SDK kullanarak belirtilen bir birincil anahtar aralığındaki verileri okuyun.
Önkoşullar
Yöntemler
public GetRangeResponse GetRange(GetRangeRequest request)
Zaman uyumsuz yöntem:
public Task<GetRangeResponse> GetRangeAsync(GetRangeRequest request)
Kod örnekleri
Birincil anahtarı row1 değerinden büyük olan test_table tablosundaki tüm satırları okuyun:
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}");
}
Tek bir GetRange isteği en fazla 5.000 satır veya 4 MB veri döndürür. Daha fazla veri eşleşirse yanıt bir NextPrimaryKey değeri içerir. Sayfalama yapmak için sonraki isteklerde bu değeri inclusiveStartPrimaryKey olarak kullanın. Aşağıdaki örnek eşleşen tüm satırlar arasında döngü oluşturur:
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;
}
}
Ek sorgu seçenekleri:
-
Geriye doğru okuma.
// 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); -
Belirli sütunları döndürme.
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); -
Satır sayısını sınırlama.
int limit = 10; // Call the GetRange method to read rows. GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, limit);