HBase- filters (various filters and code implementation)

Filter introduction

HBaseFilters provide a very powerful feature to help users improve the efficiency of processing data in their tables.

HBaseTwo of the main data reading functions are get and scan, both of which support direct access to data and access data by specifying start and stop key. You can add more restrictions to the query to reduce the amount of data you get. These restrictions can specify column families, columns, timestamps, and version numbers.

All filters take effect on the server side, called predicate push-down, which ensures that the filtered data is not transmitted to the client. Filtering can also be implemented in client code (but it affects system performance) because of this situationThe server side needs to transmit more data to the client. Users should avoid this situation as much as possible.

Filter hierarchy

At the bottom of the filter hierarchy are the Filter and FilterBase Abstract classes, which implement the shell and skeleton of the filter, allowing the actual filter class to avoid a lot of duplicate structural code.

Filters can act on the entire line of data inserted by the user, so the user can decide how to handle this line based on any available information. This information includes row keys, column names, actual column values and timestamps.

Comparison operator

Considering that the filter inherited from CompareFilter has one more compare method than the base class FilterBase, it needs to define the comparison operation using the incoming parameters.

operation

describe

LESS

Matching values less than set values

LESS_OR_EQUAL

Matching value less than or equal to the set value.

EQUAL

Matching is equal to setting the value of a value.

NOT_EQUAL

Matches the value that is not equal to the set value.

GREATER_OR_EQUAL

Matching value greater than or equal to the set value.

GREATER

Matching value greater than set value

NO_OP

Exclude all values

 

 

comparator

CompareFilterThe second type required is the comparator, which provides a variety of ways to compare different key values. The comparators are inherited from WritableByteArrayComparable, WritableByteArrayCom.Parable implements Writable and Comparable interfaces.

comparator

describe

BinaryComparator

Using Bytes.compareTo to compare current values and thresholds

BinaryPrefixComparator

Use Bytes.compareTo to match, but start with prefix matching from the left side.

NullComparator

Do not match, only to determine whether the current value is null?

BitComparator

Performs bit level comparisons through the bitwise and (AND) / (OR) XOR (XOR) operations provided by the BitwiseOp class.

RegexStringComparator

According to a regular expression, when the comparator is instantiated, it matches the data in the table.

SubstringComparator

The threshold and the data in the table are treated as string instances, and the strings are matched by contains operation.

Note: The latter three comparators, BitComparator, RegexStringComparator, Substring Comparator, can only be used with the EQUAL and NOT_EQUAL operators because of these comparisonsThe compareTo method of the processor returns 0 when it matches, and 1 when it does not. An error occurs if used with the LESS and GREATER operators.

String-based comparators, such as RegexString Comparator and Substring Comparator, are slower and more resource-intensive than byte-based comparators. Because they need to convert the given value to string in each comparison.The processing of string substrings and regular expressions takes extra time.

 

Comparison filter

The construction method of each comparison filter has a signature method inherited from CompareFilter.

public CompareFilter(final CompareOp compareOp,

      final ByteArrayComparable comparator) {

      this.compareOp = compareOp;

      this.comparator = comparator;

  }

Note: the original purpose of filters in HBase is to sift out useless information. The filtered information will not be transmitted to the client. Filter can not be used to specify what information the user needs, but in the process of reading data does not return the information the user does not want.

On the contrary, all the filtering processes based on ComppareFilter are exactly the opposite of what is described above, and they return matching values. Users need to select filters carefully according to different rules of filters.

 

HBaseConnection code

