Skip to content

Aggregation功能说明

IHEII edited this page Aug 21, 2023 · 2 revisions

Aggregation接口说明

部分版本的Server可能不支持Aggregation功能, 同时目前仅支持单分区聚合

获取AggExecutor

type Client interface {
	// NewAggExecutor create an aggregate executor.
	NewAggExecutor(tableName string, rangePairs []*table.RangePair, opts ...option.ObQueryOption) AggExecutor
}

params

  • tableName: 表名
  • rangePairs: 聚合范围
  • opts: 可选参数, 支持部分的可选参数,按需填写

return

  • AggExecutor: AggExecutor执行体

AggExecutor

type AggExecutor interface {
	// Min add a min operation to the agg executor.
	Min(columnName string) AggExecutor
	// Max add a max operation to the agg executor.
	Max(columnName string) AggExecutor
	// Count add a count operation to the agg executor.
	Count() AggExecutor
	// Sum add a sum operation to the agg executor.
	Sum(columnName string) AggExecutor
	// Avg add an avg operation to the agg executor.
	Avg(columnName string) AggExecutor
	// Execute an agg operation.
	// AggregateResult contains the results of all operations.
	Execute(ctx context.Context) (AggregateResult, error)
}

接口说明

  • Min(columnName string): 求columnName列最小值
  • Max(columnName string): 求columnName列最大值
  • Count(columnName string): 求范围内的行数
  • Sum(columnName string): 求columnName列和
  • Avg(columnName string): 求columnName列均值
  • Execute(ctx context.Context): 执行该聚合操作

Demo

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)
	}

	startRowKey := []*table.Column{table.NewColumn("c1", int64(0)), table.NewColumn("c2", table.Min)}
	endRowKey := []*table.Column{table.NewColumn("c1", int64(100)), table.NewColumn("c2", table.Max)}
	keyRanges := []*table.RangePair{table.NewRangePair(startRowKey, endRowKey)}

	// create agg executor
	aggExecutor := cli.NewAggExecutor(tableName, keyRanges).Sum("c3").Min("c2").Max("c1").Count()

	// get agg result
	res, err := aggExecutor.Execute(context.TODO())

	if err != nil {
		panic(err)
	}

	// get agg result value
	println(res.Value("sum(c3)").(int64))
	println(res.Value("min(c2)").(int64))
	println(res.Value("max(c1)").(int64))
	println(res.Value("count(*)").(int64))
}

Demo获取c1在0-100范围内的:

  • c3列的和
  • c2列最小值
  • c1列最大值
  • 范围内的总共列数
Clone this wiki locally