Use the Tablestore SDK for Go to filter rows or paginate returned attribute columns on the server when you read Wide Column model data.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
A filter screens read results on the server and does not reduce the consumed read CUs. You can specify a filter in the query criteria of GetRow, GetRange, or BatchGetRow. The Tablestore SDK for Go supports the following filter types:
-
Single-column value filter (
SingleColumnCondition): evaluates the value of an attribute column. -
Single-column value regex filter (
SingleColumnValueRegexFilter): extracts a substring from a STRING attribute column, converts the substring, and compares the result. -
Composite filter (
CompositeColumnCondition): combines filters by using AND, OR, or NOT. A composite filter can contain up to 32 conditions. -
Column pagination filter (
PaginationFilter): returns attribute columns based on an offset and count without evaluating column values.
The following sample uses a single-column value filter to return only rows for which score >= 60.
filter := tablestore.NewSingleColumnCondition(
"score",
tablestore.CT_GREATER_EQUAL,
int64(60),
)
request.RangeRowQueryCriteria.Filter = filter
response, err := client.GetRange(request)
if err != nil {
log.Fatal(err)
}
fmt.Println(response.Rows)
Parameters
Single-column value filter
Call NewSingleColumnCondition(columnName, comparator, value) to create a single-column value filter. SingleColumnCondition contains the following parameters.
|
Name |
Type |
Description |
|
ColumnName (required) |
|
The attribute column to evaluate. |
|
Comparator (required) |
|
The comparison operator. Valid values: |
|
ColumnValue (required) |
|
The value to compare against. |
|
FilterIfMissing (optional) |
|
Specifies whether to filter out the row if the column is missing. Default value: |
|
LatestVersionOnly (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
Single-column value regex filter
Call NewSingleColumnValueRegexFilter(columnName, comparator, rule, value) to create a regex filter. Only STRING attribute columns support regex filters.
In addition to the parameters of a single-column value filter, specify a ValueTransferRule with the following parameters.
|
Name |
Type |
Description |
|
Regex (required) |
|
The regular expression used to extract a substring. Maximum length: 256 bytes.
|
|
Cast_type (required) |
|
The type to which the extracted substring is converted. Valid values: |
Composite filter
Call NewCompositeColumnCondition(lo) to create a composite filter, and call AddFilter to add subfilters.
|
Name |
Type |
Description |
|
Operator (required) |
|
The logical operator. Valid values: |
|
Filters (required) |
|
The subfilters to combine. The following types are supported:
|
Column pagination filter
PaginationFilter contains the following parameters.
|
Name |
Type |
Description |
|
Limit (required) |
|
The number of attribute columns to return. The value must be greater than 0. |
|
Offset (optional) |
|
The number of attribute columns to skip. The value must be greater than or equal to 0. Default value: |
Examples
Regex filter
The following sample extracts active from a profile column value in the level:active format and compares the result with a specified value.
rule := tablestore.NewValueTransferRule(
`level:([a-z]+)`,
tablestore.Variant_STRING,
)
filter := tablestore.NewSingleColumnValueRegexFilter(
"profile",
tablestore.CT_EQUAL,
rule,
"active",
)
request.SingleRowQueryCriteria.Filter = filter
Composite filter
The following sample returns rows for which score >= 60 and status is active.
filter := tablestore.NewCompositeColumnCondition(tablestore.LO_AND)
filter.AddFilter(
tablestore.NewSingleColumnCondition("score", tablestore.CT_GREATER_EQUAL, int64(60)),
)
filter.AddFilter(
tablestore.NewSingleColumnCondition("status", tablestore.CT_EQUAL, "active"),
)
request.RangeRowQueryCriteria.Filter = filter
Paginate returned attribute columns
The following sample skips the first attribute column and returns the next two attribute columns.
request.SingleRowQueryCriteria.Filter = &tablestore.PaginationFilter{
Offset: 1,
Limit: 2,
}