Hbase使用过滤器的查询封装(idea)

在hbase上使用过滤器查询可参考:

(73条消息) 在Hbase使用过滤器(行键过滤器、列族与列过滤器、值过滤器)_小镭敲代码的博客-CSDN博客

目录

 1、 查询 值里面有的数据

2、 查询值里面包含a的数据

 3、查询在info1的列族中name的值为python的数据

4、查询在info1的列族中name的值为python的数据

5、查询以n开头的列,并且值为python的数据(过滤器不止一个)


    1、 查询 值里面有的数据

        // 查询 值里面有的数据
    public static void ScanValueFilter_1(String str) throws IOException {
           // 获取表
           Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
           // 查询valueFilter
           Scan scan = new Scan();
           // 创建值过滤器
           ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(str)));
           // 为Scan 对象 设置过滤器
           scan.setFilter(valueFilter);
           // 在表上进行查询
           ResultScanner scanner =  table.getScanner(scan);
           for(Result result:scanner){
               for(Cell cell: result.rawCells()){
                   System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
               }
           }
       }

2、 查询值里面包含a的数据

 // 查询值里面包含a的数据
      public static void ScanValueFilter_2(String bookname) throws IOException {
          // 获取表
          Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
          // 查询valueFilter
          Scan scan = new Scan();
          // 创建值过滤器
          ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(bookname));
          // 为Scan 对象 设置过滤器
          scan.setFilter(valueFilter);
          // 在表上进行查询
          ResultScanner scanner =  table.getScanner(scan);
          for(Result result:scanner){
              for(Cell cell: result.rawCells()){
                  System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
              }
          }
      }

    3、查询在info1的列族中name的值为python的数据

    //查询在info1的列族中name的值为python的数据
    public static void ScanSingleColumnValueFilter_1(String family,String columnName,String value) throws IOException {
        Scan scan = new Scan();
        SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes(family), Bytes.toBytes(columnName), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(value));
        scan.setFilter(singleColumnValueExcludeFilter);
        output(scan);
    }

4、查询在info1的列族中name的值为python的数据

public static void ScanColumnAndValueFilter_1(String columnName,String value) throws IOException{
        Scan scan = new Scan();
        //多个过滤器
        ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes(columnName));
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(value));
        //用一个集合装上多个过滤器
        ArrayList<Filter> filters = new ArrayList<>();
        filters.add(columnPrefixFilter);
        filters.add(valueFilter);
        //And
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);        //FilterList.Operator.MUST_PASS_ALL满足所有过滤器,FilterList.Operator.MUST_PASS_ONE满足一个就可以
        //为scan设置过滤器
        scan.setFilter(filterList);
        //用scan扫描数据
        output(scan);
    }

5、查询以n开头的列,并且值为python的数据(过滤器不止一个)

    //查询以n开头的列,并且值为python的数据(过滤器不止一个)
    public static void ScanColumnAndValueFilter_1(String columnName,String value) throws IOException{
        Scan scan = new Scan();
        //多个过滤器
        ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes(columnName));
        ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(value));
        //用一个集合装上多个过滤器
        ArrayList<Filter> filters = new ArrayList<>();
        filters.add(columnPrefixFilter);
        filters.add(valueFilter);
        //And
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);        //FilterList.Operator.MUST_PASS_ALL满足所有过滤器,FilterList.Operator.MUST_PASS_ONE满足一个就可以
        //为scan设置过滤器
        scan.setFilter(filterList);
        //用scan扫描数据
        output(scan);
    }

6、封装输出方法

public  static  void output(Scan scan) throws IOException {
        Table table = HbaseHelper.GetConnection().getTable(TableName.valueOf("booksystem:bookinfo"));
        ResultScanner scanner =  table.getScanner(scan);
        for(Result result:scanner){
            for(Cell cell: result.rawCells()){
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+","+Bytes.toString(CellUtil.cloneFamily(cell))+","+Bytes.toString(CellUtil.cloneQualifier(cell))+":"+ Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

7、主函数

public static void main(String[] args) throws IOException{
        System.out.println("请输入书名");
        Scanner scanner = new Scanner(System.in);
        String bookname = scanner.next();
        Demo_1.ScanValueFilter_1(bookname);
        //Demo_1.ScanSingleColumnValueFilter_1("info1","name",bookname);
        //Demo_1.ScanColumnAndValueFilter_1("n",bookname);
      }

 

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值