转自:http://blog.csdn.net/saint1126/article/details/8257941

base过滤器的比较操作符:

 

LESS  <
LESS_OR_EQUAL <=
EQUAL =
NOT_EQUAL <>
GREATER_OR_EQUAL >=
GREATER >
NO_OP no operation

比较器:
 
BinaryComparator  按字节索引顺序比较指定字节数组,采用Bytes.compareTo(byte[])
BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同
NullComparator 判断给定的是否为空
BitComparator 按位比较 a BitwiseOp class 做异或,与,并操作
RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL
SubstringComparator 判断提供的子串是否出现在table的value中。
 

Hbase的过滤器分类

 
比较过滤器
 
1、Comparision Filters
     1.1  RowFilter
构造函数
  1. public RowFilter(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp rowCompareOp, org.apache.hadoop.hbase.filter.WritableByteArrayComparable rowComparator) {}

选择比较rowkey来确定选择合适的行信息。

 
  1. public class RowFilterExample {
  2. public static void main(String[] args) throws IOException {
  3. Configuration conf = HBaseConfiguration.create();
  4. HBaseHelper helper = HBaseHelper.getHelper(conf);
  5. helper.dropTable("testtable");
  6. helper.createTable("testtable", "colfam1", "colfam2");
  7. System.out.println("Adding rows to table...");
  8. helper.fillTable("testtable", 1, 100, 100, "colfam1", "colfam2");
  9. HTable table = new HTable(conf, "testtable");
  10. // vv RowFilterExample
  11. Scan scan = new Scan();
  12. scan.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-0"));
  13. Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, // co RowFilterExample-1-Filter1 Create filter, while specifying the comparison operator and comparator. Here an exact match is needed.
  14. new BinaryComparator(Bytes.toBytes("row-22")));
  15. scan.setFilter(filter1);
  16. ResultScanner scanner1 = table.getScanner(scan);
  17. // ^^ RowFilterExample
  18. System.out.println("Scanning table #1...");
  19. // vv RowFilterExample
  20. for (Result res : scanner1) {
  21. System.out.println(res);
  22. }
  23. scanner1.close();
  24. Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL, // co RowFilterExample-2-Filter2 Another filter, this time using a regular expression to match the row keys.
  25. new RegexStringComparator(".*-.5"));
  26. scan.setFilter(filter2);
  27. ResultScanner scanner2 = table.getScanner(scan);
  28. // ^^ RowFilterExample
  29. System.out.println("Scanning table #2...");
  30. // vv RowFilterExample
  31. for (Result res : scanner2) {
  32. System.out.println(res);
  33. }
  34. scanner2.close();
  35. Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL, // co RowFilterExample-3-Filter3 The third filter uses a substring match approach.
  36. new SubstringComparator("-5"));
  37. scan.setFilter(filter3);
  38. ResultScanner scanner3 = table.getScanner(scan);
  39. // ^^ RowFilterExample
  40. System.out.println("Scanning table #3...");
  41. // vv RowFilterExample
  42. for (Result res : scanner3) {
  43. System.out.println(res);
  44. }
  45. scanner3.close();
  46. // ^^ RowFilterExample
  47. }
  48. }

1.2  FamilyFilter

构造函数

  1. public FamilyFilter(CompareOp familyCompareOp, WritableByteArrayComparable familyComparator) {}

通过和列簇比较得到,返回结果为真的数据,示例:

  1. public class FamilyFilterExample {
  2. public static void main(String[] args) throws IOException {
  3. Configuration conf = HBaseConfiguration.create();
  4. HBaseHelper helper = HBaseHelper.getHelper(conf);
  5. helper.dropTable("testtable");
  6. helper.createTable("testtable", "colfam1", "colfam2", "colfam3", "colfam4");
  7. System.out.println("Adding rows to table...");
  8. helper.fillTable("testtable", 1, 10, 2, "colfam1", "colfam2", "colfam3", "colfam4");
  9. HTable table = new HTable(conf, "testtable");
  10. // vv FamilyFilterExample
  11. Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.LESS, // co FamilyFilterExample-1-Filter Create filter, while specifying the comparison operator and comparator.
  12. new BinaryComparator(Bytes.toBytes("colfam3")));
  13. Scan scan = new Scan();
  14. scan.setFilter(filter1);
  15. ResultScanner scanner = table.getScanner(scan); // co FamilyFilterExample-2-Scan Scan over table while applying the filter.
  16. // ^^ FamilyFilterExample
  17. System.out.println("Scanning table... ");
  18. // vv FamilyFilterExample
  19. for (Result result : scanner) {
  20. System.out.println(result);
  21. }
  22. scanner.close();
  23. Get get1 = new Get(Bytes.toBytes("row-5"));
  24. get1.setFilter(filter1);
  25. Result result1 = table.get(get1); // co FamilyFilterExample-3-Get Get a row while applying the same filter.
  26. System.out.println("Result of get(): " + result1);
  27. Filter filter2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL,
  28. new BinaryComparator(Bytes.toBytes("colfam3")));
  29. Get get2 = new Get(Bytes.toBytes("row-5")); // co FamilyFilterExample-4-Mismatch Create a filter on one column family while trying to retrieve another.
  30. get2.addFamily(Bytes.toBytes("colfam1"));
  31. get2.setFilter(filter2);
  32. Result result2 = table.get(get2); // co FamilyFilterExample-5-Get2 Get the same row while applying the new filter, this will return "NONE".
  33. System.out.println("Result of get(): " + result2);
  34. // ^^ FamilyFilterExample
  35. }
  36. }
     1.3 QualifierFilter
构造函数
  1. public QualifierFilter(CompareOp qualifierCompareOp, WritableByteArrayComparable qualifierComparator) {  }

通过和列名比较,返回为真的数据,示例:

  1. // vv QualifierFilterExample
  2. Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
  3. new BinaryComparator(Bytes.toBytes("col-2")));
  4. Scan scan = new Scan();
  5. scan.setFilter(filter);
  6. ResultScanner scanner = table.getScanner(scan);
  7. // ^^ QualifierFilterExample
  8. System.out.println("Scanning table... ");
  9. // vv QualifierFilterExample
  10. for (Result result : scanner) {
  11. System.out.println(result);
  12. }
  13. scanner.close();
  14. Get get = new Get(Bytes.toBytes("row-5"));
  15. get.setFilter(filter);
  16. Result result = table.get(get);
  17. System.out.println("Result of get(): " + result);

1.4 ValueFilter

构造函数
  1. <pre name="code" class="java" style="color: rgb(88, 89, 93); font-size: 14px; line-height: 25px;">public ValueFilter(CompareOp valueCompareOp, WritableByteArrayComparable valueComparator) {}</pre>
  2. <pre></pre>
  3. 通过和列名比较,返回为真的数据,示例:
  4. <pre></pre>
  5. <pre></pre>
  6. <pre></pre>
  7. <pre></pre>
  1. Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, // co ValueFilterExample-1-Filter Create filter, while specifying the comparison operator and comparator.
  2. new SubstringComparator(".4") );
  3. Scan scan = new Scan();
  4. scan.setFilter(filter); // co ValueFilterExample-2-SetFilter Set filter for the scan.
  5. ResultScanner scanner = table.getScanner(scan);
  6. // ^^ ValueFilterExample
  7. System.out.println("Results of scan:");
  8. // vv ValueFilterExample
  9. for (Result result : scanner) {
  10. for (KeyValue kv : result.raw()) {
  11. System.out.println("KV: " + kv + ", Value: " + // co ValueFilterExample-3-Print1 Print out value to check that filter works.
  12. Bytes.toString(kv.getValue()));
  13. }
  14. }
  15. scanner.close();
  16. Get get = new Get(Bytes.toBytes("row-5"));
  17. get.setFilter(filter); // co ValueFilterExample-4-SetFilter2 Assign same filter to Get instance.
  18. Result result = table.get(get);
  19. // ^^ ValueFilterExample
  20. System.out.println("Result of get: ");
  21. // vv ValueFilterExample
  22. for (KeyValue kv : result.raw()) {
  23. System.out.println("KV: " + kv + ", Value: " +
  24. Bytes.toString(kv.getValue()));
  25. }
     1.5 DependentColumnFilter

该过滤器有两个参数 —— 列族和列修饰。 尝试找到该列所在的每一行,并返回该行具有相同时间戳的全部键值对。如果某一行不包含指定的列,则该行的任何键值对都不返回。
该过滤器还可以有一个可选布尔参数 —— dropDependentColumn. 如果为true, 从属的列不返回。
该过滤器还可以有两个可选参数 —— 一个比较操作符和一个值比较器,用于列族和修饰的进一步检查。如果从属的列找到,其值还必须通过值检查,然后就是时间戳必须考虑。

  1. package filters;
  2. // cc DependentColumnFilterExample Example using a filter to include only specific column families
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.KeyValue;
  6. import org.apache.hadoop.hbase.client.Get;
  7. import org.apache.hadoop.hbase.client.HTable;
  8. import org.apache.hadoop.hbase.client.Result;
  9. import org.apache.hadoop.hbase.client.ResultScanner;
  10. import org.apache.hadoop.hbase.client.Scan;
  11. import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
  12. import org.apache.hadoop.hbase.filter.CompareFilter;
  13. import org.apache.hadoop.hbase.filter.DependentColumnFilter;
  14. import org.apache.hadoop.hbase.filter.Filter;
  15. import org.apache.hadoop.hbase.filter.RegexStringComparator;
  16. import org.apache.hadoop.hbase.filter.WritableByteArrayComparable;
  17. import org.apache.hadoop.hbase.util.Bytes;
  18. import util.HBaseHelper;
  19. import java.io.IOException;
  20. public class DependentColumnFilterExample {
  21. private static HTable table = null;
  22. // vv DependentColumnFilterExample
  23. private static void filter(boolean drop,
  24. CompareFilter.CompareOp operator,
  25. WritableByteArrayComparable comparator)
  26. throws IOException {
  27. Filter filter;
  28. if (comparator != null) {
  29. filter = new DependentColumnFilter(Bytes.toBytes("colfam1"), // co DependentColumnFilterExample-1-CreateFilter Create the filter with various options.
  30. Bytes.toBytes("col-5"), drop, operator, comparator);
  31. } else {
  32. filter = new DependentColumnFilter(Bytes.toBytes("colfam1"),
  33. Bytes.toBytes("col-5"), drop);
  34. }
  35. Scan scan = new Scan();
  36. scan.setFilter(filter);
  37. ResultScanner scanner = table.getScanner(scan);
  38. // ^^ DependentColumnFilterExample
  39. System.out.println("Results of scan:");
  40. // vv DependentColumnFilterExample
  41. for (Result result : scanner) {
  42. for (KeyValue kv : result.raw()) {
  43. System.out.println("KV: " + kv + ", Value: " +
  44. Bytes.toString(kv.getValue()));
  45. }
  46. }
  47. scanner.close();
  48. Get get = new Get(Bytes.toBytes("row-5"));
  49. get.setFilter(filter);
  50. Result result = table.get(get);
  51. // ^^ DependentColumnFilterExample
  52. System.out.println("Result of get: ");
  53. // vv DependentColumnFilterExample
  54. for (KeyValue kv : result.raw()) {
  55. System.out.println("KV: " + kv + ", Value: " +
  56. Bytes.toString(kv.getValue()));
  57. }
  58. // ^^ DependentColumnFilterExample
  59. System.out.println("");
  60. // vv DependentColumnFilterExample
  61. }
  62. public static void main(String[] args) throws IOException {
  63. // ^^ DependentColumnFilterExample
  64. Configuration conf = HBaseConfiguration.create();
  65. HBaseHelper helper = HBaseHelper.getHelper(conf);
  66. helper.dropTable("testtable");
  67. helper.createTable("testtable", "colfam1", "colfam2");
  68. System.out.println("Adding rows to table...");
  69. helper.fillTable("testtable", 1, 10, 10, true, "colfam1", "colfam2");
  70. table = new HTable(conf, "testtable");
  71. // vv DependentColumnFilterExample
  72. filter(true, CompareFilter.CompareOp.NO_OP, null);
  73. filter(false, CompareFilter.CompareOp.NO_OP, null); // co DependentColumnFilterExample-2-Filter Call filter method with various options.
  74. filter(true, CompareFilter.CompareOp.EQUAL,
  75. new BinaryPrefixComparator(Bytes.toBytes("val-5")));
  76. filter(false, CompareFilter.CompareOp.EQUAL,
  77. new BinaryPrefixComparator(Bytes.toBytes("val-5")));
  78. filter(true, CompareFilter.CompareOp.EQUAL,
  79. new RegexStringComparator(".*\\.5"));
  80. filter(false, CompareFilter.CompareOp.EQUAL,
  81. new RegexStringComparator(".*\\.5"));
  82. }
  83. // ^^ DependentColumnFilterExample}

2、Dedicated Filters
     2.1 SingleColumnValueFilter

选定列簇和某一列,然后与列的value相比,正确的返回全部的row,注意如果某一行不含有该列,同样返回,除非通过filterIfColumnMissing 设置成真。

构造函数

  1. <pre name="code" class="java">SingleColumnValueFilter(byte[] family, byte[] qualifier, CompareOp compareOp, byte[] value)
  2. SingleColumnValueFilter(byte[] family, byte[] qualifier, CompareOp compareOp, WritableByteArrayComparable comparator)</pre>
  3. <pre></pre>
  4. 第一个构造函数相当于构建了一个BinaryComparator的实例。其他的跟CompareFilter的参数含义一样。
  5. <pre></pre>
  6. <pre></pre>
  7. <pre></pre>

该过滤器通过下面两个参数 filterIfMissing,latestVersionOnly

  1. boolean getFilterIfMissing()
  2. void setFilterIfMissing(boolean filterIfMissing)
  3. boolean getLatestVersionOnly()
  4. void setLatestVersionOnly(boolean latestVersionOnly)

如果 filterIfColumnMissing 标志设为真,如果该行没有指定的列,那么该行的所有列将不发出。缺省值为假。
如果setLatestVersionOnly 标志设为假,将检查此前的版本。缺省值为真。实例如下:

  1. // vv SingleColumnValueFilterExample
  2. SingleColumnValueFilter filter = new SingleColumnValueFilter(
  3. Bytes.toBytes("colfam1"),
  4. Bytes.toBytes("col-5"),
  5. CompareFilter.CompareOp.NOT_EQUAL,
  6. new SubstringComparator("val-5"));
  7. filter.setFilterIfMissing(true);
  8. Scan scan = new Scan();
  9. scan.setFilter(filter);
  10. ResultScanner scanner = table.getScanner(scan);
  11. // ^^ SingleColumnValueFilterExample
  12. System.out.println("Results of scan:");
  13. // vv SingleColumnValueFilterExample
  14. for (Result result : scanner) {
  15. for (KeyValue kv : result.raw()) {
  16. System.out.println("KV: " + kv + ", Value: " +
  17. Bytes.toString(kv.getValue()));
  18. }
  19. }
  20. scanner.close();
  21. Get get = new Get(Bytes.toBytes("row-6"));
  22. get.setFilter(filter);
  23. Result result = table.get(get);
  24. System.out.println("Result of get: ");
  25. for (KeyValue kv : result.raw()) {
  26. System.out.println("KV: " + kv + ", Value: " +
  27. Bytes.toString(kv.getValue()));
  28. }
     2.2 SingleColumnValueExcludeFilter

该过滤器同上面的过滤器正好相反,如果条件相符,将不会返回该列的内容。

     2.3 PrefixFilter

所有的row的实例匹配prefix的时候返回结果集合

  1. Filter filter = new PrefixFilter(Bytes.toBytes("row1"));
  2. Scan scan = new Scan();
  3. scan.setFilter(filter);
  4. ResultScanner scanner = table.getScanner(scan);
  5. for(Result result: scanner){
  6. for(KeyValue kv: result.raw()) {
  7. System.out.println("KV:" + kv + ", Value:"  + Bytes.toString(kv.getValue()));
  8. }
  9. }
  10. scanner.close();
  11. Get get = new Get(Bytes.toBytes("row-5"));
  12. get.setFilter(filter);
  13. Result result = table.get(get);
  14. for(KeyValue kv : result.raw()){
  15. System.out.println("KV:" + kv + ", Value:"  + Bytes.toString(kv.getValue()));
  16. }

2.4 PageFilter

页过滤,通过设置pagesize参数可以返回每一页page的数量。

客户端需要记住上一次访问的row的key值。

  1. package hbaseTest;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.client.HTable;
  5. import org.apache.hadoop.hbase.client.Result;
  6. import org.apache.hadoop.hbase.client.ResultScanner;
  7. import org.apache.hadoop.hbase.client.Scan;
  8. import org.apache.hadoop.hbase.filter.Filter;
  9. import org.apache.hadoop.hbase.filter.PageFilter;
  10. import org.apache.hadoop.hbase.util.Bytes;
  11. import java.io.IOException;
  12. /**
  13. * Hello world!
  14. */
  15. public class PageFilterExample {
  16. public static void main(String[] args) throws IOException {
  17. Configuration config = HBaseConfiguration.create();
  18. config.set("hbase.zookeeper.quorum", "QT-H-0038");
  19. String tableName = "testTable";
  20. String cfName = "colfam1";
  21. final byte[] POSTFIX = new byte[] { 0x00 };
  22. HTable table = new HTable(config, tableName);
  23. Filter filter = new PageFilter(15);
  24. byte[] lastRow = null;
  25. int totalRows = 0;
  26. while (true) {
  27. Scan scan = new Scan();
  28. scan.setFilter(filter);
  29. if(lastRow != null){
  30. //注意这里添加了POSTFIX操作,不然死循环了
  31. byte[] startRow = Bytes.add(lastRow,POSTFIX);
  32. scan.setStartRow(startRow);
  33. }
  34. ResultScanner scanner = table.getScanner(scan);
  35. int localRows = 0;
  36. Result result;
  37. while((result = scanner.next()) != null){
  38. System.out.println(localRows++ + ":" + result);
  39. totalRows ++;
  40. lastRow = result.getRow();
  41. }
  42. scanner.close();
  43. if(localRows == 0) break;
  44. }
  45. System.out.println("total rows:" + totalRows);
  46. }
  47. }

因为hbase的row是字典序列排列的,因此上一次的lastrow需要添加额外的0表示新的开始。另外startKey的那一行是包含在scan里面的。

 

2.5 KeyOnlyFilter

因为一些应用只想获取data数据,而不是真实的val,可以使用这个过滤器。该过滤器通过

  1. KeyOnlyFilter(boolean lenAsVal)

lenAsVal默认为假,表示不把val的长度作为val。否则 val的长度将作为val输出。

  1. final byte[] POSTFIX = new byte[] { 0x00 };
  2. HTable table = new HTable(config, tableName);
  3. Filter filter = new KeyOnlyFilter(false);
  4. byte[] lastRow = null;
  5. int totalRows = 0;
  6. Scan scan = new Scan();
  7. scan.setFilter(filter);
  8. ResultScanner scanner = table.getScanner(scan);
  9. for(Result result: scanner){
  10. for(KeyValue kv: result.raw()){
  11. System.out.println(kv + ":" + Bytes.toString(kv.getValue()));
  12. }
  13. }
     2.6 FirstKeyOnlyFilter
在对hbase的表进行扫描的时候,如果指定了FirstKeyOnlyFilter过滤条件则仅仅会返回相同key的第一条kv。
当对hbase中的表进行count,sum操作等集合操作的时候,使用FirstKeyOnlyFilter会带来性能上的提升。
  1. public class KeyOnlyFilterExample {
  2. public static void main(String[] args) throws IOException {
  3. Configuration config = HBaseConfiguration.create();
  4. config.set("hbase.zookeeper.quorum", "QT-H-0038");
  5. String tableName = "testTable";
  6. String cfName = "colfam1";
  7. final byte[] POSTFIX = new byte[] { 0x00 };
  8. HTable table = new HTable(config, tableName);
  9. Filter filter = new FirstKeyOnlyFilter();
  10. byte[] lastRow = null;
  11. int totalRows = 0;
  12. Scan scan = new Scan();
  13. scan.setFilter(filter);
  14. ResultScanner scanner = table.getScanner(scan);
  15. for(Result result: scanner){
  16. for(KeyValue kv: result.raw()){
  17. System.out.println(kv + ":" + Bytes.toString(kv.getValue()));
  18. }
  19. }
  20. }
  21. }

返回的结果是

  1. row-5/colfam1:qual1/1354673733503/Put/vlen=4:row1
  2. row1/colfam1:qual1/1354432930568/Put/vlen=4:val1
  3. row2/colfam1:qual2/1354432930568/Put/vlen=4:val3

如果注释掉过滤器的返回的结果是:

  1. row-5/colfam1:qual1/1354673733503/Put/vlen=4:row1
  2. row1/colfam1:qual1/1354432930568/Put/vlen=4:val1
  3. row1/colfam1:qual2/1354435819120/Put/vlen=4:val2
  4. row2/colfam1:qual2/1354432930568/Put/vlen=4:val3
     2.7 InclusiveStopFilter
因为hbase的scan包含start-row不包含stop-row 如果使用这个过滤器我们可以包含stop-row
  1. HTable table = new HTable(config, tableName);
  2. Filter filter  = new InclusiveStopFilter(Bytes.toBytes("row1"));
  3. Scan scan = new Scan();
  4. scan.setFilter(filter);
  5. scan.setStartRow(Bytes.toBytes("row-5"));
  6. ResultScanner scanner = table.getScanner(scan);
  7. for(Result result: scanner){
  8. System.out.println(result);
  9. }

会看到row1包含在结果中了。

 
     2.8 TimestampsFilter

当访问某个Timestamp的新闻的时候,我们需要如下的代码:

  1. TimestampsFilter(List<Long> timestamps)

接受的参数的list参数,该Filter也可以和scan.setTimeRange混合使用。例如:

  1. // vv TimestampFilterExample
  2. List<Long> ts = new ArrayList<Long>();
  3. ts.add(new Long(5));
  4. ts.add(new Long(10)); // co TimestampFilterExample-1-AddTS Add timestamps to the list.
  5. ts.add(new Long(15));
  6. Filter filter = new TimestampsFilter(ts);
  7. Scan scan1 = new Scan();
  8. scan1.setFilter(filter); // co TimestampFilterExample-2-AddFilter Add the filter to an otherwise default Scan instance.
  9. ResultScanner scanner1 = table.getScanner(scan1);
  10. // ^^ TimestampFilterExample
  11. System.out.println("Results of scan #1:");
  12. // vv TimestampFilterExample
  13. for (Result result : scanner1) {
  14. System.out.println(result);
  15. }
  16. scanner1.close();
  17. Scan scan2 = new Scan();
  18. scan2.setFilter(filter);
  19. scan2.setTimeRange(8, 12); // co TimestampFilterExample-3-AddTSRange Also add a time range to verify how it affects the filter
  20. ResultScanner scanner2 = table.getScanner(scan2);
  21. // ^^ TimestampFilterExample
  22. System.out.println("Results of scan #2:");
  23. // vv TimestampFilterExample
  24. for (Result result : scanner2) {
  25. System.out.println(result);
  26. }
  27. scanner2.close();
 
     2.9  ColumnCountGetFilter

这个在scan时,无用。

     2.10 ColumnPaginationFilter(下来用到的时候在仔细研究下)
 * A filter, based on the ColumnCountGetFilter, takes two arguments: limit and offset.
 * This filter can be used for row-based indexing, where references to other tables are stored across many columns,
 * in order to efficient lookups and paginated results for end users.

  1. Filter filter = new ColumnPaginationFilter(5, 15);
  2. Scan scan = new Scan();
  3. scan.setFilter(filter);
  4. ResultScanner scanner = table.getScanner(scan);
  5. // ^^ ColumnPaginationFilterExample
  6. System.out.println("Results of scan:");
  7. // vv ColumnPaginationFilterExample
  8. for (Result result : scanner) {
  9. System.out.println(result);
  10. }
  11. scanner.close();
     2.11 ColumnPrefixFilter
跟prefxiFilter相似,只是改成了Column,实例如下:
  1. // vv ColumnPaginationFilterExample
  2. Filter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2"));
  3. Scan scan = new Scan();
  4. scan.setFilter(filter);
  5. ResultScanner scanner = table.getScanner(scan);
  6. // ^^ ColumnPaginationFilterExample
  7. System.out.println("Results of scan:");
  8. // vv ColumnPaginationFilterExample
  9. for (Result result : scanner) {
  10. System.out.println(result);
  11. }
  12. scanner.close();

值scan到与列值与前面匹配的数据。例如qual2匹配qual21。

 
     2.12 RandomRowFilter
随即的返回row的数据,构造函数为
  1. RandomRowFilter(float chance)

chance取值为0到1.0,如果<0则为空,如果>1则包含所有的行。

3、Decorating Filters

装饰性过滤器

3.1  SkipFilter

这个过滤器只作用到keyValueFilter上。KeyValueFilter会返回所有满足条件的row及对应的列。

而加上SkipFilter以后。会发现如果某一行的某一列不符合条件,则这一行全部不返回了。

  1. public static void main(String[] args) throws IOException {
  2. Configuration conf = HBaseConfiguration.create();
  3. HBaseHelper helper = HBaseHelper.getHelper(conf);
  4. helper.dropTable("testtable");
  5. helper.createTable("testtable", "colfam1");
  6. System.out.println("Adding rows to table...");
  7. helper.fillTable("testtable", 1, 30, 5, 2, true, true, "colfam1");
  8. HTable table = new HTable(conf, "testtable");
  9. // vv SkipFilterExample
  10. Filter filter1 = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL,
  11. new BinaryComparator(Bytes.toBytes("val-0")));
  12. Scan scan = new Scan();
  13. scan.setFilter(filter1); // co SkipFilterExample-1-AddFilter1 Only add the ValueFilter to the first scan.
  14. ResultScanner scanner1 = table.getScanner(scan);
  15. // ^^ SkipFilterExample
  16. System.out.println("Results of scan #1:");
  17. int n = 0;
  18. // vv SkipFilterExample
  19. for (Result result : scanner1) {
  20. for (KeyValue kv : result.raw()) {
  21. System.out.println("KV: " + kv + ", Value: " +
  22. Bytes.toString(kv.getValue()));
  23. // ^^ SkipFilterExample
  24. n++;
  25. // vv SkipFilterExample
  26. }
  27. }
  28. scanner1.close();
  29. Filter filter2 = new SkipFilter(filter1);
  30. scan.setFilter(filter2); // co SkipFilterExample-2-AddFilter2 Add the decorating skip filter for the second scan.
  31. ResultScanner scanner2 = table.getScanner(scan);
  32. // ^^ SkipFilterExample
  33. System.out.println("Total KeyValue count for scan #1: " + n);
  34. n = 0;
  35. System.out.println("Results of scan #2:");
  36. // vv SkipFilterExample
  37. for (Result result : scanner2) {
  38. for (KeyValue kv : result.raw()) {
  39. System.out.println("KV: " + kv + ", Value: " +
  40. Bytes.toString(kv.getValue()));
  41. // ^^ SkipFilterExample
  42. n++;
  43. // vv SkipFilterExample
  44. }
  45. }
  46. scanner2.close();
  47. // ^^ SkipFilterExample
  48. System.out.println("Total KeyValue count for scan #2: " + n);
  49. }

