All Products
Search
Document Center

Tablestore:Pembaruan batch data

Last Updated:Mar 31, 2026

Lakukan operasi penulisan batch untuk menambahkan, memperbarui, dan menghapus data di beberapa tabel Tablestore dalam satu permintaan menggunakan Python SDK.

Usage notes

  • Jika terdapat kesalahan parameter pada salah satu operasi dalam permintaan, server akan melemparkan exception dan tidak ada operasi yang dieksekusi.

  • Satu permintaan penulisan batch mendukung hingga 200 baris, dengan ukuran total data dari semua baris tidak boleh melebihi 4 MB.

  • Jika Anda memanggil metode add beberapa kali untuk tabel yang sama dalam satu permintaan, hanya pemanggilan terakhir yang berlaku.

Prerequisites

Initialize Tablestore Client

Method

def batch_write_row(self, request)

Parameter permintaan

  • items (Required) List[TableInBatchWriteRowItem]: Daftar operasi yang akan dilakukan. Setiap item dalam daftar menargetkan tabel tertentu dan berisi parameter berikut:

    Parameter

    Type

    Description

    table_name (Required)

    str

    Nama tabel.

    row_items (Required)

    RowItem

    Operasi data yang akan dilakukan pada tabel, termasuk operasi put, update, dan delete.

  • transaction_id (Optional) str: ID unik dari transaksi lokal. Untuk informasi selengkapnya, lihat Local Transaction.

Sample code

Kode contoh berikut menyisipkan satu baris ke dalam tabel test_table menggunakan operasi penulisan batch.

try:
    # Buat permintaan.
    request = BatchWriteRowRequest()

    put_row_items = []
    # Buat PutRowItem.
    put_pk = [('id', 'row1')]
    put_columns = []
    put_row = Row(put_pk, put_columns)
    # Anda harus menentukan kondisi untuk operasi tulis. RowExistenceExpectation.IGNORE berarti tidak dilakukan pemeriksaan keberadaan baris.
    put_row_item = PutRowItem(put_row, Condition(RowExistenceExpectation.IGNORE))
    put_row_items.append(put_row_item)

    request.add(TableInBatchWriteRowItem('test_table', put_row_items))

    # Panggil metode batch_write_row untuk menjalankan operasi batch.
    response = client.batch_write_row(request)

    # Proses respons.
    # put row
    for table_name,put_rows in response.table_of_put.items():
        for item in put_rows:
            if not item.is_ok:
                print('TableName: %s. Gagal menambahkan baris, Pesan error: %s' % (table_name, item.error_message))
    # update row
    for table_name, update_rows in response.table_of_update.items():
        for item in update_rows:
            if not item.is_ok:
                print('TableName: %s. Gagal memperbarui baris, Pesan error: %s.' % (table_name, item.error_message))
    # delete row
    for table_name, delete_rows in response.table_of_delete.items():
        for item in delete_rows:
            if not item.is_ok:
                print('TableName: %s. Gagal menghapus baris, Pesan error: %s.' % (table_name, item.error_message))
except Exception as e:
    print('Penulisan batch baris gagal dengan error: %s' % e)

Contoh berikut menunjukkan berbagai jenis operasi data.

  • PutRowItem: Menambahkan data baris.

    put_row_items = []
    # Buat PutRowItem.
    put_pk = [('id', 'row1')]
    put_columns = []
    put_row = Row(put_pk, put_columns)
    # Anda harus menentukan kondisi untuk operasi tulis. RowExistenceExpectation.IGNORE berarti tidak dilakukan pemeriksaan keberadaan baris.
    put_row_item = PutRowItem(put_row, Condition(RowExistenceExpectation.IGNORE))
    put_row_items.append(put_row_item)
    
    request.add(TableInBatchWriteRowItem('test_table', put_row_items))

    Tambahkan kolom atribut saat menambahkan data baris.

    # Kolom atribut
    put_columns = [('col1','val1')]
    # Kolom atribut dengan timestamp kustom
    put_columns = [('col1','val1', int(time.time() * 1000))]
  • UpdateRowItem: Memperbarui data baris. Gunakan item ini untuk mengubah nilai kolom atribut, menambahkan kolom atribut, menghapus versi tertentu dari kolom atribut, atau menghapus seluruh kolom atribut.

    update_row_items = []
    # Buat UpdateRowItem.
    update_pk = [('id', 'row1')]
    update_columns = {
        'PUT': [('col1', 'changed_val1')]
    }
    update_row = Row(update_pk, update_columns)
    # Anda harus menentukan kondisi untuk operasi pembaruan. RowExistenceExpectation.IGNORE berarti tidak dilakukan pemeriksaan keberadaan baris.
    update_row_item = UpdateRowItem(update_row, Condition(RowExistenceExpectation.IGNORE))
    update_row_items.append(update_row_item)
    
    request.add(TableInBatchWriteRowItem('test_table', update_row_items))

    Tambahkan atau hapus kolom atribut saat memperbarui data baris.

    # Tambahkan kolom atribut.
    update_columns = {
        'PUT': [('col3', 'val3')]
    }
    # Tambahkan kolom atribut dengan timestamp kustom.
    update_columns = {
        'PUT': [('col4', 'val4', int(time.time() * 1000))]
    }
    # Hapus kolom atribut.
    update_columns = {
        'DELETE_ALL': ['col2']
    }
  • DeleteRowItem: Menghapus data baris.

    delete_row_items = []
    # Buat DeleteRowItem.
    delete_pk = [('id', 'row1')]
    delete_row = Row(delete_pk, [])
    # Anda harus menentukan kondisi untuk operasi penghapusan. RowExistenceExpectation.IGNORE berarti tidak dilakukan pemeriksaan keberadaan baris.
    delete_row_item = DeleteRowItem(delete_row, Condition(RowExistenceExpectation.IGNORE))
    delete_row_items.append(delete_row_item)
    
    request.add(TableInBatchWriteRowItem('test_table', delete_row_items))
  • Contoh melakukan beberapa operasi pada satu tabel

    request = BatchWriteRowRequest()
    table_batch_items = TableInBatchWriteRowItem('<TABLE_NAME>', put_row_items)
    table_batch_items.row_items += delete_row_items
    request.add(table_batch_items)

Related documents