Filters class and types (comparison, dedicated and decorating)

When reading data from HBase using Get or Scan operations, you can use custom filters to return a subset of results to the client. While this does not reduce server-side IO, it does reduce network bandwidth and reduces the amount of data the client needs to process. Filters are generally used using the Java API, but can be used from HBase Shell for testing and debugging purposes.

HBase filters take zero or more arguments, in parentheses. Where the argument is a string, it is surrounded by single quotes (‘string’).

Filters can be combined together with logical operators. Some filters take a combination of comparison operators and comparators. Following is the list of each.

Logical Operators

  • AND – the key-value must pass both the filters to be included in the results.
  • OR – the key-value must pass at least one of the filters to be included in the results.
  • SKIP – for a particular row, if any of the key-values do not pass the filter condition, the entire row is skipped.
  • WHILE – For a particular row, it continues to emit key-values until a key-value is reached that fails the filter condition.
  • Compound Filters – Using these operators, a hierarchy of filters can be created. For example:

(Filter1 AND Filter2)OR(Filter3 AND Filter4)

Comparison Operators

  • LESS (<)
  • LESS_OR_EQUAL (<=)
  • EQUAL (=)
  • NOT_EQUAL (!=)
  • GREATER_OR_EQUAL (>=)
  • GREATER (>)
  • NO_OP (no operation)

Comparators

  • BinaryComparator – lexicographically compares against the specified byte array using the Bytes.compareTo(byte[], byte[]) method.
  • BinaryPrefixComparator – lexicographically compares against a specified byte array. It only compares up to the length of this byte array.
  • RegexStringComparator – compares against the specified byte array using the given regular expression. Only EQUAL and NOT_EQUAL comparisons are valid with this comparator.
  • SubStringComparator – tests whether or not the given substring appears in a specified byte array. The comparison is case insensitive. Only EQUAL and NOT_EQUAL comparisons are valid with this comparator.

Examples

Example1: >, ‘binary:abc’ will match everything that is lexicographically greater than “abc”

Example2: =, ‘binaryprefix:abc’ will match everything whose first 3 characters are lexicographically equal to “abc”

Example3: !=, ‘regexstring:ab*yz’ will match everything that doesn’t begin with “ab” and ends with “yz”

Example4: =, ‘substring:abc123’ will match everything that begins with the substring “abc123”

Compound Operators

Within an expression, parentheses can be used to group clauses together, and parentheses have the highest order of precedence.

SKIP and WHILE operators are next, and have the same precedence.

The AND operator is next.

The OR operator is next

Examples

A filter string of the form: “Filter1 AND Filter2 OR Filter3” will be evaluated as: “(Filter1 AND Filter2) OR Filter3”

A filter string of the form: “Filter1 AND SKIP Filter2 OR Filter3” will be evaluated as: “(Filter1 AND (SKIP Filter2)) OR Filter3”

Filter Types

HBase includes several filter types, as well as the ability to group filters together and create your own custom filters.

  • KeyOnlyFilter – takes no arguments. Returns the key portion of each key-value pair.

Syntax: KeyOnlyFilter ()

  • FirstKeyOnlyFilter – takes no arguments. Returns the key portion of the first key-value pair.

Syntax: FirstKeyOnlyFilter ()

  • PrefixFilter – takes a single argument, a prefix of a row key. It returns only those key-values present in a row that start with the specified row prefix

Syntax:  PrefixFilter (‘<row_prefix>’)

Example: PrefixFilter (‘Row’)

  • ColumnPrefixFilter – takes a single argument, a column prefix. It returns only those key-values present in a column that starts with the specified column prefix.

Syntax:  ColumnPrefixFilter (‘<column_prefix>’)

Example: ColumnPrefixFilter (‘Col’)

  • MultipleColumnPrefixFilter – takes a list of column prefixes. It returns key-values that are present in a column that starts with any of the specified column prefixes.

Syntax:  MultipleColumnPrefixFilter (‘<column_prefix>’, ‘<column_prefix>’, …, ‘<column_prefix>’)

Example: MultipleColumnPrefixFilter (‘Col1’, ‘Col2’)

  • ColumnCountGetFilter – takes one argument, a limit. It returns the first limit number of columns in the table.

Syntax:  ColumnCountGetFilter (‘<limit>’)

Example: ColumnCountGetFilter (4)

  • PageFilter – takes one argument, a page size. It returns page size number of rows from the table.

Syntax:  PageFilter (‘<page_size>’)

Example: PageFilter (2)

  • ColumnPaginationFilter – takes two arguments, a limit and offset. It returns limit number of columns after offset number of columns. It does this for all the rows.

Syntax:  ColumnPaginationFilter (‘<limit>’, ‘<offset>’)

Example: ColumnPaginationFilter (3, 5)

  • InclusiveStopFilter – takes one argument, a row key on which to stop scanning. It returns all key-values present in rows up to and including the specified row.

Syntax:  InclusiveStopFilter (‘<stop_row_key>’)

Example: InclusiveStopFilter (‘Row2’)

  • TimeStampsFilter – takes a list of timestamps. It returns those key-values whose timestamps matches any of the specified timestamps.

Syntax:  TimeStampsFilter (<timestamp>, <timestamp>, … ,<timestamp>)

Example: TimeStampsFilter (5985489, 48895495, 58489845945)

  • RowFilter – takes a compare operator and a comparator. It compares each row key with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that row.