3.2 WhileMatchFilters

相当于while执行,知道不match就break了返回了。

hbase权威指南阅读随手笔记二之过滤器的更多相关文章

  1. 《HBase权威指南》读书笔记----简介

    工作中要使用HBase,刚刚开始接触HBase,理解不深,只是记录一下 . HBase基于google的bigtable论文实现,属于nosql. 几个概念: (1)列(column):最基本单位为列 ...

  2. 《HBase权威指南》学习笔记

    第一章  简介 背景: GFS:集群存储海量数据,数据在节点间冗余复制,即使一台存储服务器发生故障,也不会影响可用性. GFS的缺点:适合存储少许非常大的文件,而不适合存储大量小文件,因为文件的元数据 ...

  3. 【vue 权威指南】 学习笔记 二

    1.指令 1.1内部指令 基础指令:v-show , v-else , v-model , v-repeat , v-for , v-text , v-el , v-html , v-on , v-b ...

  4. 《javascript权威指南》读书笔记——第一篇

    <javascript权威指南>读书笔记——第一篇 金刚 javascript js javascript权威指南 由于最近想系统学习下javascript,所以开始在kindle上看这本 ...

  5. 《Kafka权威指南》读书笔记-操作系统调优篇

    <Kafka权威指南>读书笔记-操作系统调优篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大部分Linux发行版默认的内核调优参数配置已经能够满足大多数应用程序的运 ...

  6. HBase权威指南 高清中文版 PDF(来自linuxidc)

      内容提要 <HBase权威指南>由乔治(Lars George)著,探讨了 如何通过使用与HBase高度集成的Hadoop将 HBase的可 伸缩性变得简单:把大型数据集分布到相对廉价 ...

  7. 《javascript权威指南》读书笔记——第二篇

    <javascript权威指南>读书笔记——第二篇 金刚 javascript js javascript权威指南 今天是今年的196天,分享今天的读书笔记. 第2章 词法结构 2.1 字 ...

  8. 《jQuery权威指南》学习笔记之第2章 jQuery选择器

    2.1 jQuery选择器概述 2.1.1 什么使选择器 2.1.2 选择器的优势: 代码更简单,完善的检测机制  1.代码更简单   示例2-1     使用javascript实现隔行变色 < ...

  9. 《Hadoop权威指南》读书笔记1

    <Hadoop权威指南>读书笔记 Day1 第一章 1.MapReduce适合一次写入.多次读取数据的应用,关系型数据库则更适合持续更新的数据集. 2.MapReduce是一种线性的可伸缩 ...

