Use o Tablestore PHP SDK para ler linhas dentro de um intervalo de chave primária especificado.
Pré-requisitos
Método
public function getRange(array $request)
Exemplos
Leia dados da tabela test_table em que a chave primária seja maior que 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.";
}
Uma única varredura de intervalo retorna até 5.000 linhas ou 4 MB. Caso o resultado exceda esses limites, a resposta incluirá uma próxima chave primária inicial. Use consultas iterativas para ler os dados restantes.
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;
}
}
Você também pode definir as seguintes configurações opcionais:
-
Defina a direção de leitura como regressiva.
$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 ); -
Defina um intervalo de versões. Apenas os dados dentro do intervalo especificado serão retornados.
// 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) ); -
Especifique as colunas de atributo a serem lidas.
$request['columns_to_get'] = array('col2'); -
Defina o número máximo de linhas retornadas por solicitação.
$request['limit'] = 10;