Utilisez le SDK .NET pour lire les données de Tablestore de manière itérative.
Prérequis
Méthode
public IEnumerable<Row> GetRangeIterator(GetIteratorRequest request)
Exemple de code
L'exemple suivant lit de manière itérative les données de la table test_table, en commençant par la valeur de clé primaire « row1 ».
try
{
// Set the start primary key.
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// Set the end primary key. The result does not include the end primary key.
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", ColumnValue.INF_MAX }
};
// Create a CapacityUnit object to track the capacity units consumed during iteration.
CapacityUnit capacityUnit = new CapacityUnit(0, 0);
// Create the request for the iterator.
GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit);
// Call GetRangeIterator to get the iterator.
IEnumerable<Row> iterator = client.GetRangeIterator(getIteratorRequest);
foreach (var row in iterator)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get range iterator failed, exception: {ex.Message}");
}
Les exemples suivants montrent comment configurer différents paramètres pour les lectures itératives.
-
Définissez le sens de lecture.
// 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 does not include the end primary key. PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey() { { "id", new ColumnValue("row1") } }; // Create the request for the iterator and set the direction to backward. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Backward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit); -
Définissez le nombre maximal de lignes à lire.
int limit = 20; // Create the request for the iterator. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, null, limit); -
Spécifiez les colonnes d'attribut à lire.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; // Create the request for the iterator. GetIteratorRequest getIteratorRequest = new GetIteratorRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, capacityUnit, columnsToGet);