本文介紹如何通過 PHP SDK 讀取Table Store資料表的單行資料。
注意事項
讀取資料時需要提供包含自增主鍵列值在內的完整主索引值。
前提條件
方法說明
public function getRow(array $request)範例程式碼
以下範例程式碼讀取了主索引值為 row1 的單行資料。
$request = array (
'table_name' => 'test_table',
// 構造主鍵
'primary_key' => array (
array ('id', 'row1')
),
'max_versions' => 1
);
try {
// 調用 getRow 方法讀取行資料
$response = $client->getRow ($request);
echo "* Read CU Cost: " . $response['consumed']['capacity_unit']['read'] . "\n";
echo "* Write CU Cost: " . $response['consumed']['capacity_unit']['write'] . "\n";
echo "* Row Data: " . "\n";
echo "Primary Key: ". json_encode($response['primary_key']) . "\n";
echo "Attribute Columns: ". json_encode($response['attribute_columns']) . "\n";
} catch (Exception $e){
echo "Get Row failed.";
}設定讀取的資料版本範圍,結果只返回版本範圍內的資料。
// 設定查詢的資料版本範圍為目前時間往前一天 $request['time_range'] = array ( 'start_time' => intval(microtime(true) * 1000) - 86400 * 1000, 'end_time' => intval(microtime(true) * 1000) );指定讀取的屬性列。
$request['columns_to_get'] = array('col2');