知识回顾:

1. 值过滤器(ValueFilter)

Filter filter= new ValueFilter (CompareFilter.CompareOp.EQUAL, new SubstringComparator(“wang”));
解释:用ValueFilter类实例化了一个filter对象,并将wang这个参数传入到了filter对象中

2. 单列值过滤器(SingleColumnValueFilter)

Filter filter= new SingleColumnValueFilter( Bytes.toBytes(“info”), Bytes.toBytes(“name”), CompareFilter.CompareOp.EQUAL, new SubstringComparator(“wang”));
解释:用SingleColumnValueFilter类实例化一个filter对象, 将info族name列字符串转换成了字节流并封装起来,将wang这个参数传入到filter对象中

3. 列键过滤器(KeyOnlyFilter)

列键过滤器的作用就是在遍历过程中不获取值只获取列名。
KeyOnlyFilter filter = new KeyOnlyFilter();
解释:

4. 首次列键过滤器(FirstKeyOnlyFilter)

解释:首次列键过滤器是在列键过滤器的基础上更进一步,他在遍历到行的第1个列的时候就立即放弃了往下遍历该行的其他列,转而便立下一个列,由于总是获取第1个列键,所以叫首次列键过滤器。使用这种过滤器时,如果扫描到列,那么证明该行必然存在,所以在做行数统计的时候速度非常快。

5. 行过滤器(ROWFilter)

行过滤器的作用是针对rowkey进行过滤。
RowFilter filter = new RowFilter(CompareFilter.CompareOp.GREATER,new SubstringComparator(“1001”));
解释:用RowFilter类实例化一个filter对象,并将1001这个参数传入到这个对象中

6. 前缀过滤器(PrefixFilter)

前缀过滤器是一种行健过滤器,可以根据行健的前缀,来匹配行健包含指定前缀的数据。
PrefixFilter filter= new PrefixFilter(Bytes.toBytes(“row”));

7. 随机行过滤器(RandomRowFilter)

当你想随机抽取系统的一部分数据的时候,可以使用随机行过滤器,这种过滤器适用于数据分析时,对系统数据进行采样的场景,通过随机行过滤器,让你可以随机的选择系统中的一部分数据
随机行过滤器的构造函数是: RandomRowFilter(float chance)chance是一个用来比较的数值,它的取值范围从0~1.0。当扫描器遍历数据的时候,每遍历到一行数据,Hbase就会调用Random来得出一个随机数,并用这个随机数跟你提供的chance值进行比较,如果比较的结果是随机数比chance小,该条记录就会被选择出来。

8. 列族过滤器(FamilyFilter)

针对列族来进行过滤。
FamilyFilter filter= new FamilyFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(“info”));

