欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hbase RowFilter如何根据rowkey查询以及实例实现代码

程序员文章站 2022-04-14 16:02:00
...

问题导读:
1.如何根据rowkey模糊查询?
2.如何使用Comparator过滤rowkey?
3.如何查询rowkey中包含有某字符串的数据?

hbase RowFilter如何根据rowkey查询以及实例实现代码
            
    
    博客分类: 技术总结NOSql hbaseRowFilterrowkey查询代码 



RowFilter用于过滤row key
Operator Description
LESS 小于
LESS_OR_EQUAL 小于等于
[EQUAL 等于
NOT_EQUAL 不等于
GREATER_OR_EQUAL 大于等于
GREATER 大于
NO_OP 排除所有

 

Comparator Description
BinaryComparator 使用Bytes.compareTo()比较
BinaryPrefixComparator 和BinaryComparator差不多,从前面开始比较
NullComparator Does not compare against an actual value but whether a given one is null, or not  null.
BitComparator Performs a bitwise comparison, providing a BitwiseOp class with OR, and XOR operators.
RegexStringComparator 正则表达式
SubstringComparator 把数据当成字符串,用contains()来判断

 

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.HColumnDescriptor;
  5. import org.apache.hadoop.hbase.HTableDescriptor;
  6. import org.apache.hadoop.hbase.client.HBaseAdmin;
  7. import org.apache.hadoop.hbase.client.HTable;
  8. import org.apache.hadoop.hbase.client.Put;
  9. import org.apache.hadoop.hbase.client.Result;
  10. import org.apache.hadoop.hbase.client.ResultScanner;
  11. import org.apache.hadoop.hbase.client.Scan;
  12. import org.apache.hadoop.hbase.filter.BinaryComparator;
  13. import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
  14. import org.apache.hadoop.hbase.filter.CompareFilter;
  15. import org.apache.hadoop.hbase.filter.Filter;
  16. import org.apache.hadoop.hbase.filter.RegexStringComparator;
  17. import org.apache.hadoop.hbase.filter.RowFilter;
  18. import org.apache.hadoop.hbase.filter.SubstringComparator;
  19. public class TestHbaseRowFilter {
  20.         String tableName = "test_row_filter";
  21.         Configuration config = HBaseConfiguration.create();
  22.         
  23.         public void testRowFilter() throws IOException {
  24.                 HTable table = new HTable(config, tableName);
  25.                 Scan scan = new Scan();
  26.                 System.out.println("小于等于row010的行");
  27.                 Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
  28.                                 new BinaryComparator("row010".getBytes()));
  29.                 scan.setFilter(filter1);
  30.                 ResultScanner scanner1 = table.getScanner(scan);
  31.                 for (Result res : scanner1) {
  32.                         System.out.println(res);
  33.                 }
  34.                 scanner1.close();
  35.                 System.out.println("正则获取结尾为5的行");
  36.                 Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,
  37.                                 new RegexStringComparator(".*5[        DISCUZ_CODE_0        ]quot;));
  38.                 scan.setFilter(filter2);
  39.                 ResultScanner scanner2 = table.getScanner(scan);
  40.                 for (Result res : scanner2) {
  41.                         System.out.println(res);
  42.                 }
  43.                 scanner2.close();
  44.                 System.out.println("包含有5的行");
  45.                 Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,
  46.                                 new SubstringComparator("5"));
  47.                 scan.setFilter(filter3);
  48.                 ResultScanner scanner3 = table.getScanner(scan);
  49.                 for (Result res : scanner3) {
  50.                         System.out.println(res);
  51.                 }
  52.                 scanner3.close();
  53.                 System.out.println("开头是row01的");
  54.                 Filter filter4 = new RowFilter(CompareFilter.CompareOp.EQUAL,
  55.                                 new BinaryPrefixComparator("row01".getBytes()));
  56.                 scan.setFilter(filter4);
  57.                 ResultScanner scanner4 = table.getScanner(scan);
  58.                 for (Result res : scanner4) {
  59.                         System.out.println(res);
  60.                 }
  61.                 scanner3.close();
  62.         }
  63.         
  64.         public void init() {
  65.                 // 创建表和初始化数据
  66.                 try {
  67.                         HBaseAdmin admin = new HBaseAdmin(config);
  68.                         if (!admin.tableExists(tableName)) {
  69.                                 HTableDescriptor htd = new HTableDescriptor(tableName);
  70.                                 HColumnDescriptor hcd1 = new HColumnDescriptor("data");
  71.                                 htd.addFamily(hcd1);
  72.                                 HColumnDescriptor hcd2 = new HColumnDescriptor("url");
  73.                                 htd.addFamily(hcd2);
  74.                                 admin.createTable(htd);
  75.                         }
  76.                         HTable table = new HTable(config, tableName);
  77.                         table.setAutoFlush(false);
  78.                         int count = 50;
  79.                         for (int i = 1; i <= count; ++i) {
  80.                                 Put p = new Put(String.format("rowd", i).getBytes());
  81.                                 p.add("data".getBytes(), String.format("cold", i % 10)
  82.                                                 .getBytes(), String.format("datad", i).getBytes());
  83.                                 p.add("url".getBytes(), String.format("cold", i % 10)
  84.                                                 .getBytes(), String.format("urld", i).getBytes());
  85.                                 table.put(p);
  86.                         }
  87.                         table.close();
  88.                 } catch (IOException e) {
  89.                         e.printStackTrace();
  90.                 }
  91.         }
  92.         
  93.         public static void main(String[] args) throws IOException {
  94.                 TestHbaseRowFilter test = new TestHbaseRowFilter();
  95.                 test.init();
  96.                 test.testRowFilter();
  97.         }
  98. }
