Create a batch executor
The syntax is as follows:
// NewBatchExecutor create a batch executor.
NewBatchExecutor(tableName string) BatchExecutor
Input parameters:
tableName: the name of the table.
Response parameters:
BatchExecutor: the batch executor.
BatchExecutor API
The syntax is as follows:
type BatchExecutor interface {
// AddInsertOp add an insert operation to the batch executor.
AddInsertOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddUpdateOp add an update operation to the batch executor.
AddUpdateOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddInsertOrUpdateOp add an insertOrUpdate operation to the batch executor
AddInsertOrUpdateOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddReplaceOp add a replace operation to the batch executor
AddReplaceOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddIncrementOp add an increment operation to the batch executor
AddIncrementOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddAppendOp add an append operation to the batch executor
AddAppendOp(rowKey []*table.Column, mutateValues []*table.Column, opts ...ObkvOption) error
// AddDeleteOp add a delete operation to the batch executor
AddDeleteOp(rowKey []*table.Column, opts ...ObkvOption) error
// AddGetOp add a get operation to the batch executor.
AddGetOp(rowKey []*table.Column, getColumns []string, opts ...ObkvOption) error
// Execute a batch operation.
// batch operation only ensures atomicity of a single partition.
// BatchOperationResult contains the results of all operations.
Execute(ctx context.Context) (BatchOperationResult, error)
}
Input parameters:
AddInsertOp: adds the Insert operation.AddUpdateOp: adds the Update operation.AddInsertOrUpdateOp: adds the InsertOrUpdate operation.AddReplaceOp: adds the Replace operation.AddIncrementOp: adds the Increment operation.AddAppendOp: adds the Append operation.AddDeleteOp: adds the Delete operation.AddGetOp: adds the Get operation.Execute: executes multiple operations at a time. The execution result is returned inBatchOperationResult.
The syntax of BatchOperationResult is as follows:
type BatchOperationResult interface {
// IsEmptySet result is empty or not.
IsEmptySet() bool
// GetResults get all results.
GetResults() []SingleResult
// Size batch operation size.
Size() int
// SuccessIdx indexes of successful operation.
SuccessIdx() []int
// ErrorIdx indexes of unsuccessful operation.
ErrorIdx() []int
}
Input parameters:
IsEmptySet: indicates whether the result is empty.GetResults: the result array.Size: the number of results.SuccessIdx: the sequence number of a successful operation.ErrorIdx: the sequence number of a failed operation.
Example
// CREATE TABLE test(c1 bigint, c2 bigint, PRIMARY KEY(c1)) PARTITION BY hash(c1) partitions 2;
func main() {
const (
configUrl = "xxx"
fullUserName = "user@tenant#cluster"
passWord = ""
sysUserName = "sysUser"
sysPassWord = ""
tableName = "test"
)
cfg := config.NewDefaultClientConfig()
cli, err := client.NewClient(configUrl, fullUserName, passWord, sysUserName, sysPassWord, cfg)
if err != nil {
panic(err)
}
batchExecutor := cli.NewBatchExecutor(tableName)
rowKey1 := []*table.Column{table.NewColumn("c1", int64(1))}
rowKey2 := []*table.Column{table.NewColumn("c1", int64(2))}
selectColumns1 := []string{"c1"}
selectColumns2 := []string{"c2"}
err = batchExecutor.AddGetOp(rowKey1, selectColumns1)
if err != nil {
panic(err)
}
err = batchExecutor.AddGetOp(rowKey2, selectColumns2)
if err != nil {
panic(err)
}
ctx, _ := context.WithTimeout(context.Background(), time.Duration(1000)*time.Millisecond) // 1000ms
res, err := batchExecutor.Execute(ctx)
if err != nil {
panic(err)
}
if res.Size() != 2 {
panic(err)
}
println(res.GetResults()[0].Entity().GetProperty("c1"))
println(res.GetResults()[1].Entity().GetProperty("c2"))
}
- In this example, two Get operations are executed at a time.
- Two results are returned. Result 0 contains the value of
c1, and Result 1 contains the value ofc2.