Use batchWriteRow en el SDK de PHP para Tablestore para insertar, actualizar y eliminar datos por lotes en varias tablas.
Notas de uso
Si el servidor detecta un error de parámetro en alguna operación, todas las operaciones de la solicitud fallan.
Una sola operación de actualización por lotes admite hasta 200 filas. El tamaño total de los datos de todas las filas no debe superar los 4 MB.
Requisitos previos
Método
public function batchWriteRow(array $request)
Ejemplos
El siguiente ejemplo inserta una fila en la tabla test_table.
$table = array (
'table_name' => 'test_table',
'rows' => array (
array (
'operation_type' => OperationTypeConst::CONST_PUT,
'primary_key' => array ( array('id', 'row1') )
)
)
);
$request = array(
'tables' => array (
$table
)
);
try {
// Call the batchWriteRow method to perform batch data operations.
$response = $client->batchWriteRow ($request);
// Process the response.
foreach ($response['tables'] as $tableGroup) {
foreach ($tableGroup['rows'] as $row) {
if (!$row['is_ok']) {
echo "TableName: {$tableGroup['table_name']}. ErrorCode: {$row['error']['code']}. ErrorMessage: {$row['error']['message']} \n";
}
}
}
} catch (Exception $e){
echo 'Batch write row failed.';
}
A continuación se muestran ejemplos de distintos tipos de operación.
-
PutRowItem: Inserte una fila.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_PUT, 'primary_key' => array ( array('id', 'row1') ) ) ) );Inserte una fila con columnas de atributo.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_PUT, 'primary_key' => array ( array('id', 'row1') ), 'attribute_columns' => array ( array('col1', 'val1'), array('col2', 'val2', null, intval(microtime(true) * 1000)) ) ) ) ); -
UpdateRowItem: Actualice una fila al modificar valores de columnas de atributo, agregar columnas de atributo o eliminar una versión específica o todas las versiones de una columna de atributo.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_UPDATE, 'primary_key' => array ( array('id', 'row1') ), 'update_of_attribute_columns'=> array( 'PUT' => array ( array('col1', 'changed_val1') ) ), // A condition is required to update a row. RowExistenceExpectation.IGNORE skips the row existence check. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) );Agregue o elimine columnas de atributo al actualizar una fila.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_UPDATE, 'primary_key' => array ( array('id', 'row1') ), 'update_of_attribute_columns'=> array( 'PUT' => array ( // Add an attribute column. array('col3', 'val3'), // Add an attribute column with a custom version number. array('col4', 'val4', null, intval(microtime(true) * 1000)) ), // Delete an attribute column. 'DELETE_ALL' => array('col2') ), // A condition is required to update a row. RowExistenceExpectation.IGNORE skips the row existence check. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) ); -
DeleteRowItem: Elimine una fila.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_DELETE, 'primary_key' => array ( array('id', 'row1') ), // A condition is required to delete a row. RowExistenceExpectation.IGNORE skips the row existence check. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) );