189 8069 5689

hbase的过滤器查询

  hbase的过滤器有很多:大致分为两大类:比较过滤器和专用过滤器,过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;
hbase 过滤器的比较运算符

创新互联公司专业为企业提供牟平网站建设、牟平做网站、牟平网站设计、牟平网站制作等企业网站建设、网页设计与制作、牟平企业网站模板建站服务,十年牟平做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

 LESS  -----  <
 LESS_OR_EQUAL  -----  <=
 EQUAL  -----  =
 NOT_EQUAL -----   <>
 GREATER_OR_EQUAL  ----- >=
 GREATER  -----  > 
 NO_OP   #排除所有 

HBase 过滤器的比较器(指定比较机制)

BinaryComparator 按字节索引顺序比较指定字节数组,采用 Bytes.compareTo(byte[])
BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同
NullComparator 判断给定的是否为空
BitComparator 按位比较
RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非 EQUAL
SubstringComparator 判断提供的子串是否出现在 value 中。

1. 比较过滤器

//行键过滤器 RowFilter
Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("user0000")));
scan.setFilter(filter1);
//列簇过滤器 FamilyFilter
Filter filter1 = new FamilyFilter(CompareOp.LESS, new
BinaryComparator(Bytes.toBytes("base_info")));
scan.setFilter(filter1);
//列过滤器 QualifierFilter
Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("name")));
scan.setFilter(filter1);
//值过滤器 ValueFilter
Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("zhangsan") );
scan.setFilter(filter1); 
//时间戳过滤器 TimestampsFilter
List tss = new ArrayList();
tss.add(1495398833002l);
Filter filter1 = new TimestampsFilter(tss);
scan.setFilter(filter1);

2.专用过滤器

//单列值过滤器 SingleColumnValueFilter ----会返回满足条件的整行
SingleColumnValueFilter filter = new SingleColumnValueFilter(
 Bytes.toBytes("colfam1"),
 Bytes.toBytes("col-5"),
 CompareFilter.CompareOp.NOT_EQUAL,
 new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1); 
//单列值排除器 SingleColumnValueExcludeFilter -----返回排除了该列的结果
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(
 Bytes.toBytes("colfam1"),
 Bytes.toBytes("col-5"),
 CompareFilter.CompareOp.NOT_EQUAL,
 new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1);
//前缀过滤器 PrefixFilter----针对行键
Filter filter = new PrefixFilter(Bytes.toBytes("row1"));
scan.setFilter(filter1); 
//列前缀过滤器 ColumnPrefixFilter
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2"));
scan.setFilter(filter1); 

实战案例

public class HBase_Filter01 {
    private static String ZK_KEY = "hbase.zookeeper.quorum";
    private static String ZK_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
    private static Configuration conf;
    private static Connection connection;
    private static Admin admin;
    static {
        conf = HBaseConfiguration.create();
        conf.set(ZK_KEY,ZK_VALUE);
        try {
            connection= ConnectionFactory.createConnection(conf);
            admin=connection.getAdmin();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Scan scan=new Scan();
        ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));
        scan.setFilter(filter);
        TableName tableName =TableName.valueOf("user_info");
        try {
            Table table = connection.getTable(tableName);
            ResultScanner scanner = table.getScanner(scan);
            Iterator iterator=scanner.iterator();
            while(iterator.hasNext()){
                 Result result = iterator.next();
                System.out.println(result.list());
                 byte[] row = result.getRow();
                System.out.println(new String(row));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

本文标题:hbase的过滤器查询
转载源于:http://cdxtjz.com/article/psghgd.html

其他资讯