溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

hbase的過濾器查詢

發(fā)布時間:2020-06-12 23:15:10 來源:網(wǎng)絡(luò) 閱讀:804 作者:原生zzy 欄目:大數(shù)據(jù)

  hbase的過濾器有很多:大致分為兩大類:比較過濾器和專用過濾器,過濾器的作用是在服務(wù)端判斷數(shù)據(jù)是否滿足條件,然后只將滿足條件的數(shù)據(jù)返回給客戶端;
hbase 過濾器的比較運算符

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

HBase 過濾器的比較器(指定比較機制)

BinaryComparator 按字節(jié)索引順序比較指定字節(jié)數(shù)組,采用 Bytes.compareTo(byte[])
BinaryPrefixComparator 跟前面相同,只是比較左端的數(shù)據(jù)是否相同
NullComparator 判斷給定的是否為空
BitComparator 按位比較
RegexStringComparator 提供一個正則的比較器,僅支持 EQUAL 和非 EQUAL
SubstringComparator 判斷提供的子串是否出現(xiàn)在 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<Long> tss = new ArrayList<Long>();
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); //如果不設(shè)置為 true,則那些不包含指定 column 的行也會返回
scan.setFilter(filter1); 
//單列值排除器 SingleColumnValueExcludeFilter -----返回排除了該列的結(jié)果
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(
 Bytes.toBytes("colfam1"),
 Bytes.toBytes("col-5"),
 CompareFilter.CompareOp.NOT_EQUAL,
 new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不設(shè)置為 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); 

實戰(zhàn)案例

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<Result> 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();
        }
    }
}
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI