Get an AggExecutor
The syntax is as follows:
type Client interface {
// NewAggExecutor create an aggregate executor.
NewAggExecutor(tableName string, rangePairs []*table.RangePair, opts ...option.ObQueryOption) AggExecutor
}
Input parameters:
tableName: the name of the table.rangePairs: the aggregation range.opts: the optional parameters, some of which are supported. You can specify these parameters as needed.
Response parameters:
AggExecutor: the aggregation executor.
AggExecutor
The syntax is as follows:
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)
}
Input parameters:
Min(columnName string): calculates the minimum value of the column specified bycolumnName.Max(columnName string): calculates the maximum value of the column specified bycolumnName.Count(columnName string): calculates the number of rows within the range.Sum(columnName string): calculates the sum of the column specified bycolumnName.Avg(columnName string): calculates the average value of the column specified bycolumnName.Execute(ctx context.Context): executes the aggregation operation.
Example
Get the sum of column c3, the minimum value of column c2, and the maximum value of column c1 for the rows whose c1 value is in the range of 0 to 100, as well as the total number of columns within the range.
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))
}