Skip to content

批量操作说明

IHEII edited this page Aug 21, 2023 · 3 revisions

批量操作

创建批量执行器

// NewBatchExecutor create a batch executor.
NewBatchExecutor(tableName string) BatchExecutor

param

  • tableName: 表名

return

  • BatchExecutor: 批量执行器

BatchExecutor接口

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)
}
  • AddInsertOp: 添加insert操作
  • AddUpdateOp: 添加update操作
  • AddInsertOrUpdateOp: 添加insertOrUpdate操作
  • AddReplaceOp: 添加replace操作
  • AddIncrementOp: 添加increment操作
  • AddAppendOp: 添加append操作
  • AddDeleteOp: 添加delete操作
  • AddGetOp: 添加get操作
  • Execute: 执行批量操作, 执行的结果为BatchOperationResult
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
}
  • IsEmptySet: 结果是否为空
  • GetResults: 获取结果数组
  • Size: 获取结果数量
  • SuccessIdx: 获取成功操作序号
  • ErrorIdx: 获取失败操作序号

demo

// 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"))
}
  • 这是一个multi get的demo, 一个batch操作中包含了两个get操作
  • demo中, 返回的结果有两个, 我们从结果0中获取c1的值, 从结果1中获取c2的值
Clone this wiki locally