private static String addr="node233,node232,node231";
	private static  String port="2181";
	Logger logger = Logger.getLogger(getClass());

	private static Connection connection;
	
	public static void getConnection(){
		Configuration conf = HBaseConfiguration.create();

		conf.set("hbase.zookeeper.quorum",addr);
		conf.set("hbase.zookeeper.property.clientPort", port);
		try {
			connection = ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * Close connection** * /Public static void close () {/ * * * * ** close connection* * * /If (connection! = n)Ull) {Try {Connection.close ();} catch (IOException E) {E.printStackTrace ();}}}Public static void main (String[] args) {GetConnection ();Try {ColumnPaginationFiltErTest ();} catch (IOException E) {E.printStackTrace ();}Close ();}

 

1.Line filter (RowFilter)

Row filters are based on row keys to filter data.

/**
	 * Retrieval of rowkey* @throws IOException* * /Public static void rowFilterTest () throws IOException {TaBle table = connection.getTable (TableName.valueOf ("test_hbase"));Scan scan = new Scan ();SCAN.addColumn (Bytes.toBytes ("userInfo")) and Bytes.toBytes ("Id"));Filter filter = new RowFilter (Compare)Filter.CompareOp.LESS_OR_EQUAL, new BinaryComparator (Bytes.toBytes ("110115199402265244")));Scan.sEtFilter (filter);ResultScanner scanner = table.getScanner (scan);//keyvalues={110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0}For (Result result: scanner) {System.out.Println (result);}Scanner.close ();System.out.println ("==============filter2==============")= ");/ / retrieve the ending of rowkey at 244.Filter Filter2 = new RowFilter (CompareFilter.CompareOp.EQUAL, new RegexS)TringComparator (".*.244"));Scan.setFilter (Filter2);ResultScanner scanner2 = table.getScanner (Scan);For (Result result: scanner2) {System.out.println (result);}Scanner2.close ();System.out.println ("==============filter3===============");/ / retrieve that rowkey contains 244.Filter filter3 =New RowFilter (CompareFilter.CompareOp.EQUAL, new SubstringComparator ("244"));Scan.setFilter (filt)Er3);ResultScanner scanner3 = table.getScanner (scan);For (Result result: scanner3) {Cell[Cells = result.rawCells ();For (Cell cell: cells) {System.out.println (Bytes.toString (Cell)Util.cloneValue (cell));}System.out.println (result);}Scanner3.close ();}

  

2.Column family filter (FamilyFilter)

This filter is similar to a row filter, but it returns results by comparing column families rather than row keys. By using different combinations of operators and comparators, users can filter the required data at the column family level.

/**
     * Column family filter* @throws IOException* * /Public static void faimlyFilterTest () throws IOExceptioN {Table table = connection.getTable (TableName.valueOf ("test_hbase"));Filter filter= new FamilyFilter (CompareOp.LESS, new BinaryComparator (Bytes.toBytes ("userInfo")));Scan SCAN = new Scan ();Scan.setFilter (filter);/ / use filter scan tableResultScanner scanner = tablE.getScanner (scan);//keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seQid=0}//keyvalues={110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0}For (Result result: scanner) {System.out.println (result);}Scanner.closE ();System.out.println ("============get==============");/ / use the same filter to get a row of data.//ResUlt of get (): keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0}GeT get = new Get (Bytes.toBytes ("110115199402265244"));Get.setFilter (filter);Result reSult = table.get (get);System.out.println ("Result of get ()": + result);System.out.priNtln ("============filter=============");/ / create a filter on a column family and get another row data./ / use the new filter to get the same row data and return at this time.The result is NONE//Result of get (): keyvalues=NONEFilter Filter2 = new FamilyFilter (CompareOp.E)QUAL, new BinaryComparator (Bytes.toBytes ("userInfo1")));Get get2 = new Get (Bytes.toBytes ("110115199402265244 ");Get2.addFamily (Bytes.toBytes ("tag"));Get2.setFilter (Filter2);Result result2 = table.get (get2);System.out.println ("Result of get ()": + result2);}

 

3.Column name filter (QualifierFilter)

/**
     * Column name filter (QualifierFilter)* @throws IOException* * /Public static void qualifiterFilterTesT () throws IOException {Table table = connection.getTable (TableName.valueOf ("test_hbase"));Filter filter = new QualifierFilter (CompareOp.LESS_OR_EQUAL, new BinaryComparator (Bytes.toByt)ES ("basic_222"));Scan scan = new Scan ();Scan.setFilter (filter);ResultScanneR scanner = table.getScanner (scan);//keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0}For (Result result: scanner) {System.out.println (result);}Scanner.close ();Get get = new Get (Bytes.toBytes ("110115199402265244"));Get.setFilter (filter);Result result = table.get (get);//Result of get (): keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0}System.out.println ("Resu")Lt of get (): "+ result";}

 

4.Value filter (ValueFilter)

A cell that filters a specific value.

/**
     * Value filter* @throws IOException* * /Public static void valueFilterTest () throws IOException{Table table = connection.getTable (TableName.valueOf ("test_hbase"));Filter filter =New ValueFilter (CompareOp.EQUAL, new SubstringComparator ("Si"));Scan scan = new Scan ();Scan.setFilter (filter);ResultScanner scanner = table.getScanner (scan);//CELL: 110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0, Value:lisiFor (Result result)Scanner) {For (Cell cell: result.rawCells ()) {System.out.println ("CELL")"+ cell +", Value: "+ Bytes.toString (CellUtil.cloneValue (cell)));}}SCAnner.close ();System.out.println ("===============get===============");Get get = NewGet (Bytes.toBytes ("110115199402265245"));Get.setFilter (filter);Result result = table.get (get);For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + CEL)L +, "Value:" + Bytes.toString (CellUtil.cloneValue (cell));}}

 

5.Reference column filter (DependentColumnFilter)

This filter allows the user to specify a reference column or a reference column, and uses the reference column to control the filtering of other columns. The reference column filter uses the reference column timestamp and includes all columns with the same reference timestamp when filtering.

Construction method:

public DependentColumnFilter(final byte [] family, final byte[] qualifier,
      final boolean dropDependentColumn, final CompareOp valueCompareOp,
      final ByteArrayComparable valueComparator)
public DependentColumnFilter(final byte [] family, final byte [] qualifier)
public DependentColumnFilter(final byte [] family, final byte [] qualifier,
      final boolean dropDependentColumn)

The function of ValueFilter can be enabled by passing in comparison operators and benchmark values. By default, the constructor of this filter allows the user to ignore operators and comparators on all columns, as well as to shield value-based filtering, which means that the entire filter is filtered only based on the timestamp of the reference column.

dropDependentColumnHelps users manipulate the selection column: Setting this parameter to false or true determines whether the reference column can be returned or discarded.

/**
     * Reference column filter* @throws IOException* * /Public static void dependentColumnFilterTest () throwsIOException {//CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value: 223Filter (true, CompareOp.NO_OP, null);System.out.println ("----------filter (false),CompareOp.NO_OP, null);//CELL: 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, Value: "Zhang three".//CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/Seqid=0, Value:223//CELL: 110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0, VAlue:lisiFilter (false, CompareOp.NO_OP, null);System.out.println ("----------filter" (T)Rue, CompareOp.EQUAL, new BinaryPrefixComparator (Bytes.toBytes ("row\")); - - (-);//CELL:110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value:223Filter (true, Com)PareOp.EQUAL, new BinaryPrefixComparator (Bytes.toBytes ("Li")));System.out.println ("- -- ---filter (false, CompareOp.EQUAL, new BinaryPrefixComparator (Bytes.toBytes ("row\")); - - (-);//CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value:223//CELL110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0, Value:lisiFilter (false,CompareOp.EQUAL, new BinaryPrefixComparator (Bytes.toBytes ("Li")));System.out.println ("-----filter (true, CompareOp.EQUAL, new RegexStringComparator (".*.si\")); - - (-);//CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value:223Filter (true, Compa)ReOp.EQUAL, new RegexStringComparator (".*.si"));System.out.println ("----------filter (false),CompareOp.EQUAL, new RegexStringComparator (\ ".*.si\"); - --//CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value:223//CELL: 110115199402265245/userInfoName/1535527529043/Put/vlen=4/seqid=0, Value:lisiFilter (false, CompareOp.EQUAL, new RegexStr)IngComparator (".*.si"));}Public static void filter (Boolean drop, CompareOp operator, ByteARrayComparable comparator) throws IOException {Table table = connection.getTable (TableName.v)AlueOf ("test_hbase"));Filter filter;If (null = comparator) {Filter = neW DependentColumnFilter (Bytes.toBytes ("userInfo")) and Bytes.toBytes ("name").Drop, OpErator, comparator);} else {Filter = new DependentColumnFilter (Bytes.toBytes ("us")ErInfo "", Bytes.toBytes ("name"), drop);}Scan scan = new Scan ();Scan.setFilteR (filter);ResultScanner scanner = table.getScanner (scan);For (Result result: scanne)R) {For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + CE)Ll +, "Value:" + Bytes.toString (CellUtil.cloneValue (cell));}}}

This filter is not compatible with batch processing function of scanning operation. Filters need to look at the entire row of data to determine which data is filtered, and using batch processing may cause the data to be fetched to exclude reference columns, so the results are wrong.

Special filter

HBaseThe dedicated filters provided are directly inherited from FilterBase, and are used for more specific usage scenarios. Some of these filters can only be used for row filtering, so they are only suitable for scanning operations.

1.Single row value filter (SingleColumnValueFilter)

Determines whether a row of data is filtered with a column value.

Construction method

public SingleColumnValueFilter(final byte [] family, final byte [] qualifier,
      final CompareOp compareOp, final byte[] value)
public SingleColumnValueFilter(final byte [] family, final byte [] qualifier,
      final CompareOp compareOp, final ByteArrayComparable comparator)

Auxiliary method fine tuning filter

public boolean getFilterIfMissing()
public void setFilterIfMissing (boolean filterIfMissing)
public boolean getLatestVersionOnly()
public void setLatestVersionOnly(boolean latestVersionOnly)

setFilterIfMissing:How to handle this line when the reference column does not exist. The default row is in the hugged year result, and if you want to filter out the rows, you can use setFilterIfMissing (true) to filter out, so that all rows that do not contain the reference column can be filtered out.

Note: Users must include reference columns when scanning, and users use methods similar to addColumn to add reference columns to queries. If this is not done, that is, the scan result does not include a reference column, then the result may be empty or contain all rows, depending on setFiltThe setting value of erIfMissing returns.

setLatestVersionOnly:You can change the behavior of the filter to true by default, where the filter checks only the latest version of the reference column, and if set to false, all versions are checked.

/**
	 * Single row filtering* @throws IOException* eliminate eligible conditions.* * /Public static void singleColumnValueFilterTest () thRows IOException {Table table = connection.getTable (TableName.valueOf ("test_hbase"));SingleColumnValueFilter filter =New SingleColumnValueFilter (Bytes.toBytes ("userInfo")), Bytes.toBytes("name"),CompareOp.NOT_EQUAL, new SubstringComparator ("Si"));Filter.setFilterIfMissing (TRU)E);/ * * * * ** CELL: 110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, Value:222CELL: 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, Value:110115199402265244CELL: 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, Value: "Zhang three".CELL: 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0, Value:row-1* * /Scan scan = new Scan() ();Scan.setFilter (filter);ResultScanner scanner = table.getScanner (scan);For (Result result)Scanner) {For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + cell + ", Va)Lue: "+ Bytes.toString (CellUtil.cloneValue (cell)));}}Scanner.close ();/ * * * * ** ResulT of get (): keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0}CELL: 110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, Value:222CELL: 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, Value:110115199402265244CELL: 110115199402265244/UserInfo:name/1535527096150/Put/vlen=8/seqid=0, Value: "Zhang three".CELL: 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0, Value:row-1* * /Get get = new Get (Bytes.toBytes ("110115199402265244 "))Get.setFilter (filter);Result result = table.get (get);System.out.println ("Result of G")ET (): "+ result";For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + cell + ",Value: "+ Bytes.toString (CellUtil.cloneValue (cell)));}}

2.Single column exclusion filter (SingleColumnValueExcludeFilter)

The single row exclude filter inherits from SingleColumnValueFilter, and the reference column is not included in the result. Users can use the same features and methods as before to control the filter’s work, except that users in the client Result instance never get itReference columns for inspection purposes.

3.Prefix filter (PrefixFilter)

A prefix is passed in when the current filter is constructed, and all rows that match the prefix are returned to the client. It is very useful in scanning operation.

/**
	 * Prefix filter* @throws IOException* return to meet the conditions.* * /Public static void prefixFilterTest () throws IOExcePtion {Table table = connection.getTable (TableName.valueOf ("test_hbase"));Filter filter = nEW PrefixFilter (Bytes.toBytes ("110"));Scan scan = new Scan ();Scan.setFilter (filter);ResultSCanner scanner = table.getScanner (scan);/ * * * * ** CELL: 110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, Value:222CELL: 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, Value:110115199402265244CELL: 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, Value: "Zhang three".CELL: 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0, Value:Row-1CELL: 110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0, Value:223CELL:110115199402265245/userInfo:id/1535524869900/Put/vlen=18/seqid=0, Value:110115199402265245CELL: 110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0, Value:lisiCELL: 110115199402265245/userInfo:row/1535525622873/Put/vlen=5/seqid=0, Value:row-2* * /For (Result result: scanner){For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + cell + ", Value: + Byte")S.toString (CellUtil.cloneValue (cell)));}}Scanner.close ();}

Scanning operations are lookup in lexicographic order. When encountering larger rows than prefixes, the scan operation is over. By using the filter in conjunction with the start line, the filter’s scan performance is greatly improved because it skips all the subsequent rows when it finds that they do not meet the requirements.

 

4.Paging filter (PageFilter)

Users can use this filter to paging the results by line. The pageSize parameter, which controls the number of rows returned per page, is specified when the user creates the current filter instance.

Note: When performing filtering operations in parallel on physically separated servers, the following considerations should be noted.

Filters executed in parallel on different region servers cannot share their current state and boundaries, so each filter gets the results of the pageCount row before completing the scan, which makes the paging filter likely to fail and return more than needed. mostThe end client can choose to return all the results when merging the results, or it can use the API to filter the results according to the requirements.

The client code logs the last line of the scan, sets the last line of the last scan of the record to the start line of the scan the next time the data is retrieved, retains the same filter properties, and iterates over them in turn.

Paging imposes strict limits on the number of rows returned in turn, and the number of rows covered by sequential scanning is likely to be larger than the size of the page. Once this happens, the filter has a mechanism to notify the region server to stop scanning.

/**
	 * Paging filter* @throws IOException* * /Public static void pageFilterTest () throws IOException {TableTable = connection.getTable (TableName.valueOf ("socialSecurity"));Int pageCount = 10;Filter fiLter = new PageFilter (pageCount);Int totalRows = 0;Byte[] lastRow = null;While (true) {SCan scan = new Scan ();Scan.setFilter (filter);If (lastRow = null) {Byte[] startRow = ByTes.add (lastRow, new byte[0]);System.out.println ("start row:" + Bytes.toStringBinary (startRow));Scan.setStartRow (startRow);}ResultScanner scanner = table.getScanner (scan);Int locAlRows = 0;Result result;While ((result = scanner.next ()) = = null) {System.out.println (LocalRows++ + ":" + result);TotalRows++;LastRow = result.getRow ();}Scanner.close ();If (localRows = = 0 localRows < pageCount) {Break;}}System.out.println ("to")Tal rows: "+ totalRows";}

HBaseThe row keys are arranged in dictionary order, so are the returned results, and the starting row is included in the result. The user needs to splice a zero byte (a long zero byte array) to the previous row key, which ensures that the last returned row is not included in this round of scanning. When resetWhen scanning borders, zero byte is the most reliable way, because zero byte is the smallest increase. Even if one row has exactly the same line key as the previous row plus zero bytes, there’s no problem with this round of loops, because the starting row is included in the scan.

 

5.Line key filter (KeyOnlyFilter)

In some applications, you only need to return the key of the KeyValue instance in the result, instead of the actual data. KeyOnlyFilter provides functions that can modify scanned columns and cells. This filter is passed through KeyValue.convertToKe.The yOnly (Boolean) method helps the invocation only return keys and do not return values.

public KeyOnlyFilter(boolean lenAsVal)

The filter’s constructor needs a Boolean parameter called lenAsVal. This parameter will be passed into the convertToKeyOnly method, which can control the median value of the KeyValue instance. The default value is false, set to false, value.The byte array, which is set to length 0, is set to true, and the value is set as an array of bytes of the original value.

 

6.First time key filter (FirstKeyOnlyFilter)

If the user needs to access the first column in a row, the filter can meet the requirements. This filter is typically used in a row counter application scenario where only the line is checked to see if it exists. In a column storage database, if a row exists, the row is in the middle.There must be a list.

Because the columns are also arranged in dictionary order, other scenarios that might be used are to generate column names in chronological order, so that the oldest column is at the front, so that the column with the longest timestamp is retrieved first.

This class uses another optimization feature provided by the filter framework: it notifies the region server after checking the first column to end the scan of the current row and skip to the next row, which improves performance compared to the global scan.

 

7.Filter containing end (InclusiveStopFilter)

The start line in the scan operation is included in the result, but the termination row is excluded. Using this filter, you can also include the termination row.

 

/**
	 * Filter with end* @throws IOException* * /Public static void inclusiveStopFilterTest () throws IOExceptIon {Table table = connection.getTable (TableName.valueOf ("test_hbase"));Filter filter = NewInclusiveStopFilter (Bytes.toBytes ("110115199402265245"));Scan scan = new Scan ();Scan.setStartRow (Bytes.toBytes ("110115199402265244"));//keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0}//keyvalues={110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqiD=0, 110115199402265245/userInfo:id/1535524869900/Put/vlen=18/seqid=0, 110115199402265245/userInfo:nAme/1535527529043/Put/vlen=4/seqid=0, 110115199402265245/userInfo:row/1535525622873/Put/vlen=5/seqid=0}Scan.setFilter (filter);//keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vLen=3/seqid=0, 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, 110115199402265244/UserInfo:name/1535527096150/Put/vlen=8/seqid=0, 110115199402265244/userInfo:row/1535525614718/Put/vlEn=5/seqid=0}//scan.setStopRow (Bytes.toBytes ("110115199402265245"));ResultScanner scanner = taBle.getScanner (scan);For (Result result: scanner) {System.out.println (result);}Scanner.close ();}

  

8.Time stamp filter (TimestampFilter)

This filter satisfies the requirements when the user needs fine-grained control over the version in the scan results. Users need to import a List instance loaded with timestamp.

public TimestampsFilter(List<Long> timestamps)

A version is a value that is listed at a specific time and therefore is represented by a timestamp. When a filter requests a series of timestamps, it finds the exact version of the column that matches the timestamp in it.

/**
	 * Time stamp filter* @throws IOException* if multiple timestamps correspond to one row, only one row is returned.* if the specified time stamp of the same column is specified, the data of the new timestamp is returned.* * /Public stAtic void timestampsFilterTest () throws IOException {Table table = connection.getTable (TableName.)ValueOf ("test_hbase"));List< Long> ts = new ArrayList< Long> ();Ts.add (New Long (15350)80434895L))Ts.add (New Long (1535527096150L));Ts.add (New Long (1535079590454L));Filter filter= new TimestampsFilter (TS);/ * * * * ** keyvalues={110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0}Keyvalues={110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0110115199402265245/userInfo:name/1535079590454/Put/vlen=4/seqid=0}* * /Scan scan = new Scan ();Scan.setFilter (filter);ResultScanner scanner = table.getScanner (scan);For (Result result:Scanner) {For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + cell + ", Value)"+ Bytes.toString (CellUtil.cloneValue (cell)));}}Scanner.close ();System.out.println("==========scan2==========");/ * * * * ** keyvalues={110115199402265244/tag:basic_222/1535079521796/Put/vlen=3/seqid=0, 110115199402265244/userInfo:id/1535524861515/Put/vlen=18/seqid=0, 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seqid=0, 110115199402265244/userInfo:row/1535525614718/Put/vlen=5/seqid=0}Keyvalues={110115199402265245/tag:basic_223/1535079590454/Put/vlen=3/seqid=0,110115199402265245/userInfo:id/1535524869900/Put/vlen=18/seqid=0, 110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0, 110115199402265245/userInfo:row/1535525622873/Put/vlen=5/seqid=0}* * /Scan scan2 = new Scan ();Scan.setFilter (filter);Scan.setTimeRange (1535080434895L, 15355)27096149L);ResultScanner scanner2 = table.getScanner (scan2);For (Result result: scanner2) {For (Cell cell: result.rawCells ()) {System.out.println ("CELL:" + cell + ", Value: + Bytes.t")OString (CellUtil.cloneValue (cell)));}}Scanner2.close ();}

9.Column count filter (ColumnCountGetFilter)

You can use this filter to limit how many rows per line can be retrieved.

public ColumnCountGetFilter(final int n)

When the number of columns in a row reaches a set maximum, the filter stops the whole scan, so it’s not very suitable for the scan operation, but more suitable for the get method.

 

10.Column paging filter (ColumnPaginationFilter)

Similar to PageFilter, this filter can pagination all columns of a row. The constructor is as follows

public ColumnPaginationFilter(final int limit, final int offset)

It skips all columns with offsets less than offset and includes all subsequent columns with offsets before limit.

/**
	 * Column paging filter* @throws IOException* * /Public static void columnPaginationFilterTest () throws IOExcepTion {Table table = connection.getTable (TableName.valueOf ("test_hbase"));/ / returns two columns, starting from column index 1 (second columns).Filter filter = new ColumnPaginationFilter (2, 1);/ * * * * ** keyvalues={110115199402265244/userInfO:id/1535524861515/Put/vlen=18/seqid=0, 110115199402265244/userInfo:name/1535527096150/Put/vlen=8/seQid=0}Keyvalues={110115199402265245/userInfo:id/1535524869900/Put/vlen=18/seqid=0, 110115199402265245/userInfo:name/1535527529043/Put/vlen=4/seqid=0}* * /Scan scan = new Scan ();Scan.setFiltEr (filter);ResultScanner scanner = table.getScanner (scan);For (Result result: scanner) {SYstem.out.println (result);}Scanner.close ();}

 

11.Column prefix filter (ColumnPrefixFilter)

Similar to PrefixFilter, this filter performs prefix matching filtering through column names.

public ColumnPrefixFilter(final byte [] prefix)

 

12.Random line filter (RandomRowFilter)

You can include random rows in the result. The constructor is as follows

public RandomRowFilter(float chance)

Within the filter, the Java method Random. nextFloat () is used to determine whether a row is filtered, and the results are compared with the user-set chance. If the user assign a negative value to chance, all the results will be filtered out.Conversely, if chance is greater than 1, the result set contains all rows.

 

Leave a Reply

Your email address will not be published. Required fields are marked *