hbase各种遍历查询shell语句 包含过滤组合条件

 

  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.Arrays;  
  4. import java.util.List;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.hbase.Cell;  
  8. import org.apache.hadoop.hbase.CellUtil;  
  9. import org.apache.hadoop.hbase.HBaseConfiguration;  
  10. import org.apache.hadoop.hbase.TableName;  
  11. import org.apache.hadoop.hbase.client.Admin;  
  12. import org.apache.hadoop.hbase.client.Connection;  
  13. import org.apache.hadoop.hbase.client.ConnectionFactory;  
  14. import org.apache.hadoop.hbase.client.Get;  
  15. import org.apache.hadoop.hbase.client.Result;  
  16. import org.apache.hadoop.hbase.client.ResultScanner;  
  17. import org.apache.hadoop.hbase.client.Scan;  
  18. import org.apache.hadoop.hbase.client.Table;  
  19. import org.apache.hadoop.hbase.filter.BinaryComparator;  
  20. import org.apache.hadoop.hbase.filter.ColumnCountGetFilter;  
  21. import org.apache.hadoop.hbase.filter.ColumnPaginationFilter;  
  22. import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;  
  23. import org.apache.hadoop.hbase.filter.ColumnRangeFilter;  
  24. import org.apache.hadoop.hbase.filter.DependentColumnFilter;  
  25. import org.apache.hadoop.hbase.filter.FamilyFilter;  
  26. import org.apache.hadoop.hbase.filter.Filter;  
  27. import org.apache.hadoop.hbase.filter.FilterList;  
  28. import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;  
  29. import org.apache.hadoop.hbase.filter.FuzzyRowFilter;  
  30. import org.apache.hadoop.hbase.filter.InclusiveStopFilter;  
  31. import org.apache.hadoop.hbase.filter.KeyOnlyFilter;  
  32. import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;  
  33. import org.apache.hadoop.hbase.filter.PageFilter;  
  34. import org.apache.hadoop.hbase.filter.PrefixFilter;  
  35. import org.apache.hadoop.hbase.filter.QualifierFilter;  
  36. import org.apache.hadoop.hbase.filter.RandomRowFilter;  
  37. import org.apache.hadoop.hbase.filter.RegexStringComparator;  
  38. import org.apache.hadoop.hbase.filter.RowFilter;  
  39. import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;  
  40. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;  
  41. import org.apache.hadoop.hbase.filter.SkipFilter;  
  42. import org.apache.hadoop.hbase.filter.SubstringComparator;  
  43. import org.apache.hadoop.hbase.filter.TimestampsFilter;  
  44. import org.apache.hadoop.hbase.filter.ValueFilter;  
  45. import org.apache.hadoop.hbase.filter.WhileMatchFilter;  
  46. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;  
  47. import org.apache.hadoop.hbase.util.Bytes;  
  48. import org.apache.hadoop.hbase.util.Pair;  
  49.   
  50. public class HbaseUtils {  
  51.   
  52.     public static Admin admin = null;  
  53.     public static Connection conn = null;  
  54.   
  55.     public HbaseUtils() {  
  56.         Configuration conf = HBaseConfiguration.create();  
  57.         conf.set("hbase.zookeeper.quorum", "h71:2181");  
  58.         conf.set("hbase.rootdir", "hdfs://h71:9000/hbase");   
  59.         try {  
  60.             conn = ConnectionFactory.createConnection(conf);  
  61.             admin = conn.getAdmin();  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.     }  
  66.     public static void main(String[] args) throws Exception {  
  67.         HbaseUtils hbase = new HbaseUtils();  
  68.         //1,FamilyFilter:基于“列族”来过滤数据;  
  69. //      hbase.FamilyFilter("scores");  
  70.         //2,QualifierFilter:基于“列名”来过滤数据;  
  71. //      hbase.QualifierFilter("scores");  
  72.         //3.RowFilter:基于rowkey来过滤数据;  
  73. //      hbase.RowFilter("scores","zhangsan01");  
  74.         //4.PrefixFilter:基于rowkey前缀来过滤数据;  
  75. //      hbase.PrefixFilter("scores","zhang");  
  76.         //后缀过滤数据  
  77. //      hbase.HouZui("scores");  
  78.         //5,ColumnPrefixFilter:基于列名前缀来过滤数据;  
  79. //      hbase.ColumnPrefixFilter("scores");  
  80.         //6,MultipleColumnPrefixFilter:ColumnPrefixFilter的加强版;  
  81. //      hbase.MultipleColumnPrefixFilter("scores");  
  82.         //7,ColumnCountGetFilter:限制每行返回多少列;  
  83. //      hbase.columnCountGetFilter();  
  84.         //8,ColumnPaginationFilter:对一行的所有列分页,只返回[limit, offset]范围内的列;  
  85. //      hbase.ColumnPaginationFilter("scores");  
  86.         //9,ColumnRangeFilter:可用于获得一个范围的列  
  87. //      hbase.ColumnRangeFilter("scores");  
  88.         //10,DependentColumnFilter:返回(与(符合条件[列族,列名]或[列族,列名,值]的参考列)具有相同的时间戳)的所有列,即:基于比较器过滤参考列,基于参考列的时间戳过滤其他列;  
  89. //      hbase.DependentColumnFilter("scores");  
  90.         //11,FirstKeyOnlyFilter:结果只返回每行的第一个值对;  
  91. //      hbase.FirstKeyOnlyFilter("scores");  
  92.         //12,FuzzyRowFilter:模糊row查询;  
  93. //      hbase.FuzzyRowFilter("scores");  
  94.         //13,InclusiveStopFilter:将stoprow也一起返回;  
  95. //      hbase.InclusiveStopFilter("scores");  
  96.         //14,KeyOnlyFilter:只返回行键;  
  97. //      hbase.KeyOnlyFilter("scores");  
  98.         //15,PageFilter: 取回XX条数据  ;  
  99. //      hbase.PageFilter("scores");  
  100.         //16,RandomRowFilter:随机获取一定比例(比例为参数)的数据;  
  101. //      hbase.RandomRowFilter("scores");  
  102.         //17,SingleColumnValueFilter:基于参考列的值来过滤数据;  
  103. //      hbase.SingleColumnValueFilter("scores");  
  104.         //18,ValueFilter:基于值来过滤数据;  
  105. //      hbase.ValueFilter("scores");  
  106.         //19,SkipFilter:当过滤器发现某一行中的一列要过滤时,就将整行数据都过滤掉;  
  107. //      hbase.SkipFilter("scores");  
  108.         //20,TimestampsFilter:基于时间戳来过滤数据;  
  109. //      hbase.TimestampsFilter("scores");  
  110.         //21,WhileMatchFilter:一旦遇到一条符合过滤条件的数据,就停止扫描;  
  111. //      hbase.WhileMatchFilter("scores");  
  112.         //22,FilterList:多个过滤器组合过滤。  
  113. //      hbase.FilterList("scores");  
  114.     }  
  115.   
  116.     /** 
  117.     1,FamilyFilter 
  118.             a,按family(列族)查找,取回所有符合条件的“family” 
  119.             b,构造方法第一个参数为compareOp 
  120.             c,第二个参数为WritableByteArrayComparable,有BinaryComparator, BinaryPrefixComparator, 
  121.              BitComparator, NullComparator, RegexStringComparator, SubstringComparator这些类, 
  122.                       最常用的为BinaryComparator 
  123.     */  
  124.     public void FamilyFilter(String tableName) throws Exception {  
  125.         Table table = conn.getTable(TableName.valueOf(tableName));  
  126.         Scan scan = new Scan();  
  127.         Filter filter = new FamilyFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("grc")));  
  128.         scan.setFilter(filter);  
  129.         ResultScanner scanner = table.getScanner(scan);  
  130.         for (Result r : scanner) {  
  131.             for (Cell cell : r.rawCells()) {  
  132.                 System.out.println(  
  133.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  134.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  135.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  136.             }  
  137.         }  
  138.     }  
  139. /* 
  140. hbase(main):224:0> scan 'scores', {FILTER => "FamilyFilter(<=,'binary:grc')"} 
  141. 或者 
  142. hbase(main):011:0> scan 'scores', FILTER => "FamilyFilter(<=,'binary:grc')" 
  143. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  144.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  145.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  146.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  147.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  148.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  149.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  150.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  151.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  152.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  153. 3 row(s) in 0.0220 seconds  
  154. */  
  155.       
  156.     /** 
  157.     2,QualifierFilter 
  158.        类似于FamilyFilter,取回所有符合条件的“列” 
  159.        构造方法第一个参数   compareOp 
  160.        第二个参数为WritableByteArrayComparable 
  161.     */  
  162.     public void QualifierFilter(String tableName) throws Exception {  
  163.         Table table = conn.getTable(TableName.valueOf(tableName));  
  164.         Scan scan = new Scan();  
  165.         Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("grc")));  
  166.         //这里输的参数是相应位置比大小,及当输入ms的时候,所有列名的第一位小于等于m,如果第一位相等则比较第二位的大小。一开始没理解,所以一开始参数输入math或course的时候把我整懵了。  
  167.         scan.setFilter(filter);  
  168.         ResultScanner scanner = table.getScanner(scan);  
  169.         for (Result r : scanner) {  
  170.             for (Cell cell : r.rawCells()) {  
  171.                 System.out.println(  
  172.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  173.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  174.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  175.             }  
  176.         }  
  177.     }  
  178. /* 
  179. hbase(main):221:0> scan 'scores', {FILTER => "QualifierFilter(<=,'binary:b')"} 
  180. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  181.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  182.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  183.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  184.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  185.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  186.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  187. 3 row(s) in 0.0470 seconds 
  188. */  
  189.       
  190.     /** 
  191.     3,RowFilter 
  192.         构造方法参数设置类似于FamilyFilter,符合条件的row都返回 
  193.         但是通过row查询时,如果知道开始结束的row,还是用scan的start和end方法更直接并且经测试速度快一半以上 
  194.     */  
  195.     public void RowFilter(String tableName, String reg) throws Exception {  
  196.         Table table = conn.getTable(TableName.valueOf(tableName));  
  197.         Scan scan = new Scan();  
  198.         //这个参数EQUAL很重要,如果参数不同,查询的结果也会不同  
  199. //      RowFilter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(reg)));//这样写也行  
  200. //      Filter filter = new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(reg)));  
  201.         Filter filter = new RowFilter(CompareOp.LESS_OR_EQUAL,  new BinaryComparator(Bytes.toBytes(reg)));  
  202.         scan.setFilter(filter);  
  203.         ResultScanner scanner = table.getScanner(scan);  
  204.         for (Result r : scanner) {  
  205.             for (Cell cell : r.rawCells()) {  
  206.                 System.out.println(  
  207.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  208.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  209.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  210.             }  
  211.         }  
  212.     /** 
  213.     * 更推荐用下面的方法直接指定起止行,因为filter本质上还是会遍历全部数据,而设定起止行后会直接从指定行开始,指定行结束,效率高很多。 
  214.     */  
  215.     // scan.setStartRow(Bytes.toBytes("AAAAAAAAAAAA"));  
  216.     // scan.setStopRow(Bytes.toBytes( "AAAAAAAAABBB"));  
  217.     }  
  218. /* 
  219. hbase(main):004:0> scan 'scores', {FILTER => "RowFilter(<=,'binary:zhangsan01')"} 
  220. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  221.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  222.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  223.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  224.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  225.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  226.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  227. 2 row(s) in 0.0210 seconds 
  228. */  
  229.       
  230.     /** 
  231.     4,PrefixFilter 
  232.         取回rowkey以指定prefix开头的所有行 
  233.     */  
  234.     public void PrefixFilter(String tableName, String reg) throws Exception {  
  235.         Table table = conn.getTable(TableName.valueOf(tableName));  
  236.         Scan scan = new Scan();  
  237.         Filter filter = new PrefixFilter(Bytes.toBytes("zhang"));  
  238.         scan.setFilter(filter);  
  239.         ResultScanner scanner = table.getScanner(scan);  
  240.         for (Result r : scanner) {  
  241.             for (Cell cell : r.rawCells()) {  
  242.                 System.out.println(  
  243.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  244.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  245.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  246.             }  
  247.         }  
  248.     }  
  249. /* 
  250. hbase(main):022:0> scan 'scores', {FILTER => org.apache.hadoop.hbase.filter.PrefixFilter.new(org.apache.hadoop.hbase.util.Bytes.toBytes('li'))} 
  251. 或者 
  252. hbase(main):004:0> scan 'scores', {FILTER => "PrefixFilter('li')"} 
  253. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  254.  lisi01                                                      column=course:art, timestamp=1489747672249, value=89                                                                                                                             
  255.  lisi01                                                      column=course:math, timestamp=1489747666861, value=89                                                                                                                            
  256.  lisi01                                                      column=grade:, timestamp=1489747677402, value=201                                                                                                                                
  257. 1 row(s) in 0.0110 seconds 
  258. */  
  259.       
  260.     /** 
  261.     由于其原生带有PrefixFilter这种对ROWKEY的前缀过滤查询,因此想着实现的后缀查询的过程中,发现这一方面相对来说还是空白。 
  262.     因此,只能采用一些策略来实现,主要还是采用正则表达式的方式。  
  263.     */  
  264.     public void HouZui(String tableName) throws Exception {  
  265.         Table table = conn.getTable(TableName.valueOf(tableName));  
  266.         Scan scan = new Scan();  
  267.         Filter filter = new RowFilter(CompareOp.EQUAL,new RegexStringComparator(".*n01"));  
  268.         scan.setFilter(filter);  
  269.         ResultScanner scanner = table.getScanner(scan);  
  270.         for (Result r : scanner) {  
  271.             for (Cell cell : r.rawCells()) {  
  272.                 System.out.println(  
  273.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  274.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  275.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  276.             }  
  277.         }  
  278.     }  
  279. /* 
  280. hbase(main):020:0> scan 'scores', {FILTER => "RowFilter(=,'regexstring:.*n01')"} 
  281. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  282.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  283.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  284.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  285. 1 row(s) in 0.0080 seconds 
  286. */  
  287.       
  288.     /** 
  289.     5,ColumnPrefixFilter 
  290.     */  
  291.     public void ColumnPrefixFilter(String tableName) throws Exception {  
  292.         Table table = conn.getTable(TableName.valueOf(tableName));  
  293.         Scan scan = new Scan();  
  294.         byte[] prefix = Bytes.toBytes("ar");  
  295.         Filter filter = new ColumnPrefixFilter(prefix);  
  296.         scan.setFilter(filter);  
  297.         ResultScanner scanner = table.getScanner(scan);  
  298.         for (Result r : scanner) {  
  299.             for (Cell cell : r.rawCells()) {  
  300.                 System.out.println(  
  301.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  302.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  303.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  304.             }  
  305.         }  
  306.     }  
  307. /* 
  308. hbase(main):021:0> scan 'scores', {FILTER => "ColumnPrefixFilter('ar')"} 
  309. 或者 
  310. hbase(main):022:0> scan 'scores', {FILTER => org.apache.hadoop.hbase.filter.ColumnPrefixFilter.new(org.apache.hadoop.hbase.util.Bytes.toBytes('ar'))} 
  311. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  312.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  313.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  314.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  315. 3 row(s) in 0.0140 seconds 
  316. */  
  317.       
  318.     /** 
  319.     6,MultipleColumnPrefixFilter 
  320.             a,返回有此前缀的所有列, 
  321.             b,在byte[][]中定义所有需要的列前缀,只要满足其中一条约束就会被返回(ColumnPrefixFilter的加强版), 
  322.     */  
  323.     public void MultipleColumnPrefixFilter(String tableName) throws Exception {  
  324.         Table table = conn.getTable(TableName.valueOf(tableName));  
  325.         Scan scan = new Scan();  
  326.         byte[][] prefix = {Bytes.toBytes("ar"),Bytes.toBytes("ma")};  
  327.         Filter filter = new MultipleColumnPrefixFilter(prefix);  
  328.         scan.setFilter(filter);  
  329.         ResultScanner scanner = table.getScanner(scan);  
  330.         for (Result r : scanner) {  
  331.             for (Cell cell : r.rawCells()) {  
  332.                 System.out.println(  
  333.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  334.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  335.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  336.             }  
  337.         }  
  338.     }  
  339. /* 
  340. hbase(main):023:0> scan 'scores', {FILTER => "MultipleColumnPrefixFilter('ar','ma')"} 
  341. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  342.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  343.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  344.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  345.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  346.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  347.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  348. 3 row(s) in 0.0290 seconds 
  349. */  
  350.       
  351.     /** 
  352.     7,ColumnCountGetFilter 
  353.           a,无法再scan中使用,只能在Get中 
  354.           b,若设为0,则无法返回数据,设为几就按服务器中存储位置取回几列 
  355.           c,可用size()取到列数,观察效果 
  356.     */  
  357.     public void columnCountGetFilter() throws Exception {  
  358.         Table table = conn.getTable(TableName.valueOf("scores"));  
  359.         Get get = new Get(Bytes.toBytes("zhangsan01"));    
  360.         get.setFilter(new ColumnCountGetFilter(2));  
  361.         Result result = table.get(get);    
  362.         //输出结果size,观察效果  
  363.         System.out.println(result.size());  
  364. //        byte[] value1 = result.getValue("course".getBytes(), "art".getBytes());    
  365. //        byte[] value2 = result.getValue("course".getBytes(), "math".getBytes());    
  366. //        System.out.println("course:art"+"-->"+new String(value1)+"  "    
  367. //                +"course:math"+"-->"+new String(value2));  
  368.     }  
  369. /* 
  370. hbase(main):026:0> scan 'scores', {FILTER => "ColumnCountGetFilter(2)"} 
  371. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  372.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  373.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  374.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  375.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  376.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  377.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  378. 3 row(s) in 0.0120 seconds 
  379. */  
  380.       
  381.     /** 
  382.     8,ColumnPaginationFilter 
  383.           a,limit 表示返回列数 
  384.           b,offset 表示返回列的偏移量,如果为0,则全部取出,如果为1,则返回第二列及以后 
  385.     */  
  386.     public void ColumnPaginationFilter(String tableName) throws Exception {  
  387.         Table table = conn.getTable(TableName.valueOf(tableName));  
  388.         Scan scan = new Scan();  
  389.         Filter filter = new ColumnPaginationFilter(2,1);  
  390.         scan.setFilter(filter);  
  391. //      用addFamily增加列族后,会只返回指定列族的数据  
  392.         scan.addFamily(Bytes.toBytes("course"));  
  393.         ResultScanner scanner = table.getScanner(scan);  
  394.         for (Result r : scanner) {  
  395.             for (Cell cell : r.rawCells()) {  
  396.                 System.out.println(  
  397.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  398.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  399.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  400.             }  
  401.         }  
  402.     }  
  403. /* 
  404. hbase(main):031:0> scan 'scores',{FILTER=>org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(2,1)} 
  405. 或者 
  406. hbase(main):030:0> scan 'scores',{FILTER=> "ColumnPaginationFilter(2,1)"} 
  407. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  408.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  409.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  410.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  411.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  412.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  413.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  414. 3 row(s) in 0.0100 seconds 
  415. */  
  416.       
  417.     /** 
  418.     9,ColumnRangeFilter 
  419.     构造函数: 
  420.     ColumnRangeFilter(byte[] minColumn, boolean minColumnInclusive, byte[] maxColumn, boolean maxColumnInclusive) 
  421.     *可用于获得一个范围的列,例如,如果你的一行中有百万个列,但是你只希望查看列名为bbbb到dddd的范围 
  422.     *该过滤器可以进行高效的列名内部扫描。(为何是高效呢???因为列名是已经按字典排序好的)HBase-0.9.2 版本引入该功能。 
  423.     *一个列名是可以出现在多个列族中的,该过滤器将返回所有列族中匹配的列 
  424.     */  
  425.     public void ColumnRangeFilter(String tableName) throws Exception {  
  426.         Table table = conn.getTable(TableName.valueOf(tableName));  
  427.         Scan scan = new Scan();  
  428.         Filter filter = new ColumnRangeFilter(Bytes.toBytes("a"),true, Bytes.toBytes("n"),true);  
  429.         scan.setFilter(filter);  
  430.         ResultScanner scanner = table.getScanner(scan);  
  431.         for (Result r : scanner) {  
  432.             for (Cell cell : r.rawCells()) {  
  433.                 System.out.println(  
  434.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  435.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  436.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  437.             }  
  438.         }  
  439.     }  
  440. /* 
  441. hbase(main):032:0> scan 'scores',{FILTER=> "ColumnRangeFilter('a',true,'n',true)"} 
  442. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  443.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  444.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  445.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  446.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  447.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  448.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  449. 3 row(s) in 0.0140 seconds 
  450. */  
  451.       
  452.     /** 
  453.     10, DependentColumnFilter (该过滤器有两个参数:family和Qualifier,尝试找到该列所在的每一行, 
  454.         并返回该行具有相同时间戳的全部键值对。如果某一行不包含指定的列,则该行的任何键值对都不返回, 
  455.         该过滤器还可以有一个可选的布尔参数-如果为true,从属的列不返回; 
  456.         该过滤器还可以有两个可选的参数--一个比较操作符和一个值比较器,用于family和Qualifier 
  457.         的进一步检查,如果从属的列找到,其值还必须通过值检查,然后就是时间戳必须考虑) 
  458.     */  
  459.     public void DependentColumnFilter(String tableName) throws Exception {  
  460.         Table table = conn.getTable(TableName.valueOf(tableName));  
  461.         Scan scan = new Scan();  
  462. //      Filter filter = new DependentColumnFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),false);  
  463. //      Filter filter = new DependentColumnFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),true);  
  464.         Filter filter = new DependentColumnFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),false,CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("90")));  
  465. //      Filter filter = new DependentColumnFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),true,CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("90")));  
  466.         //上面这四种情况输出的for循环中的内容也不一样,要做相应的修改,否则会报java.lang.NullPointerException  
  467.         scan.setFilter(filter);  
  468.         ResultScanner scanner = table.getScanner(scan);  
  469.         for (Result r : scanner) {  
  470.             for (Cell cell : r.rawCells()) {  
  471.                 System.out.println(  
  472.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  473.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  474.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  475.             }  
  476.         }  
  477.     }  
  478. /* 
  479. hbase(main):036:0> scan 'scores',{FILTER=> "DependentColumnFilter('course','art',false,=,'binary:90')"} 
  480. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  481.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  482.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  483.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  484.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  485. 2 row(s) in 0.0160 seconds 
  486. */  
  487.       
  488.     /** 
  489.     11,FirstKeyOnlyFilter 
  490.           如名字所示,结果只返回每行的第一个值对 
  491.     */  
  492.     public void FirstKeyOnlyFilter(String tableName) throws Exception {  
  493.         Table table = conn.getTable(TableName.valueOf(tableName));  
  494.         Scan scan = new Scan();  
  495.         Filter filter = new FirstKeyOnlyFilter();  
  496.         scan.setFilter(filter);  
  497.         ResultScanner scanner = table.getScanner(scan);  
  498.         for (Result r : scanner) {  
  499.             for (Cell cell : r.rawCells()) {  
  500.                 System.out.println(  
  501.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  502.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  503.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  504.             }  
  505.         }  
  506.     }  
  507. /* 
  508. hbase(main):037:0> scan 'scores',{FILTER=> "FirstKeyOnlyFilter()"} 
  509. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  510.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  511.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  512.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  513. 3 row(s) in 0.0160 seconds 
  514. */  
  515.       
  516.     /** 
  517.     12,FuzzyRowFilter 
  518.             模糊row查询 
  519.         pair中第一个参数为模糊查询的string 
  520.             第二个参数为byte[]其中装与string位数相同的数值0或1,0表示该位必须与string中值相同,1表示可以不同          
  521.     */  
  522.     public void FuzzyRowFilter(String tableName) throws Exception {  
  523.         Table table = conn.getTable(TableName.valueOf(tableName));  
  524.         Scan scan = new Scan();  
  525.         Filter filter = new FuzzyRowFilter( Arrays.asList(new Pair<byte[], byte[]>(Bytes.toBytes("zhangsan01"),  
  526.                         new byte[] {0, 0, 0, 0 , 0, 0, 0, 0, 0, 1})));  
  527.         scan.setFilter(filter);  
  528.         ResultScanner scanner = table.getScanner(scan);  
  529.         for (Result r : scanner) {  
  530.             for (Cell cell : r.rawCells()) {  
  531.                 System.out.println(  
  532.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  533.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  534.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  535.             }  
  536.         }  
  537.     }  
  538. /* 
  539. 。。。。。 
  540. */  
  541.       
  542.     /** 
  543.     13,InclusiveStopFilter 
  544.         指定stopRow,程序在scan时从头扫描全部返回,直到stopRow停止(stopRow这行也会返回,然后scan停止)  
  545.     */  
  546.     public void InclusiveStopFilter(String tableName) throws Exception {  
  547.         Table table = conn.getTable(TableName.valueOf(tableName));  
  548.         Scan scan = new Scan();  
  549.         Filter filter = new InclusiveStopFilter(Bytes.toBytes("zhangsan01"));  
  550.         scan.setFilter(filter);  
  551.         ResultScanner scanner = table.getScanner(scan);  
  552.         for (Result r : scanner) {  
  553.             for (Cell cell : r.rawCells()) {  
  554.                 System.out.println(  
  555.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  556.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  557.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  558.             }  
  559.         }  
  560.     }  
  561. /* 
  562. hbase(main):012:0> scan 'scores', {FILTER => "InclusiveStopFilter('zhangsan01')"} 
  563. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  564.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  565.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  566.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  567.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  568.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  569.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  570. 2 row(s) in 0.0170 seconds 
  571. */  
  572.       
  573.     /** 
  574.     14,KeyOnlyFilter 
  575.        只取key值,size正常,说明value不是没取而是在取的时候被重写为空(能打印,不是null) 
  576.      lenAsVal这个值没大搞明白,当设为false时打印为空,如果设为true时打印的将会是“口口口口” 
  577.     */  
  578.     public void KeyOnlyFilter(String tableName) throws Exception {  
  579.         Table table = conn.getTable(TableName.valueOf(tableName));  
  580.         Scan scan = new Scan();  
  581.         Filter filter = new KeyOnlyFilter(true);  
  582.         scan.setFilter(filter);  
  583.         ResultScanner scanner = table.getScanner(scan);  
  584.         for (Result r : scanner) {  
  585.             for (Cell cell : r.rawCells()) {  
  586.                 System.out.println(  
  587.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  588.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  589.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  590.             }  
  591.         }  
  592.     }  
  593. /* 
  594. hbase(main):016:0> scan 'scores', {FILTER => "KeyOnlyFilter(true)"} 
  595. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  596.  lisi01                                                      column=course:art, timestamp=1498003655021, value=\x00\x00\x00\x02                                                                                                               
  597.  lisi01                                                      column=course:math, timestamp=1498003561726, value=\x00\x00\x00\x02                                                                                                              
  598.  lisi01                                                      column=grade:, timestamp=1498003561726, value=\x00\x00\x00\x03                                                                                                                   
  599.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=\x00\x00\x00\x02                                                                                                               
  600.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=\x00\x00\x00\x02                                                                                                              
  601.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=\x00\x00\x00\x03                                                                                                                   
  602.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=\x00\x00\x00\x02                                                                                                               
  603.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=\x00\x00\x00\x02                                                                                                              
  604.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=\x00\x00\x00\x03                                                                                                                   
  605. 3 row(s) in 0.0320 seconds 
  606. hbase(main):015:0> scan 'scores', {FILTER => "KeyOnlyFilter(false)"} 
  607. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  608.  lisi01                                                      column=course:art, timestamp=1498003655021, value=                                                                                                                               
  609.  lisi01                                                      column=course:math, timestamp=1498003561726, value=                                                                                                                              
  610.  lisi01                                                      column=grade:, timestamp=1498003561726, value=                                                                                                                                   
  611.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=                                                                                                                               
  612.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=                                                                                                                              
  613.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=                                                                                                                                   
  614.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=                                                                                                                               
  615.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=                                                                                                                              
  616.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=                                                                                                                                   
  617. 3 row(s) in 0.0190 seconds 
  618. */  
  619.       
  620.     /** 
  621.     15,PageFilter 
  622.         取回XX条数据   
  623.     */  
  624.     public void PageFilter(String tableName) throws Exception {  
  625.         Table table = conn.getTable(TableName.valueOf(tableName));  
  626.         Scan scan = new Scan();  
  627.         Filter filter = new PageFilter(2);  
  628.         scan.setFilter(filter);  
  629.         ResultScanner scanner = table.getScanner(scan);  
  630.         for (Result r : scanner) {  
  631.             for (Cell cell : r.rawCells()) {  
  632.                 System.out.println(  
  633.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  634.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  635.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  636.             }  
  637.         }  
  638.     }  
  639. /* 
  640. hbase(main):017:0> scan 'scores', {FILTER => "PageFilter(2)"} 
  641. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  642.  lisi01                                                      column=course:art, timestamp=1498003655021, value=89                                                                                                                             
  643.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  644.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  645.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  646.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  647.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  648. 2 row(s) in 0.0130 seconds 
  649. */  
  650.       
  651.     /** 
  652.     16,RandomRowFilter 
  653.         参数小于0时一条查不出大于1值会返回所有,而想取随机行的话有效区间为0~1,值代表取到的几率 
  654.     */  
  655.     public void RandomRowFilter(String tableName) throws Exception {  
  656.         Table table = conn.getTable(TableName.valueOf(tableName));  
  657.         Scan scan = new Scan();  
  658.         Filter filter = new RandomRowFilter((float)0.5);  
  659.         //即使是0.5有时候也一条查不出来,有时候却全出来了,是几率并不是一定,那我就不知道这个具体有什么实际运用了。。。根据rowkey随机而不是根据列随机  
  660.         scan.setFilter(filter);  
  661.         ResultScanner scanner = table.getScanner(scan);  
  662.         for (Result r : scanner) {  
  663.             for (Cell cell : r.rawCells()) {  
  664.                 System.out.println(  
  665.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  666.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  667.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  668.             }  
  669.         }  
  670.     }  
  671. /* 
  672. 。。。。。 
  673. */  
  674.       
  675.     /** 
  676.     17,SingleColumnValueFilter和SingleColumnValueExcludeFilter 
  677.         用来查找并返回指定条件的列的数据 
  678.             a,如果查找时没有该列,两种filter都会把该行所有数据返回 
  679.             b,如果查找时有该列,但是不符合条件,则该行所有列都不返回 
  680.             c,如果找到该列,并且符合条件,前者返回所有列,后者返回除该列以外的所有列 
  681.      */  
  682.     public void SingleColumnValueFilter(String tableName) throws Exception {  
  683.         Table table = conn.getTable(TableName.valueOf(tableName));  
  684.         Scan scan = new Scan();  
  685.         //完整匹配字节数组  
  686. //      Filter filter = new SingleColumnValueFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("90")));  
  687.         //匹配正则表达式  
  688. //      Filter filter = new SingleColumnValueFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),CompareOp.EQUAL,new RegexStringComparator("8"));  
  689.         //匹配是否包含子串,大小写不敏感  
  690. //      Filter filter = new SingleColumnValueFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),CompareOp.EQUAL,new SubstringComparator("9"));  
  691.         Filter filter = new SingleColumnValueExcludeFilter(Bytes.toBytes("course"), Bytes.toBytes("art"), CompareOp.EQUAL,new SubstringComparator("9"));  
  692.         scan.setFilter(filter);  
  693.         ResultScanner scanner = table.getScanner(scan);  
  694.         for (Result r : scanner) {  
  695.             for (Cell cell : r.rawCells()) {  
  696.                 System.out.println(  
  697.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  698.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  699.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  700.             }  
  701.         }  
  702.     }  
  703. /* 
  704. hbase(main):032:0> scan 'scores', {FILTER => "SingleColumnValueExcludeFilter('course','art',=,'substring:9')"} 
  705. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  706.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  707.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  708.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  709.  zhangsan01                                                  column=grade:, timestamp=1498003593575, value=101                                                                                                                                
  710.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  711.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  712. 3 row(s) in 0.0150 seconds 
  713. */  
  714.       
  715.     /** 
  716.     18,ValueFilter 
  717.     按value全数据库搜索(全部列的value均会被检索) 
  718.     */  
  719.     public void ValueFilter(String tableName) throws Exception {  
  720.         Table table = conn.getTable(TableName.valueOf(tableName));  
  721.         Scan scan = new Scan();  
  722.         Filter filter = new ValueFilter(CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("102")));  
  723.         scan.setFilter(filter);  
  724.         ResultScanner scanner = table.getScanner(scan);  
  725.         for (Result r : scanner) {  
  726.             for (Cell cell : r.rawCells()) {  
  727.                 System.out.println(  
  728.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  729.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  730.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  731.             }  
  732.         }  
  733.     }  
  734. /* 
  735. NOT_EQUAL不知道该咋么表示。。。。。 
  736. */  
  737.       
  738.     /** 
  739.     19,SkipFilter 
  740.         根据整行中的每个列来做过滤,只要存在一列不满足条件,整行都被过滤掉。 
  741.         例如,如果一行中的所有列代表的是不同物品的重量,则真实场景下这些数值都必须大于零,我们希望将那些包含任意列值为0的行都过滤掉。 
  742.         在这个情况下,我们结合ValueFilter和SkipFilter共同实现该目的: 
  743.         scan.setFilter(new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes(0)))); 
  744.     */  
  745.     public void SkipFilter(String tableName) throws Exception {  
  746.         Table table = conn.getTable(TableName.valueOf(tableName));  
  747.         Scan scan = new Scan();  
  748.         Filter filter = new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("102"))));  
  749. //      Filter filter = new SkipFilter(new DependentColumnFilter(Bytes.toBytes("course"), Bytes.toBytes("art"),false,CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("90"))));  
  750.         //该过滤器需要配合其他过滤器来使用  
  751.         scan.setFilter(filter);  
  752.         ResultScanner scanner = table.getScanner(scan);  
  753.         for (Result r : scanner) {  
  754.             for (Cell cell : r.rawCells()) {  
  755.                 System.out.println(  
  756.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  757.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  758.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  759.             }  
  760.         }  
  761.     }  
  762. /* 
  763. 不知道咋么把两个过滤器嵌套使用。。。。。 
  764. */  
  765.       
  766.     /** 
  767.     20,TimestampsFilter 
  768.         a,按时间戳搜索数据库 
  769.         b,需设定List<Long> 存放所有需要检索的时间戳, 
  770.     */  
  771.     public void TimestampsFilter(String tableName) throws Exception {  
  772.         Table table = conn.getTable(TableName.valueOf(tableName));  
  773.         Scan scan = new Scan();  
  774.         //ls中存放所有需要查找匹配的时间戳  
  775.         List<Long> ls = new ArrayList<Long>();  
  776.         ls.add((long)1498003561726L);  
  777.         ls.add((long)1498003601365L);  
  778.         //java语言的整型常量默认为int型,声明long型常量可以后加”l“或”L“  
  779.         Filter filter = new TimestampsFilter(ls);  
  780.         scan.setFilter(filter);  
  781.         ResultScanner scanner = table.getScanner(scan);  
  782. //      for (Result result : scanner) {  
  783. //          if(new String(result.getRow()).equals("zhangsan01")){  
  784. //              System.out.println(new String(result.getRow())+"  "  
  785. //                  +"course:art"+"-->"+new String(result.getValue(Bytes.toBytes("course"), Bytes.toBytes("art")))+"  "  
  786. //                  +"course:math"+"-->"+new String(result.getValue(Bytes.toBytes("course"), Bytes.toBytes("math"))));  
  787. //          }else if(new String(result.getRow()).equals("zhangsan02")){  
  788. //              System.out.println(new String(result.getRow())+"  "  
  789. //                  +"course:art"+"-->"+new String(result.getValue(Bytes.toBytes("course"), Bytes.toBytes("art")))+"  "  
  790. //                  +"course:math"+"-->"+new String(result.getValue(Bytes.toBytes("course"), Bytes.toBytes("math")))+"  "  
  791. //                  +"grade:"+"-->"+new String(result.getValue(Bytes.toBytes("grade"), Bytes.toBytes(""))));  
  792. //          }else{  
  793. //              System.out.println(new String(result.getRow())+"  "  
  794. //                  +"course:math"+"-->"+new String(result.getValue(Bytes.toBytes("course"), Bytes.toBytes("math")))+"  "  
  795. //                  +"grade:"+"-->"+new String(result.getValue(Bytes.toBytes("grade"), Bytes.toBytes(""))));  
  796. //          }  
  797. //      }  
  798.         for (Result r : scanner) {  
  799.             for (Cell cell : r.rawCells()) {  
  800.                 System.out.println(  
  801.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  802.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  803.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  804.             }  
  805.         }  
  806.     }  
  807. /* 
  808. hbase(main):039:0> scan 'scores', {FILTER => "TimestampsFilter(1498003561726,1498003601365)"} 
  809. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  810.  lisi01                                                      column=course:math, timestamp=1498003561726, value=89                                                                                                                            
  811.  lisi01                                                      column=grade:, timestamp=1498003561726, value=201                                                                                                                                
  812.  zhangsan01                                                  column=course:art, timestamp=1498003561726, value=90                                                                                                                             
  813.  zhangsan01                                                  column=course:math, timestamp=1498003561726, value=99                                                                                                                            
  814.  zhangsan02                                                  column=course:art, timestamp=1498003601365, value=90                                                                                                                             
  815.  zhangsan02                                                  column=course:math, timestamp=1498003561726, value=66                                                                                                                            
  816.  zhangsan02                                                  column=grade:, timestamp=1498003601365, value=102                                                                                                                                
  817. 3 row(s) in 0.0160 seconds 
  818. */  
  819.       
  820.     /** 
  821.     21,WhileMatchFilter 
  822.         相当于while执行,直到不match就break了返回了。 
  823.     */  
  824.     public void WhileMatchFilter(String tableName) throws Exception {  
  825.         Table table = conn.getTable(TableName.valueOf(tableName));  
  826.         Scan scan = new Scan();  
  827.         Filter filter = new WhileMatchFilter(new ValueFilter(CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("101"))));  
  828.         scan.setFilter(filter);  
  829.         ResultScanner scanner = table.getScanner(scan);  
  830.         for (Result r : scanner) {  
  831.             for (Cell cell : r.rawCells()) {  
  832.                 System.out.println(  
  833.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  834.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  835.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  836.             }  
  837.         }  
  838.     }  
  839. /* 
  840. 。。。。。 
  841. */  
  842.       
  843.     /** 
  844.     22,FilterList  
  845.         代表一个过滤器链,它可以包含一组即将应用于目标数据集的过滤器,过滤器间具有“与”FilterList.Operator.MUST_PASS_ALL和“或”FilterList.Operator.MUST_PASS_ONE关系。 
  846.         官网实例代码,两个“或”关系的过滤器的写法: 
  847.     */  
  848.     public void FilterList(String tableName) throws Exception {  
  849.         Table table = conn.getTable(TableName.valueOf(tableName));  
  850.         FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);   //数据只要满足一组过滤器中的一个就可以  
  851.         SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("course"), Bytes.toBytes("math"),CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("89")));  
  852.         list.addFilter(filter1);      
  853.         SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("course"), Bytes.toBytes("math"),CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("66")));  
  854.         list.addFilter(filter2);  
  855.         Scan scan = new Scan();  
  856.         scan.setFilter(list);  
  857.         ResultScanner scanner = table.getScanner(scan);  
  858.         for (Result r : scanner) {  
  859.             for (Cell cell : r.rawCells()) {  
  860.                 System.out.println(  
  861.                         "Rowkey-->"+Bytes.toString(r.getRow())+"  "+  
  862.                         "Familiy:Quilifier-->"+Bytes.toString(CellUtil.cloneQualifier(cell))+"  "+  
  863.                         "Value-->"+Bytes.toString(CellUtil.cloneValue(cell)));  
  864.             }  
  865.         }  
  866.     }  
  867. /* 
  868. hbase(main):009:0> scan 'scores', {FILTER => "PrefixFilter('zhang') OR QualifierFilter(>=,'binary:b')"} 
  869. ROW                                                          COLUMN+CELL                                                                                                                                                                      
  870.  lisi01                                                      column=course:math, timestamp=1489747666861, value=89                                                                                                                            
  871.  lisi01                                                      column=grade:, timestamp=1489747677402, value=201                                                                                                                                
  872.  zhangsan01                                                  column=course:art, timestamp=1489747593360, value=90                                                                                                                             
  873.  zhangsan01                                                  column=course:math, timestamp=1489747589255, value=99                                                                                                                            
  874.  zhangsan01                                                  column=grade:, timestamp=1489747598001, value=101                                                                                                                                
  875.  zhangsan02                                                  column=course:art, timestamp=1489747607561, value=60                                                                                                                             
  876.  zhangsan02                                                  column=course:math, timestamp=1489747602883, value=66                                                                                                                            
  877.  zhangsan02                                                  column=grade:, timestamp=1489747614601, value=102                                                                                                                                
  878. 3 row(s) in 0.0180 seconds 
  879. */  
  880. }  

上面有几个过滤器在hbase shell中没有找出,如果大家有找到的告诉我一声,一起进步。

 

 

参考:
http://blog.csdn.net/blue__yeah/article/details/41040399
http://blog.csdn.net/liangtingac/article/details/40078637
http://blog.csdn.net/u010967382/article/details/37653177
http://blog.csdn.net/sparkexpert/article/details/51942354

 

from:https://blog.csdn.net/m0_37739193/article/details/73615016

 

posted on 2018-04-27 10:50  浪子回头jin不换  阅读(1063)  评论(0编辑  收藏  举报

导航