本文介紹如何通過 PHP SDK 批量讀取Table Store中的資料,支援查詢多個表的資料。
注意事項
單次批量讀取操作最多支援讀取 100 行資料。
前提條件
方法說明
public function batchGetRow(array $request)範例程式碼
以下範例程式碼用於讀取 test_table 表中主索引值為 row1 和 row2 的兩行資料。
$table = array (
'table_name' => 'test_table',
'max_versions' => 1,
'primary_keys' => array (
array ( array('id', 'row1') ),
array ( array('id', 'row2') )
)
);
$request = array (
'tables' => array (
$table
)
);
try {
// 調用 batchGetRow 方法讀取行資料
$response = $client->batchGetRow ($request);
// 返回結果處理
foreach ($response['tables'] as $tableGroup) {
echo "* Table: " . $tableGroup['table_name'] . "\n";
foreach ($tableGroup['rows'] as $row) {
if ($row['is_ok']) {
if ($row['primary_key'] != null) {
echo "Succeeded Row: " . json_encode($row['primary_key']) . json_encode($row['attribute_columns']) . "\n";
} else {
echo "Succeeded Row: This row is not exist." . "\n";
}
} else {
echo "Failed Row: " . $row['error']['code'] . '. ' . $row['error']['message'] . "\n";
}
}
}
} catch (Exception $e){
echo "Batch get row failed.";
}您可以在批量讀取資料時參考以下範例程式碼進行參數設定。
讀取多張表的資料。批量讀取支援一次讀取多張表的資料,您需要為每張表指定一個MultiRowQueryCriteria。
$table1 = array ( 'table_name' => 'orders_small', 'max_versions' => 1, 'primary_keys' => array ( array ( array('order_id', '90fb478c-1360-11f0-a34d-00163e30a2a9') ) ) ); $request = array ( 'tables' => array ( $table, $table1 ) );設定讀取的資料版本範圍,結果只返回版本範圍內的資料。
// 設定查詢的資料版本範圍為目前時間往前一天 $table['time_range'] = array ( 'start_time' => intval(microtime(true) * 1000) - 86400 * 1000, 'end_time' => intval(microtime(true) * 1000) );指定讀取的屬性列。
$table['columns_to_get'] = array('col2');