Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1758411
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: Java

2013-04-27 10:25:24

Hbase的api中可以通过filter来实现like查询,详情如下:

对行key进行like查询:

  1. private void test() throws Exception {
  2.         Configuration conf = getCfg();
  3.         Scan scan = new Scan();
  4.         RegexStringComparator comp = new RegexStringComparator("(##test)");
  5.         RowFilter filter = new RowFilter(CompareOp.EQUAL, comp);
  6.         scan.setFilter(filter);
  7.         scan.setCaching(200);
  8.         scan.setCacheBlocks(false);
  9.         HTable hTable = new HTable(conf, "Test");
  10.         ResultScanner scanner = hTable.getScanner(scan);
  11.         byte[] bytes = Bytes.toBytes("T");
  12.         for (Result result : scanner) {
  13.             String all = Bytes.toString(result.getValue(bytes, bytes));
  14.             System.out.println(all);
  15.         }
  16.     }

 主要借助于RegexStringComparator

对列值进行like查询:

  1. private void test() throws Exception {
  2.         Configuration conf = getCfg();
  3.         Scan scan = new Scan();
  4.         RegexStringComparator comp = new RegexStringComparator("(##test)");
  5.         byte[] bytes = Bytes.toBytes("T");
  6.         Filter filter = new SingleColumnValueFilter(bytes, bytes, CompareOp.EQUAL, comp);
  7.         scan.setFilter(filter);
  8.         scan.setCaching(200);
  9.         scan.setCacheBlocks(false);
  10.         HTable hTable = new HTable(conf, "Test");
  11.         ResultScanner scanner = hTable.getScanner(scan);
  12.         for (Result result : scanner) {
  13.             String all = Bytes.toString(result.getValue(bytes , bytes ));
  14.             System.out.println(all);
  15.         }
  16.     }

仍然是借助于RegexStringComparator

转自:http://smallnetvisitor.iteye.com/blog/1725765

阅读(3633) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~