随机推荐

  1. RHEL7 -- 通过gerp使用正则表达式

    正则表达式常会含有shell元字符(如S.*等),建议使用单引号('')来括起行令上的正则表达式 1.行定位符号 行首定位符号^和行尾定位符$ #找出以s开头的行: # grep '^s' /etc/ ...

  2. Java 异常模型综述

    一. 异常的引入及基础 发现错误的理想时机是在编译阶段.也就是在你试图运行程序之前. 然而,编译期间编译器并不能找出全部的错误,余下的错误仅仅有在运行期才干发现和解决,这类错误就是 Throwable ...

  3. SICP 习题 (2.8) 解题总结:区间的减法

    SICP 习题 2.8 须要我们完毕区间运算的减法.区间运算的加法书中已经有了,代码例如以下: (define (add-interval x y) (make-interval (+ (lower- ...

  4. 使用 dockerfile 创建镜像

    dockerfile 是一个文本格式的配置文件,可以使用 dockerfile 快速创建自定义的镜像. dockerfile 一般包含4部分信息:基础镜像信息.维护者信息.镜像操作指令.容器启动时执行 ...

  5. unity5 Orthographic模式相机视截体尺寸计算

    一,通过编辑器中数值计算. 如图,相机为Orthographic模式,其camera size为5.57,是什么含义呢,经过测量,发现视图中视截体的高度是5.57x2. 那么视截体的宽度怎么决定呢? ...

  6. mod_fastcgi和mod_fcgid的区别

    mod_fcgid是一个跟mod_fastcgi二进制兼容的Apache module. 原 来的mod_fastcgi因为实现方式的限制,所以可能会创建了很多不必要的进程,而实际上只需要更少的进程就 ...

  7. chkconfig命令具体介绍

    命令介绍: chkconfig命令用来更新.查询.改动不同执行级上的系统服务.比方安装了httpd服务,而且把启动的脚本放在了/etc/rc.d/init.d文件夹下,有时候须要开机自己主动启动它,而 ...

  8. Sampling and Estimation

    Sampling and Estimation Sampling Error Sampling error is the difference between a sample statistic(t ...

  9. js操作checkbox(复选框)的方法总结

    收集了一些用js代码操作checkbox复选框的代码,分享出来,供需要的朋友参考: <script> //复选框checkbox 处理方法 //搜集整理 www.jbxue.com fun ...

  10. win常用

    //base.Invoke((MethodInvoker)delegate() //{ // this.Close(); //});