The Tablestore SDK for Java filters rows by column value or returns a slice of attribute columns on the server to reduce the amount of data transferred to the client.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Description
A filter runs on the server after each row is read and returns only the rows that match your criteria. Because filtering happens after the read, it does not reduce the number of rows scanned, but it does reduce the volume of data sent over the network.
Set a filter by calling setFilter on SingleRowQueryCriteria, RangeRowQueryCriteria, MultiRowQueryCriteria, or RangeIteratorParameter. The following filter types are available:
-
SingleColumnValueFilter: compares the value of one property column against a target value with a relational operator.
-
SingleColumnValueRegexFilter: extracts a substring from a
Stringproperty column with a regular expression, converts it to a target type, and compares it against a target value. -
CompositeColumnValueFilter: combines multiple filters with logical operators (
AND,OR, orNOT). A composite filter supports up to 32 sub-conditions. -
ColumnPaginationFilter: returns attribute columns based on an offset and a limit without evaluating column values.
new SingleColumnValueFilter(columnName, operator, columnValue)
new SingleColumnValueRegexFilter(columnName, regexRule, operator, columnValue)
new CompositeColumnValueFilter(logicOperator)
new ColumnPaginationFilter(limit, offset)
The following example reads rows from the filter_demo table where the col1 column equals val1 by using SingleColumnValueFilter.
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria("filter_demo");
PrimaryKeyBuilder startBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
criteria.setInclusiveStartPrimaryKey(startBuilder.build());
PrimaryKeyBuilder endBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endBuilder.build());
criteria.setMaxVersions(1);
// Build the filter: col1 == "val1"
SingleColumnValueFilter filter = new SingleColumnValueFilter(
"col1",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val1"));
criteria.setFilter(filter);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("Matched rows: " + response.getRows().size());
Parameters
Single-column value filter
The constructor of SingleColumnValueFilter contains the following parameters.
|
Name |
Type |
Description |
|
columnName (required) |
String |
The name of the property column to evaluate. |
|
operator (required) |
CompareOperator |
The relational operator. Valid values:
|
|
columnValue (required) |
ColumnValue |
The value used in the comparison. |
|
passIfMissing (optional) |
boolean |
Whether to return rows that do not contain the target column. Default Set to |
|
latestVersionsOnly (optional) |
boolean |
Whether to evaluate only the latest version of the column. Default Set to |
Regular expression filter
The constructor of SingleColumnValueRegexFilter contains the following parameters. If you specify a regex rule, the target attribute column must be of the String type.
|
Name |
Type |
Description |
|
columnName (required) |
String |
The name of the attribute column to evaluate. If |
|
regexRule (optional) |
RegexRule |
The regex matching rule. If specified, the filter extracts a substring from the string column value, converts the substring, and then evaluates it. If omitted, the filter evaluates the original column value. The rule contains the following parameters:
|
|
operator (required) |
CompareOperator |
The evaluation operator. Valid values: |
|
columnValue (optional) |
ColumnValue |
The comparison value. This parameter is required for the six relational operators and must be omitted for |
|
latestVersionsOnly (optional) |
boolean |
Specifies whether to evaluate only the latest version of the attribute column. Default value: |
Composite filter
The constructor and sub-filter list of CompositeColumnValueFilter contain the following parameters. Add sub-filters by calling addFilter(). A composite filter supports up to 32 sub-conditions.
|
Name |
Type |
Description |
|
type (required) |
LogicOperator |
The logical operator. Valid values:
|
|
filters (required) |
List<ColumnValueFilter> |
The sub-filters combined by the logical operator. Add each sub-filter with |
Attribute column pagination filter
The constructor of ColumnPaginationFilter contains the following parameters. If offset is omitted, the default value is 0.
|
Name |
Type |
Description |
|
limit (required) |
int |
The number of attribute columns to return. The value must be greater than 0. |
|
offset (optional) |
int |
The zero-based offset of the first attribute column to return. The value must be greater than or equal to 0. Default value: |
Examples
Compare a substring extracted with a regular expression
Use RegexRule to extract a substring from a column value, then compare the substring against a target value. The following example applies the regex 1([a-z]+)5 to col2, captures the first group, and compares it against the string aaa.
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria("filter_demo");
PrimaryKeyBuilder startBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
criteria.setInclusiveStartPrimaryKey(startBuilder.build());
PrimaryKeyBuilder endBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endBuilder.build());
criteria.setMaxVersions(1);
// The regex "1([a-z]+)5" captures the first group; castType=VT_STRING compares the result as a string.
RegexRule regexRule = new RegexRule("1([a-z]+)5", RegexRule.CastType.VT_STRING);
SingleColumnValueRegexFilter filter = new SingleColumnValueRegexFilter(
"col2",
regexRule,
SingleColumnValueRegexFilter.CompareOperator.EQUAL,
ColumnValue.fromString("aaa"));
criteria.setFilter(filter);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("Matched rows: " + response.getRows().size());
Combine multiple filters with logical operators
Use CompositeColumnValueFilter to combine multiple filters with logical operators. Composite filters can be nested. The following example builds the condition (col1 == "val1" OR cast<String>(reg(col2)) >= "aaa") AND col3 == "val3".
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria("filter_demo");
PrimaryKeyBuilder startBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
startBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"));
criteria.setInclusiveStartPrimaryKey(startBuilder.build());
PrimaryKeyBuilder endBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
endBuilder.addPrimaryKeyColumn("id", PrimaryKeyValue.INF_MAX);
criteria.setExclusiveEndPrimaryKey(endBuilder.build());
criteria.setMaxVersions(1);
// Leaf 1: col1 == "val1"
SingleColumnValueFilter leaf1 = new SingleColumnValueFilter(
"col1",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val1"));
// Leaf 2: cast<String>(reg(col2)) >= "aaa"
RegexRule regexRule = new RegexRule("1([a-z]+)5", RegexRule.CastType.VT_STRING);
SingleColumnValueRegexFilter leaf2 = new SingleColumnValueRegexFilter(
"col2",
regexRule,
SingleColumnValueRegexFilter.CompareOperator.GREATER_EQUAL,
ColumnValue.fromString("aaa"));
// OR combination: leaf1 OR leaf2
CompositeColumnValueFilter orFilter = new CompositeColumnValueFilter(
CompositeColumnValueFilter.LogicOperator.OR);
orFilter.addFilter(leaf1);
orFilter.addFilter(leaf2);
// Leaf 3: col3 == "val3"
SingleColumnValueFilter leaf3 = new SingleColumnValueFilter(
"col3",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val3"));
// AND combination: (leaf1 OR leaf2) AND leaf3
CompositeColumnValueFilter andFilter = new CompositeColumnValueFilter(
CompositeColumnValueFilter.LogicOperator.AND);
andFilter.addFilter(orFilter);
andFilter.addFilter(leaf3);
criteria.setFilter(andFilter);
GetRangeResponse response = client.getRange(new GetRangeRequest(criteria));
System.out.println("Matched rows: " + response.getRows().size());
Control evaluation of missing columns and historical versions
Use setPassIfMissing to control whether rows that do not contain the target column are returned, and setLatestVersionsOnly to control whether historical versions are evaluated as well.
SingleColumnValueFilter filter = new SingleColumnValueFilter(
"col1",
SingleColumnValueFilter.CompareOperator.EQUAL,
ColumnValue.fromString("val1"));
// Skip rows that do not contain col1 (default: include such rows).
filter.setPassIfMissing(false);
// Evaluate all versions; return the row if any version matches (default: evaluate only the latest version).
filter.setLatestVersionsOnly(false);
criteria.setFilter(filter);
Return a page of attribute columns
Use ColumnPaginationFilter to return a specified number of attribute columns starting at an offset. The following example skips the first attribute column and returns the next two.
PrimaryKey primaryKey = PrimaryKeyBuilder.createPrimaryKeyBuilder()
.addPrimaryKeyColumn("id", PrimaryKeyValue.fromString("row1"))
.build();
SingleRowQueryCriteria criteria = new SingleRowQueryCriteria("filter_demo", primaryKey);
criteria.setMaxVersions(1);
criteria.setFilter(new ColumnPaginationFilter(2, 1));
GetRowResponse response = client.getRow(new GetRowRequest(criteria));
for (Column column : response.getRow().getColumns()) {
System.out.println(column.getName());
}