HBase过滤器[RowFilter]之比较过滤器

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

添加数据

 /** 
    * @Description: 向Hbase表中插入单条数据 
    * @Param:  tableName 表名
    * @Param:  rowKey 唯一标识
    * @Param:  cfName 列族名
    * @Param:  qualifier 列标识
    * @Param:  data 数据
    * @return:  是否插入成功
    * @Author: zhoucan
    * @Date: 2018/8/2 
    */  
    public static boolean putRow(String tableName,String rowKey,String cfName,String qualifier,String data){


        try(Table table = HBaseConn.getTable(tableName)){

            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier),Bytes.toBytes(data));
            table.put(put);

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




        return true;
    }


    @Test
    public void addData(){

        HBaseUtil.putRow("FileTable","rowkey0","fileInfo","name","file0.txt");
        HBaseUtil.putRow("FileTable","rowkey0","fileInfo","type","txt");
        HBaseUtil.putRow("FileTable","rowkey0","fileInfo","size","1014");
        HBaseUtil.putRow("FileTable","rowkey0","saveInfo","creator","zhoucan");

        HBaseUtil.putRow("FileTable","rowkey1","fileInfo","name","file1.txt");
        HBaseUtil.putRow("FileTable","rowkey1","fileInfo","type","txt");
        HBaseUtil.putRow("FileTable","rowkey1","fileInfo","size","1024");
        HBaseUtil.putRow("FileTable","rowkey1","saveInfo","creator","zhoucan");

        HBaseUtil.putRow("FileTable","rowkey2","fileInfo","name","file2.txt");
        HBaseUtil.putRow("FileTable","rowkey2","fileInfo","type","txt");
        HBaseUtil.putRow("FileTable","rowkey2","fileInfo","size","1025");
        HBaseUtil.putRow("FileTable","rowkey2","saveInfo","creator","zhoucan");

        HBaseUtil.putRow("FileTable","rowkey3","fileInfo","name","a.jpg");
        HBaseUtil.putRow("FileTable","rowkey3","fileInfo","type","jpg");
        HBaseUtil.putRow("FileTable","rowkey3","fileInfo","size","11025");
        HBaseUtil.putRow("FileTable","rowkey3","saveInfo","creator","wuchao");

        HBaseUtil.putRow("FileTable","rowkey4","fileInfo","name","file4.txt");
        HBaseUtil.putRow("FileTable","rowkey4","fileInfo","type","txt");
        HBaseUtil.putRow("FileTable","rowkey4","fileInfo","size","10251");
        HBaseUtil.putRow("FileTable","rowkey4","saveInfo","creator","fangshuo");

        HBaseUtil.putRow("FileTable","rowkey5","fileInfo","name","file5.png");
        HBaseUtil.putRow("FileTable","rowkey5","fileInfo","type","png");
        HBaseUtil.putRow("FileTable","rowkey5","fileInfo","size","10225");
        HBaseUtil.putRow("FileTable","rowkey5","saveInfo","creator","wuchao");

}

1.RowFilter

//过滤器练习
     //过滤器练习
    @Test
    public void rowFilterTest(){


        String[] qualifiers = new String[]{"name","size"};

        rowFilter("FileTable","fileInfo",qualifiers,"rowkey3",CompareOperator.LESS_OR_EQUAL);

    }




    //基于行键过滤器
    public void rowFilter(String tableName,String cfName,String[] qualifiers,String rowkey,CompareOperator operator){


        try(Table table = HBaseConn.getTable(tableName)){

            Scan scan = new Scan();


            for(String name:qualifiers){

                scan.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(name));
            }


          //行过滤器
            Filter filter = new RowFilter(operator,new BinaryComparator(Bytes.toBytes(rowkey)));

            scan.setCaching(1000);

            scan.setFilter(filter);

            ResultScanner scanner = table.getScanner(scan);


            scanner.forEach(result -> {

                if (result != null){
                    System.out.println("rowkey="+Bytes.toString(result.getRow()));
                    System.out.println("fileName="+Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"),Bytes.toBytes("name"))));
                    System.out.println("szie="+Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"),Bytes.toBytes("size"))));
                }

            });




            scanner.close();



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



    }

结果:

rowkey=rowkey0
fileName=file0.txt
szie=1014
rowkey=rowkey1
fileName=file1.txt
szie=1024
rowkey=rowkey2
fileName=file2.txt
szie=1025
rowkey=rowkey3
fileName=a.jpg
szie=11025

总结:

1.
//Get the column from the specified family with the specified qualifier.
public Scan addColumn(byte [] family, byte [] qualifier)

上面返回特定列族特定列,注释掉下面,则返回rowkey对应的所有数据

for(String name:qualifiers){                
  scan.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(name));
 }

2. /**
   * 参数详情介绍下图所示
   * @param op the compare op for row matching
   * @param rowComparator the comparator for row matching
   */
  public RowFilter(final CompareOperator op,
                   final ByteArrayComparable rowComparator) {
    super(op, rowComparator);
  }



//比较运算符
public enum CompareOperator {
  // Keeps same names as the enums over in filter's CompareOp intentionally.
  // The convertion of operator to protobuf representation is via a name comparison.
  /** less than */
  LESS,
  /** less than or equal to */
  LESS_OR_EQUAL,
  /** equals */
  EQUAL,
  /** not equal */
  NOT_EQUAL,
  /** greater than or equal to */
  GREATER_OR_EQUAL,
  /** greater than */
  GREATER,
  /** no operation */
  NO_OP,
}
HBase过滤器[RowFilter]之比较过滤器_第1张图片
过滤器比较运算符和比较器

你可能感兴趣的:(HBase过滤器[RowFilter]之比较过滤器)