上代码:

  1. public class HBaseFilter {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. //获取配置文件
  5. Configuration config = HBaseConfiguration.create();
  6. config.set("hbase.zookeeper.quorum", "192.168.141.30");
  7. config.set("hbase.master","localhost:16010");
  8. config.set("hbase.zookeeper.property.clientPort", "2181");
  9. try {
  10. //创建链接
  11. Connection connection = ConnectionFactory.createConnection(config);
  12. Admin admin = connection.getAdmin();
  13. //链接Table接口
  14. Table table = connection.getTable(TableName.valueOf("test"));
  15. Scan scan = new Scan();
  16. //值过滤器
  17. ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("wang"));
  18. scan.setFilter(filter);
  19. ResultScanner rs = table.getScanner(scan);
  20. for(Result result : rs) {
  21. Cell[] cells = result.rawCells();
  22. for(Cell cell : cells) {
  23. System.out.println("Value=" + Bytes.toString(CellUtil.cloneValue(cell)));
  24. }
  25. }
  26. rs.close();
  27. table.close();
  28. connection.close();
  29. /* //单列值过滤器
  30. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, new SubstringComparator("wang"));
  31. scan.setFilter(filter);
  32. ResultScanner rs = table.getScanner(scan);
  33. for(Result result : rs) {
  34. String name = Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name")));
  35. System.out.println("Value=" + name);
  36. }
  37. rs.close();
  38. table.close();
  39. connection.close();
  40. */
  41. /* //列键过滤器
  42. KeyOnlyFilter filter = new KeyOnlyFilter();
  43. scan.setFilter(filter);
  44. ResultScanner rs = table.getScanner(scan);
  45. for(Result result : rs) {
  46. String rowkey = Bytes.toString(result.getRow());
  47. System.out.println("Rowkey=" + rowkey);
  48. Cell[] cells = result.rawCells();
  49. for(Cell cell : cells) {
  50. System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  51. }
  52. }
  53. rs.close();
  54. table.close();
  55. connection.close();
  56. */
  57. /* //首次列键过滤器
  58. FirstKeyOnlyFilter filter = new FirstKeyOnlyFilter();
  59. scan.setFilter(filter);
  60. ResultScanner rs = table.getScanner(scan);
  61. int count = 0;
  62. for(Result result : rs) {
  63. count ++;
  64. }
  65. System.out.println("该表共有:" + count + "行");
  66. rs.close();
  67. table.close();
  68. connection.close();
  69. */
  70. /* //行过滤器
  71. RowFilter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("1002")));
  72. scan.setFilter(filter);
  73. ResultScanner rs = table.getScanner(scan);
  74. for(Result result : rs) {
  75. Cell[] cells = result.rawCells();
  76. for(Cell cell : cells) {
  77. System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
  78. System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
  79. System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  80. System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  81. }
  82. }
  83. rs.close();
  84. table.close();
  85. connection.close();
  86. */
  87. /* //前缀过滤器
  88. PrefixFilter filter = new PrefixFilter(Bytes.toBytes("100"));
  89. scan.setFilter(filter);
  90. ResultScanner rs = table.getScanner(scan);
  91. for(Result result : rs) {
  92. Cell[] cells = result.rawCells();
  93. for(Cell cell : cells) {
  94. System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
  95. System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
  96. System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  97. System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  98. }
  99. }
  100. rs.close();
  101. table.close();
  102. connection.close();
  103. */
  104. /* //随机行过滤器
  105. RandomRowFilter filter = new RandomRowFilter(new Float(0.8));
  106. scan.setFilter(filter);
  107. ResultScanner rs = table.getScanner(scan);
  108. for(Result result : rs) {
  109. String rowkey = Bytes.toString(result.getRow());
  110. System.out.println("Rowkey=" + rowkey);
  111. }
  112. rs.close();
  113. table.close();
  114. connection.close();
  115. */
  116. /* //列族过滤器
  117. FamilyFilter filter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("info2")));
  118. scan.setFilter(filter);
  119. ResultScanner rs = table.getScanner(scan);
  120. for(Result result : rs) {
  121. Cell[] cells = result.rawCells();
  122. for(Cell cell : cells) {
  123. System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
  124. System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
  125. System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  126. System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  127. }
  128. }
  129. rs.close();
  130. table.close();
  131. connection.close();
  132. */
  133. /* //列过滤器
  134. QualifierFilter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("tname")));
  135. scan.setFilter(filter);
  136. ResultScanner rs = table.getScanner(scan);
  137. for(Result result : rs) {
  138. Cell[] cells = result.rawCells();
  139. for(Cell cell : cells) {
  140. System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
  141. System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
  142. System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  143. System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  144. }
  145. }
  146. rs.close();
  147. table.close();
  148. connection.close();
  149. */
  150. }catch(IOException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. }

1. 值过滤器

代码:
该过滤器使用单元格的值来过滤数据,只有满足条件、指定值的单元格才会被返回

//值过滤器
ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("wang"));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for(Result result : rs) {
    Cell[] cells = result.rawCells();
    for(Cell cell : cells) {
        System.out.println("Value=" + Bytes.toString(CellUtil.cloneValue(cell)));
    }
}

实验结果:
_%TFTUV[~NVCSNL_]WVMZIU.png

2.单列值过滤器

该过滤器类似于关系型数据库where条件语句,通过判断数据行指定的列限定符对应的值是否匹配指定的条件(如:等于1000)来决定是否将改数据行返回
代码:

SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info1"), Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL, new SubstringComparator("wang"));
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for(Result result : rs) {
    String name = Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name")));
    System.out.println("Value=" + name);
}

实验结果
GE$8T}6%61CH1KN4USM@(~4.png

3.列键过滤器

该过滤器使得查询结果只返回行键

KeyOnlyFilter filter = new KeyOnlyFilter();
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
for(Result result : rs) {
    String rowkey = Bytes.toString(result.getRow());
    System.out.println("Rowkey=" + rowkey);
    Cell[] cells = result.rawCells();
    for(Cell cell : cells) {
        System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
    }
}
rs.close();
table.close();
connection.close();

![TRRDGZ1@A~L5107$V9FM94.png

4. 首次列键过滤器

FirstKeyOnlyFilter filter = new FirstKeyOnlyFilter();
scan.setFilter(filter);
ResultScanner rs = table.getScanner(scan);
int count = 0;
for(Result result : rs) {
    count ++;
}
System.out.println("该表共有:" + count + "行");
rs.close();
table.close();
connection.close();

image.png

5TWA}%5$1_@HOBB(1CDGKXM.png
3IB9DZ~LL1JG1CYC$_V}XVX.png
Z3FB89IX5F{FC%7C)HEE`82.png
$P{426OATW@H(PSOH`B}XYS.png
E$))JSW@WDA87O210}94N(T.png