hbase行键过滤器RowFilter

RowFilter是用来对rowkey进行过滤的,比较符如下:

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 AND, OR, and XOR operators.

RegexStringComparator

正则表达式

SubstringComparator

把数据当成字符串,用contains()来判断





下面是相关比较符的例子

点击(此处)折叠或打开

  1. package com.hxl;

  2. import java.io.IOException;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.Cell;
  5. import org.apache.hadoop.hbase.CellUtil;
  6. import org.apache.hadoop.hbase.KeyValue;
  7. import org.apache.hadoop.hbase.client.HTable;
  8. import org.apache.hadoop.hbase.client.Result;
  9. import org.apache.hadoop.hbase.client.ResultScanner;
  10. import org.apache.hadoop.hbase.client.Scan;
  11. import org.apache.hadoop.hbase.filter.BinaryComparator;
  12. import org.apache.hadoop.hbase.filter.CompareFilter;
  13. import org.apache.hadoop.hbase.filter.Filter;
  14. import org.apache.hadoop.hbase.filter.RowFilter;
  15. import org.apache.hadoop.hbase.util.Bytes;

  16. import com.hxl.ConnectDB;

  17. public class RowFilterTest {

  18.     public void rowfilter(String tablename, String rowkeyvalue) {
  19.         Configuration configuration = null;
  20.         configuration = new ConnectDB().getConf();

  21.         try {
  22.             HTable table = new HTable(configuration, tablename);

  23.             Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL,
  24.                     new BinaryComparator(Bytes.toBytes(rowkeyvalue)));
  25.             // EQUAL =

  26.             Scan s = new Scan();
  27.             s.setFilter(filter1);
  28.             ResultScanner rs = table.getScanner(s);

  29.             for (Result r : rs) {
  30.                 System.out.println("rowkey:" + new String(r.getRow()));
  31.                 for (KeyValue keyValue : r.raw()) {
  32.                     System.out.println("列族:" + new String(keyValue.getFamily())
  33.                             + " 列:" + new String(keyValue.getQualifier()) + ":"
  34.                             + new String(keyValue.getValue()));
  35.                 }
  36.             }

  37.         } catch (IOException e) {
  38.             // TODO Auto-generated catch block
  39.             e.printStackTrace();

  40.         }

  41.     }

  42. }

相关的过滤方法使用:
提取rowkey以01结尾数据
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*01$"));

提取rowkey以包含201407的数据

Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("201407"));


提取rowkey以123开头的数据
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator("123".getBytes()));


-- The End --
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值