All Products
Search
Document Center

Tablestore:Use a filter

Last Updated:Aug 23, 2024

Tablestore filters query results on the server and then returns the filtered results. Only the rows that meet the filter conditions are returned.

Prerequisites

Introduction

When you call the GetRow, BatchGetRow, or GetRange operation to query data, you can use a filter to return only the rows that meet the filter conditions.

Filters include SingleColumnValueFilter and CompositeColumnValueFilter.

  • SingleColumnValueFilter: determines whether to return a row based only on the value of a reference column.

  • CompositeColumnValueFilter: determines whether to return a row based on a combination of filter conditions for values of multiple reference columns.

Note

For more information about filters, see Configure a filter.

Usage notes

  • Filter conditions support relational operators of =, !=, >, >=, <, and <= and logical operators of NOT, AND, and OR. A filter condition can include up to 10 subconditions.

  • The reference columns that are used by a filter must be included in the query results. If the specified columns from which data is read do not include reference columns, the filter cannot query the values of reference columns.

  • When you use the GetRange operation, up to 5,000 rows or 4 MB of data can be scanned at a time.

    If no data matches the filter conditions in the range of the scan, the returned rows are empty. However, NextStartPrimaryKey may not be empty. If NextStartPrimaryKey is not empty, use the parameter value to continue scanning until the return value of NextStartPrimaryKey is empty.

Parameters

Parameter

Description

columnName

The name of the reference column used by the filter.

columnValue

The value of the reference column used by the filter.

ComparatorType

The relational operators used by the filter. For more information about relational operators, see ComparatorType.

The following relational operators are supported: EQUAL (=), NOT_EQUAL (!=), GREATER_THAN (>), GREATER_EQUAL (>=), LESS_THAN (<), and LESS_EQUAL (<=).

LogicOperator

The logical operators used by the filter. For information about logical operators, see LogicalOperator.

The following logical operators are supported: NOT, AND, and OR.

passIfMissing

Specifies whether to return a row if a reference column does not exist in the row. The data type of the parameter value is Boolean. Valid values:

  • true (default): If a reference column does not exist in a row, the row is returned.

  • false: If a reference column does not exist in a row, the row is not returned.

latestVersionOnly

Specifies whether to use only the latest version of data in a reference column for comparison when the reference column contains multiple versions of data. Type: Boolean. Valid values:

  • true: If a reference column contains multiple versions of data, only the latest version of data is used for comparison. This is the default value.

  • false: If a reference column contains multiple versions of data, all versions of data are used for comparison. If one version of data in the reference column meets the filter conditions, the row is returned.

Examples

Use SingleColumnValueFilter to filter data

The following sample code shows how to return data rows whose data is Tablestore in column col1 in a data table.

function getRowWithCondition() {
  // Specify that the row is returned when the value of the col1 column is Tablestore. When passIfMissing is set to true, the row is returned if this column does not exist. When passIfMissing is set to false, the row is not returned if this column does not exist. 
  var condition = new TableStore.SingleColumnCondition('col1', 'Tablestore', TableStore.ComparatorType.EQUAL,true);

  params.columnFilter = condition;
  client.getRow(params, function (err, data) {
    if (err) {
      console.log('error:', err);
      return;
    }
    console.log('success:', data);
  });
}

Use CompositeColumnValueFilter to filter data

The following sample code shows how to return data rows whose data is Tablestore in column col1 and 123456789 in column col5 in a data table.

function getRowWithCompositeCondition() {
  // Specify that the row is returned when the value of the col1 column is Tablestore and the value of the col5 column is 123456789. 
  var condition = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
  condition.addSubCondition(new TableStore.SingleColumnCondition('col1', 'Tablestore', TableStore.ComparatorType.EQUAL));
  condition.addSubCondition(new TableStore.SingleColumnCondition('col5', Long.fromNumber(123456789), TableStore.ComparatorType.EQUAL));

  params.columnFilter = condition;
  client.getRow(params, function (err, data) {
    if (err) {
      console.log('error:', err);
      return;
    }
    console.log('success:', data);
  });
}

References

  • If an application needs to use different attribute columns as query conditions to query data, you can specify the attribute columns as the primary key columns of a secondary index. This way, you can use the secondary index to query data based on the attribute columns. This accelerates data queries. For more information, see Secondary Index.

  • If your business requires multi-dimensional queries and data analysis, you can create a search index and specify the required attribute columns as the fields of the search index. Then, you can query and analyze data by using the search index. For example, you can use a search index to perform queries based on non-primary key columns, Boolean queries, and fuzzy queries. You can also use a search index to obtain the maximum and minimum values, collect statistics about the number of rows, and group query results. For more information, see Search index.

  • You can use the SQL query feature to query and analyze data in a table. For more information, see Query data.