Syntax:  RowFilter (<compareOp>, ‘<row_comparator>’)

Example: RowFilter (<=, ‘binary:xyz)

  • FamilyFilter – takes a compare operator and a comparator. It compares each family name with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that family.

Syntax:  FamilyFilter (<compareOp>, ‘<family_comparator>’)

Example: FamilyFilter (>=, ‘binaryprefix:FamilyB’)

  • QualifierFilter – takes a compare operator and a comparator. It compares each qualifier name with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that column.

Syntax:  QualifierFilter (<compareOp>, ‘<qualifier_comparator>’)

Example: QualifierFilter (=, ‘substring:Column1’)

  • ValueFilter – takes a compare operator and a comparator. It compares each value with the comparator using the compare operator and if the comparison returns true, it returns that key-value.

Syntax:  ValueFilter (<compareOp>, ‘<value_comparator>’)

Example: ValueFilter (!=, ‘binary:Value’)

  • DependentColumnFilter – takes two arguments required arguments, a family and a qualifier. It tries to locate this column in each row and returns all key-values in that row that have the same timestamp. If the row does not contain the specified column, none of the key-values in that row will be returned. The filter can also take an optional boolean argument, dropDependentColumn. If set to true, the column used for the filter does not get returned.

The filter can also take two more additional optional arguments, a compare operator and a value comparator, which are further checks in addition to the family and qualifier. If the dependent column is found, its value should also pass the value check. If it does pass the value check, only then is its timestamp taken into consideration.

Syntax:  DependentColumnFilter (‘<family>’, ‘<qualifier>’, <boolean>, <compare operator>, ‘<value comparator’)

DependentColumnFilter (‘<family>’, ‘<qualifier>’, <boolean>)

DependentColumnFilter (‘<family>’, ‘<qualifier>’)

Example: DependentColumnFilter (‘conf’, ‘blacklist’, false, >=, ‘zebra’)

DependentColumnFilter (‘conf’, ‘blacklist’, true)

DependentColumnFilter (‘conf’, ‘blacklist’)

  • SingleColumnValueFilter – takes a column family, a qualifier, a compare operator and a comparator. If the specified column is not found, all the columns of that row will be emitted. If the column is found and the comparison with the comparator returns true, all the columns of the row will be emitted. If the condition fails, the row will not be emitted.

This filter also takes two additional optional boolean arguments, filterIfColumnMissing and setLatestVersionOnly.

If the filterIfColumnMissing flag is set to true, the columns of the row will not be emitted if the specified column to check is not found in the row. The default value is false.

If the setLatestVersionOnly flag is set to false, it will test previous versions (timestamps) in addition to the most recent. The default value is true.

These flags are optional and dependent on each other. You must set neither or both of them together.

Syntax:  SingleColumnValueFilter (‘<family>’, ‘<qualifier>’, <compare operator>, ‘<comparator>’, <filterIfColumnMissing_boolean>, <latest_version_boolean>)

Syntax:  SingleColumnValueFilter (‘<family>’, ‘<qualifier>’, <compare operator>, ‘<comparator>’)

Example: SingleColumnValueFilter (‘FamilyA’, ‘Column1’, <=, ‘abc’, true, false)

Example: SingleColumnValueFilter (‘FamilyA’, ‘Column1’, <=, ‘abc’)

SingleColumnValueExcludeFilter – takes the same arguments and behaves same as SingleColumnValueFilter. However, if the column is found and the condition passes, all the columns of the row will be emitted except for the tested column value.

Syntax:  SingleColumnValueExcludeFilter (<family>, <qualifier>, <compare operators>, <comparator>, <latest_version_boolean>, <filterIfColumnMissing_boolean>)

Syntax:  SingleColumnValueExcludeFilter (<family>, <qualifier>, <compare operator> <comparator>)

Example: SingleColumnValueExcludeFilter (‘FamilyA’, ‘Column1’, ‘<=’, ‘abc’, ‘false’, ‘true’)

Example: SingleColumnValueExcludeFilter (‘FamilyA’, ‘Column1’, ‘<=’, ‘abc’)

  • ColumnRangeFilter – takes either minColumn, maxColumn, or both. Returns only those keys with columns that are between minColumn and maxColumn. It also takes two boolean variables to indicate whether to include the minColumn and maxColumn or not. If you don’t want to set the minColumn or the maxColumn, you can pass in an empty argument.

Syntax:  ColumnRangeFilter (‘<minColumn >’, <minColumnInclusive_bool>, ‘<maxColumn>’, <maxColumnInclusive_bool>)

Example: ColumnRangeFilter (‘abc’, true, ‘xyz’, false)

  • Custom Filter – You can create a custom filter by implementing the Filter class. The JAR must be available on all RegionServers.

HBase Shell Example

This example scans the ‘users’ table for rows where the contents of the cf:name column equals the string ‘abc’.

hbase> scan ‘users’, { FILTER => SingleColumnValueFilter.new(Bytes.toBytes(‘cf’),

Bytes.toBytes(‘name’), CompareFilter::CompareOp.valueOf(‘EQUAL’),

BinaryComparator.new(Bytes.toBytes(‘abc’)))}

Apply for HBase Certification

https://www.vskills.in/certification/certified-hbase-professional

Back to Tutorials

Get industry recognized certification – Contact us

Menu