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

Hbase使用过滤器的查询封装(idea),第1张

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 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 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);
      }

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存