Masukkan, perbarui, dan hapus data secara batch di beberapa tabel dengan memanggil batchWriteRow dalam Tablestore PHP SDK.
Catatan penggunaan
Jika server mendeteksi kesalahan parameter pada salah satu operasi, seluruh permintaan akan gagal.
Satu operasi pembaruan batch mendukung hingga 200 baris, dengan ukuran total data dari semua baris tidak melebihi 4 MB.
Prasyarat
Metode
public function batchWriteRow(array $request)Contoh
Kode berikut memasukkan satu baris ke dalam tabel 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 {
// Panggil metode batchWriteRow untuk melakukan operasi data batch.
$response = $client->batchWriteRow ($request);
// Proses respons.
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.';
}Lihat contoh berikut untuk berbagai jenis operasi.
PutRowItem: Masukkan baris.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_PUT, 'primary_key' => array ( array('id', 'row1') ) ) ) );Masukkan baris dengan kolom atribut.
$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: Perbarui baris dengan mengubah nilai kolom atribut, menambahkan kolom atribut, atau menghapus versi tertentu atau semua versi dari kolom atribut.
$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') ) ), // Kondisi diperlukan untuk memperbarui baris. RowExistenceExpectation.IGNORE melewati pemeriksaan keberadaan baris. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) );Tambahkan atau hapus kolom atribut saat memperbarui baris.
$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 ( // Tambahkan kolom atribut. array('col3', 'val3'), // Tambahkan kolom atribut dengan nomor versi kustom. array('col4', 'val4', null, intval(microtime(true) * 1000)) ), // Hapus kolom atribut. 'DELETE_ALL' => array('col2') ), // Kondisi diperlukan untuk memperbarui baris. RowExistenceExpectation.IGNORE melewati pemeriksaan keberadaan baris. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) );DeleteRowItem: Hapus baris.
$table = array ( 'table_name' => 'test_table', 'rows' => array ( array ( 'operation_type' => OperationTypeConst::CONST_DELETE, 'primary_key' => array ( array('id', 'row1') ), // Kondisi diperlukan untuk menghapus baris. RowExistenceExpectation.IGNORE melewati pemeriksaan keberadaan baris. 'condition' => RowExistenceExpectationConst::CONST_IGNORE ) ) );