Get a query iterator
The syntax is as follows:
type Client interface {
// Query records by rangePairs.
Query(ctx context.Context, tableName string, rangePairs []*table.RangePair, opts ...option.ObQueryOption) (QueryResultIterator, error)
}
Input parameters:
ctx: the API timeout period.tableName: the name of the table.rangePairs: the primary key.opts: the additional options. This parameter is optional.
Response parameters:
QueryResultIterator: the query iterator to obtain the result.error: the error code and error message.
ObQueryOption
The syntax is as follows:
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
}
Note
You can set most available parameters in the format of option.WithQuery+corresponding parameter(). You can set the Limit parameter in the format of option.WithQueryLimit().
Input parameters:
WithQueryFilter: the query filter.WithQuerySelectColumns: the names of the columns whose values need to be returned. The names of the columns that use filters and the columns whose values need to be returned must be included.WithQueryIndexName: the index name. The default value is the primary key.WithQueryBatchSize: the maximum number of results carried in a packet during network communication.WithQueryMaxResultSize: the maximum number of packets to be returned.WithQueryLimit: the maximum number of results for a partition.WithQueryOffset: the result sequence number starting from which results are returned.WithQueryScanOrder: the scan order.
QueryResultIterator
The syntax is as follows:
type QueryResultIterator interface {
IsClosed() bool
Close()
Next() (QueryResult, error)
NextBatch() ([]QueryResult, error)
}
Input parameters:
IsClosed(): specifies whether to disable the iterator.Close(): specifies to disable the iterator.Next(): specifies to obtain the next output. The result is of the QueryResult type. This parameter cannot be used withNextBatch().NextBatch(): specifies to obtain the next batch of output. The result type is a QueryResult array. This parameter cannot be used withNext().
QueryResult
The syntax is as follows:
type QueryResult interface {
Value(columnName string) interface{}
Values() []interface{}
}
Input parameters:
Value(columnName string): obtains the values of the column specified bycolumnName. If the column does not exist,nilis returned.Values(): obtains the values of a row.
Example
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)
Note
All rows whose c1 values are within the range of 0 to 100 are scanned, and then the values of the c1, c2, and c3 columns of the rows that meet the following conditions are returned: c2 > 10 and c2 < 30.