Insert, update, and delete data in batches across multiple tables by calling batchWriteRow in the Tablestore PHP SDK.
Usage notes
If the server detects a parameter error in any operation, all operations in the request fail.
A single batch update operation supports up to 200 rows. The total data size of all rows must not exceed 4 MB.
Prerequisites
Method
public function batchWriteRow(array $request)Examples
The following code inserts a row into the test_table 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.';
}Refer to the following examples for different operation types.
PutRowItem: Insert a row.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_PUT, 'primary_key' => array ( array('id', 'row1') ) ) ) );Insert a row with attribute columns.
$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: Update a row by modifying attribute column values, adding attribute columns, or deleting a specific version or all versions of an attribute column.
$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 ) ) );Add or delete attribute columns when updating a row.
$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: Delete a row.
$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 ) ) );