Use the Tablestore PHP SDK to read rows within a specified primary key range.
Prerequisites
Method
public function getRange(array $request)Examples
The following example reads data from the test_table table where the primary key value is greater than row1.
$request = array (
'table_name' => 'test_table',
// Set the start primary key
'inclusive_start_primary_key' => array (
array('id', 'row1')
),
// Set the end primary key (the response excludes this key)
'exclusive_end_primary_key' => array (
array('id', null, PrimaryKeyTypeConst::CONST_INF_MAX)
),
// Set forward read
'direction' => DirectionConst::CONST_FORWARD,
// Set the number of versions to read
'max_versions' => 1
);
try {
// Call getRange to read rows
$response = $client->getRange ($request);
// Process the response
echo "* Read CU Cost: " . $response['consumed']['capacity_unit']['read'] . "\n";
echo "* Write CU Cost: " . $response['consumed']['capacity_unit']['write'] . "\n";
echo "* Row Data: " . "\n";
foreach ($response['rows'] as $row) {
echo json_encode($row) . "\n";
}
} catch (Exception $e){
echo "Get Range failed.";
}A single range scan returns up to 5,000 rows or 4 MB. If the result exceeds these limits, the response includes a start primary key for the next request. Use the following code to perform iterative queries.
while (true) {
// Call getRange to read rows
$response = $client->getRange ($request);
// Process the response
echo "* Read CU Cost: " . $response['consumed']['capacity_unit']['read'] . "\n";
echo "* Write CU Cost: " . $response['consumed']['capacity_unit']['write'] . "\n";
echo "* Row Data: " . "\n";
foreach ($response['rows'] as $row) {
echo json_encode($row) . "\n";
}
if ($response['next_start_primary_key'] != null){
$request['inclusive_start_primary_key'] = $response['next_start_primary_key'];
} else {
break;
}
}Configure the following optional settings as needed.
Set the read direction to reverse.
$request = array ( 'table_name' => 'test_table', // Set the start primary key (for backward reads, the start key must be greater than the end key) 'inclusive_start_primary_key' => array ( array('id', null, PrimaryKeyTypeConst::CONST_INF_MAX) ), // Set the end primary key (the response excludes this key) 'exclusive_end_primary_key' => array ( array('id', 'row1') ), // Set backward read 'direction' => DirectionConst::CONST_BACKWARD, // Set the number of versions to read 'max_versions' => 1 );Set a version range. Only data within the specified range is returned.
// Set the version range to one day before the current time $request['time_range'] = array ( 'start_time' => intval(microtime(true) * 1000) - 86400 * 1000, 'end_time' => intval(microtime(true) * 1000) );Specify the attribute columns to read.
$request['columns_to_get'] = array('col2');Set the maximum number of rows to return per request.
$request['limit'] = 10;