-
Notifications
You must be signed in to change notification settings - Fork 13
Query说明
IHEII edited this page Aug 21, 2023
·
2 revisions
部分版本的Server可能不支持某些可选参数
type Client interface {
// Query records by rangePairs.
Query(ctx context.Context, tableName string, rangePairs []*table.RangePair, opts ...option.ObQueryOption) (QueryResultIterator, error)
}
param
- ctx: 用于控制接口超时
- tableName: 表名
- rangePairs: 主键
- opts: 扩展项,按需填写
return
- QueryResultIterator: Query迭代器, 用于获取结果
- error: 错误信息, 包括错误码和错误message
type ObQueryOptions struct {
QueryFilter filter.ObTableFilter
HTableFilter hfilter.ObHTableFilter
SelectColumns []string
IndexName string
BatchSize int32
MaxResultSize int64
Limit int32
Offset int32
ScanOrder table.ScanOrder
IsHbaseQuery bool
KeyValueMode table.ObKeyValueMode
}
大多数可用的参数都可以用option.WithQuery+相应参数()
即可设置,如想设置Limit
,应该使用option.WithQueryLimit()
- WithQueryFilter: Query过滤Filter
- WithQuerySelectColumns: 需要返回结果的列名, 需要包含使用Filter的列及想返回的列名
- WithQueryIndexName: 使用索引名, 默认主键
- WithQueryBatchSize: 网络通信中一个包所携带最大的结果数量
- WithQueryMaxResultSize: 最大返回包数
- WithQueryLimit: 每个分区的最大结果数量
- WithQueryOffset: 从几个结果开始返回
- WithQueryScanOrder: 扫描顺序
type QueryResultIterator interface {
IsClosed() bool
Close()
Next() (QueryResult, error)
NextBatch() ([]QueryResult, error)
}
- IsClosed(): 迭代器是否关闭
- Close(): 关闭迭代器
- Next(): 获取下一个输出, 结果类型为QueryResult, 不能与NextBatch()混用
- NextBatch(): 获取下一批输出, 结果类型为QueryResult数组, 不能与Next()混用
type QueryResult interface {
Value(columnName string) interface{}
Values() []interface{}
}
- Value(columnName string): 获取columnName这一列的值, 若不存在该列则返回nil
- Values(): 获取一行的值
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)}
lt30 := filter.CompareVal(filter.LessThan, "c2", int64(30))
gt10 := filter.CompareVal(filter.GreaterThan, "c2", int64(10))
filterList := filter.AndList(lt30, gt10)
resSet, err := cli.Query(
context.TODO(),
tableName,
keyRanges,
option.WithQuerySelectColumns([]string{"c1", "c2", "c3"}),
option.WithQueryBatchSize(batchSize),
option.WithQueryFilter(filterList),
)
assert.Equal(t, nil, err)
i := 0
res, err := resSet.Next()
for ; res != nil && err == nil; res, err = resSet.Next() {
assert.EqualValues(t, res.Value("c1"), res.Value("c2"))
assert.EqualValues(t, "hello", res.Value("c3"))
i++
}
assert.Equal(t, nil, err)
assert.EqualValues(t, 19, i)
说明: Demo首先扫描了c1在0-100的所有行, 然后过滤出c2>10且c2<30的所有行,返回了这些行的c1, c2, c3列