Topik ini menjelaskan cara menggunakan Go SDK untuk melakukan operasi batch—menulis, memperbarui, dan menghapus data—di Tablestore. Satu permintaan dapat menargetkan baris di beberapa tabel.
Precautions
Jika server mendeteksi kesalahan parameter pada salah satu operasi dalam permintaan, seluruh permintaan akan gagal dan tidak ada operasi yang dijalankan.
Satu operasi batch data dapat mencakup hingga 200 baris, dengan ukuran total semua baris tidak melebihi 4 MB.
Prerequisites
Method
func (tableStoreClient *TableStoreClient) BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse, error)Sample code
Contoh kode berikut menunjukkan cara menggunakan operasi batch data untuk menyisipkan satu baris ke dalam tabel test_table.
func BatchWriteRowSample(client *tablestore.TableStoreClient) {
// Buat permintaan.
batchWriteRequest := new(tablestore.BatchWriteRowRequest)
// Tambahkan PutRowChange.
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// Anda harus menetapkan kondisi untuk operasi ini. RowExistenceExpectation_IGNORE melewati pemeriksaan keberadaan baris.
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
batchWriteRequest.AddRowChange(putRowChange)
// Panggil metode batchWriteRow untuk menjalankan operasi batch.
response, err := client.BatchWriteRow(batchWriteRequest)
// Proses respons.
if err != nil {
fmt.Println("Batch write row gagal dengan error:", err)
} else {
fmt.Printf("RequestId: %s \n", response.RequestId)
for tableName, rows := range response.TableToRowsResult {
for _, row := range rows {
if !row.IsSucceed {
fmt.Printf("TableName: %s. Failed Rows: %v \n", tableName, row.Error)
}
}
}
}
}Contoh berikut menunjukkan berbagai jenis operasi data.
PutRowChange: Menulis satu baris.putPk := new(tablestore.PrimaryKey) putPk.AddPrimaryKeyColumn("id", "row1") putRowChange := new(tablestore.PutRowChange) putRowChange.TableName = "test_table" putRowChange.PrimaryKey = putPk // Anda harus menetapkan kondisi untuk operasi ini. RowExistenceExpectation_IGNORE melewati pemeriksaan keberadaan baris. putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(putRowChange)Tambahkan kolom atribut saat menulis baris.
// Tambahkan kolom atribut. putRowChange.AddColumn("col1", "val1") // Tambahkan kolom atribut dengan nomor versi data kustom. putRowChange.AddColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000))UpdateRowChange: Memperbarui satu baris. Anda dapat mengubah nilai kolom atribut, menambahkan kolom atribut, atau menghapus seluruh kolom atribut atau versi tertentu darinya.updatePk := new(tablestore.PrimaryKey) updatePk.AddPrimaryKeyColumn("id", "row1") updateRowChange := new(tablestore.UpdateRowChange) updateRowChange.TableName = "test_table" updateRowChange.PrimaryKey = updatePk updateRowChange.PutColumn("col1", "changed_val1") // Anda harus menetapkan kondisi untuk operasi ini. RowExistenceExpectation_IGNORE melewati pemeriksaan keberadaan baris. updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(updateRowChange)Anda juga dapat menambahkan atau menghapus kolom atribut selama pembaruan:
// Tambahkan kolom atribut. updateRowChange.PutColumn("col3", "val3") // Tambahkan kolom atribut dengan nomor versi data kustom. updateRowChange.PutColumnWithTimestamp("col4", "val4", int64(time.Now().Unix() * 1000)) // Hapus kolom atribut. updateRowChange.DeleteColumn("col2")DeleteRowChange: Menghapus satu baris.deletePk := new(tablestore.PrimaryKey) deletePk.AddPrimaryKeyColumn("id", "row1") deleteRowChange := new(tablestore.DeleteRowChange) deleteRowChange.TableName = "test_table" deleteRowChange.PrimaryKey = deletePk // Anda harus menetapkan kondisi untuk operasi ini. RowExistenceExpectation_IGNORE melewati pemeriksaan keberadaan baris. deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(deleteRowChange)