Topik ini menjelaskan cara membaca satu baris data dari tabel Tablestore menggunakan .NET SDK.
Usage notes
Saat membaca data, Anda harus menyediakan nilai kunci primer lengkap, termasuk nilai kolom kunci utama auto-increment jika tersedia.
Prerequisites
Method
public GetRowResponse GetRow(GetRowRequest request)Metode asinkron:
public Task<GetRowResponse> GetRowAsync(GetRowRequest request)Code example
Contoh berikut membaca satu baris dengan nilai kunci primer 'row1'.
try
{
// Construct the primary key.
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// Call the GetRow method to read the row.
GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey);
GetRowResponse getRowResponse = client.GetRow(getRowRequest);
Console.WriteLine($"RequestId: {getRowResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRowResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRowResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine($"Row Data: {getRowResponse.Row}");
}
catch (Exception ex)
{
Console.WriteLine($"Get row failed, exception: {ex.Message}");
}Tentukan rentang versi untuk data yang akan dibaca. Metode ini hanya mengembalikan data dalam rentang versi yang ditentukan.
TimeRange timeRange = new TimeRange { StartTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - 86400 * 1000, EndTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; // Call the GetRow method to read the row. GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, null, null, timeRange);Tentukan kolom atribut yang akan dibaca.
HashSet<string> columnsToGet = new HashSet<string> { "col2" }; // Call the GetRow method to read the row. GetRowRequest getRowRequest = new GetRowRequest("test_table", primaryKey, columnsToGet);