复制代码
输出结果
  1. 小于等于row010的行
  2. keyvalues={row001/data:col1/1364133382268/Put/vlen=7, row001/url:col1/1364133382268/Put/vlen=6}
  3. keyvalues={row002/data:col2/1364133382268/Put/vlen=7, row002/url:col2/1364133382268/Put/vlen=6}
  4. keyvalues={row003/data:col3/1364133382268/Put/vlen=7, row003/url:col3/1364133382268/Put/vlen=6}
  5. keyvalues={row004/data:col4/1364133382268/Put/vlen=7, row004/url:col4/1364133382268/Put/vlen=6}
  6. keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6}
  7. keyvalues={row006/data:col6/1364133382268/Put/vlen=7, row006/url:col6/1364133382268/Put/vlen=6}
  8. keyvalues={row007/data:col7/1364133382268/Put/vlen=7, row007/url:col7/1364133382268/Put/vlen=6}
  9. keyvalues={row008/data:col8/1364133382268/Put/vlen=7, row008/url:col8/1364133382268/Put/vlen=6}
  10. keyvalues={row009/data:col9/1364133382268/Put/vlen=7, row009/url:col9/1364133382268/Put/vlen=6}
  11. keyvalues={row010/data:col0/1364133382268/Put/vlen=7, row010/url:col0/1364133382268/Put/vlen=6}
  12. 正则获取结尾为5的行
  13. keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6}
  14. keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6}
  15. keyvalues={row025/data:col5/1364133382268/Put/vlen=7, row025/url:col5/1364133382268/Put/vlen=6}
  16. keyvalues={row035/data:col5/1364133382268/Put/vlen=7, row035/url:col5/1364133382268/Put/vlen=6}
  17. keyvalues={row045/data:col5/1364133382268/Put/vlen=7, row045/url:col5/1364133382268/Put/vlen=6}
  18. 包行有5的行
  19. keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6}
  20. keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6}
  21. keyvalues={row025/data:col5/1364133382268/Put/vlen=7, row025/url:col5/1364133382268/Put/vlen=6}
  22. keyvalues={row035/data:col5/1364133382268/Put/vlen=7, row035/url:col5/1364133382268/Put/vlen=6}
  23. keyvalues={row045/data:col5/1364133382268/Put/vlen=7, row045/url:col5/1364133382268/Put/vlen=6}
  24. keyvalues={row050/data:col0/1364133382268/Put/vlen=7, row050/url:col0/1364133382268/Put/vlen=6}
  25. 开头是row01的
  26. keyvalues={row010/data:col0/1364133382268/Put/vlen=7, row010/url:col0/1364133382268/Put/vlen=6}
  27. keyvalues={row011/data:col1/1364133382268/Put/vlen=7, row011/url:col1/1364133382268/Put/vlen=6}
  28. keyvalues={row012/data:col2/1364133382268/Put/vlen=7, row012/url:col2/1364133382268/Put/vlen=6}
  29. keyvalues={row013/data:col3/1364133382268/Put/vlen=7, row013/url:col3/1364133382268/Put/vlen=6}
  30. keyvalues={row014/data:col4/1364133382268/Put/vlen=7, row014/url:col4/1364133382268/Put/vlen=6}
  31. keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6}
  32. keyvalues={row016/data:col6/1364133382268/Put/vlen=7, row016/url:col6/1364133382268/Put/vlen=6}
  33. keyvalues={row017/data:col7/1364133382268/Put/vlen=7, row017/url:col7/1364133382268/Put/vlen=6}
  34. keyvalues={row018/data:col8/1364133382268/Put/vlen=7, row018/url:col8/1364133382268/Put/vlen=6}
  35. keyvalues={row019/data:col9/1364133382268/Put/vlen=7, row019/url:col9/1364133382268/Put/vlen=6}

 

 

http://blog.sina.com.cn/s/blog_68674da70102v6q4.html