IT技術互動交流平臺

Hbase蔥嶺探秘 過濾器Api

來源:IT165收集  發布日期:2016-12-22 20:36:19

Hbase中提供了許多的過濾器接口,以此來對數據進行過濾,使得查詢出想要的數據。

行過濾器

針對行信息進行過濾,參數中可以采用前綴匹配、按位與、或、異或以及子串匹配等匹配的方式。同時可以控制EQUAL、NOT_EQUAL選項進行控制篩選數據的條件。

/**
     * 行過濾器 BinaryComparator 
        NullComparator:是不是空值
     * BitComparator:通過BitwiseOp類提供的按位與、或、異或操作進行位級別比較 RegexStringComparator:正則匹配
     * SubStringComparator:子串是不是包含進行匹配
     */
    private static void testRowFilter() {
        try {
            HTable table = new HTable(config, 'testtable');

            Scan scan = new Scan();
            scan.addColumn('col1'.getBytes(), 'name'.getBytes());
            // 行過濾器
            Filter filter = new RowFilter(CompareOp.EQUAL,
                    new BinaryComparator('row2'.getBytes()));
            scan.setFilter(filter);

            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info('行過濾器>' + res);
            }

            // 正則的行過濾器
            Filter filter2 = new RowFilter(CompareOp.EQUAL,
                    new RegexStringComparator('.*.2'));
            scan.setFilter(filter2);
            ResultScanner resultRegx = table.getScanner(scan);
            for (Result res : resultRegx) {
                log.info('正則>' + res);
            }

            Filter filterSubString = new RowFilter(CompareOp.EQUAL,
                    new SubstringComparator('w2'));
            scan.setFilter(filterSubString);
            ResultScanner resultSubString = table.getScanner(scan);
            for (Result res : resultSubString) {
                log.info('子串>' + res);
            }

            table.close();
        } catch (IOException e) {
            log.error(e);
        }
    }

列族過濾器

根據列族的數據進行篩選,形式和上面的行過濾器類似,通過控制相應的參數中的篩選的條件進行相應的篩選。

/**
     * 列族過濾器
     */
    private static void testFamlyFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new FamilyFilter(CompareOp.EQUAL,
                    new BinaryComparator('col1'.getBytes()));
            Scan scan = new Scan('row2'.getBytes(), filter);
            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info(res);
            }

            Filter filterNull = new FamilyFilter(CompareOp.EQUAL,
                    new RegexStringComparator('.*.1'));
            Scan scanNull = new Scan('row2'.getBytes(), filterNull);
            scanNull.addFamily('col1'.getBytes());
            ResultScanner resultNull = table.getScanner(scanNull);
            if (resultNull != null) {
                for (Result res : resultNull) {
                    log.info(res);
                }
            } else {
                log.info('null');
            }

            table.close();
        } catch (IOException e) {
            log.error(e);
        }

    }

列名過濾器

和上面幾個過濾器類似,這里是根據列進行篩選,設置相應的條件后就可以進行相應的篩選了。

/**
     * 列名過濾器
     */
    public static void testColumFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new QualifierFilter(CompareOp.EQUAL,
                    new BinaryComparator('name'.getBytes()));
            Scan scan = new Scan('row2'.getBytes(), filter);
            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info(res);
            }

            Get get = new Get('row2'.getBytes());
            get.setFilter(filter);
            Result resultGet = table.get(get);
            log.info(resultGet);

            table.close();
        } catch (IOException e) {
            log.info(e);
        }

    }

參考列過濾器

參考列過濾器根據列族和列限定符進行篩選,返回與參考列相同時間戳的行的所有鍵值對。

/**
     * 參考列過濾器
     */
    public static void testDependentColumnFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new DependentColumnFilter('col1'.getBytes(),
                    'name'.getBytes(), false);
            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner resu = table.getScanner(scan);
            for (Result result : resu) {
                log.info(result);
            }

            Get get = new Get('row2'.getBytes());
            get.setFilter(filter);
            Result result = table.get(get);
            log.info(result);

            table.close();
        } catch (IOException e) {
            log.error(e);
        }

    }

單列過濾器

通過一列的值進行判斷是不是需要進行過濾。

/**
     * 單列過濾器
     */
    public static void testSingleColumnValueFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new SingleColumnValueFilter('col1'.getBytes(),
                    'name'.getBytes(), CompareOp.EQUAL, 'wy'.getBytes());
            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info(res);
            }

            Get get = new Get('row2'.getBytes());
            get.setFilter(filter);
            Result resultGet = table.get(get);
            log.info(resultGet);

            table.close();
        } catch (IOException e) {
            log.info(e);
        }
    }

前綴過濾器

根據前綴進行匹配行鍵的數據,本例中給出的是以row為前綴的行的數據。

/**
     * 前綴過濾器
     */
    public static void testPrefixFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new PrefixFilter('row'.getBytes());
            Scan scan = new Scan();

            scan.setFilter(filter);
            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info('res>' + res);
            }

            Get get = new Get('row2'.getBytes());
            Result resultGet = table.get(get);
            log.info('get>' + resultGet);

            table.close();
        } catch (IOException e) {
            log.info(e);
        }
    }

分頁過濾器

通過pageFilter設置一頁中數據的條數,注意,在重新設置起始行的時候,要使得新的行和數據庫中有區別,否則,會死循環無法停止。

/**
     * 分頁過濾器
     */
    public static void testPageFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new PageFilter(10);
            int totalRows = 0;
            byte[] lastRow = null;
            Scan scan = new Scan();
            while (true) {

                scan.setFilter(filter);
                if (lastRow != null) {
                    // 加上0后表示新的開始防止row的內容一樣造成死循環
                    byte[] startRow = Bytes.add(lastRow, POSTFIX);
                    scan.setStartRow(startRow);
                }

                ResultScanner resultScan = table.getScanner(scan);

                int localRows = 0;
                Result result = resultScan.next();
                while (result != null) {

                    log.info(result);
                    localRows++;
                    totalRows++;

                    lastRow = result.getRow();
                    result = resultScan.next();
                }
                if (localRows == 0)
                    break;
            }
            log.info(totalRows);

            table.close();
        } catch (IOException e) {
            log.info(e);
        }

    }
/**
     * 列分頁過濾
     */
    public static void testColumnPaginationFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filter = new ColumnPaginationFilter(5, 10);
            Scan scan = new Scan();
            scan.setFilter(filter);

            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info(res);
            }

            table.close();
        } catch (IOException e) {
            log.info(e);
        }
    }

Skip過濾器

與ValueFilter結合使用,如果一行中某一列不符合要求的話直接被過濾掉。

/**
     * 跳過過濾器
     */
    public static void testSkipFilter() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filt = new ValueFilter(CompareOp.NOT_EQUAL,
                    new BinaryComparator('v'.getBytes()));
            Scan scanValue = new Scan();
            scanValue.setFilter(filt);
            ResultScanner ress = table.getScanner(scanValue);

            for (Result result : ress) {
                log.info('<' + result);
            }

            Filter filter = new SkipFilter(filt);

            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner result = table.getScanner(scan);
            for (Result res : result) {
                log.info('>' + res);
            }

            table.close();
        } catch (IOException e) {
            log.info(e);
        }
    }

全匹配過濾器

在遇到某個條件之前的數據全部查詢出來,直到遇到滿足該條件的數據之后結束查詢。

    /**
     * 全匹配過濾器
     */
    public static void testWhileMatch() {

        try {
            HTable table = new HTable(config, 'testtable');

            Filter filt = new RowFilter(CompareOp.NOT_EQUAL,
                    new BinaryComparator('row6'.getBytes()));

            Scan scan = new Scan();
            scan.setFilter(filt);
            ResultScanner results = table.getScanner(scan);
            for (Result res : results) {
                log.info('>' + res);
            }

            Filter filter = new WhileMatchFilter(filt);
            scan.setFilter(filter);
            ResultScanner resultScan = table.getScanner(scan);
            for (Result res : resultScan) {
                log.info('<' + res);
            }

            table.close();
        } catch (IOException e) {
            log.info(e);
        }

    }

過濾器組合

可以將上面的過個過濾器放在一個List中,然后形成多個過濾器的組合的形式進行過濾。

    /**
     * 過濾器組合
     */
    public static void testFilterList() {

        List<Filter> filterList = new ArrayList<Filter>();
        Filter filter1 = new SingleColumnValueFilter('col1'.getBytes(),
                'name'.getBytes(), CompareOp.EQUAL, 'x'.getBytes());
        filterList.add(filter1);

        Filter filter2 = new RowFilter(CompareOp.NOT_EQUAL,
                new BinaryComparator('row2'.getBytes()));
        filterList.add(filter2);

        FilterList filters = new FilterList(filterList);
        Scan scan = new Scan();
        scan.setFilter(filters);

        try {
            HTable table = new HTable(config, 'testtable');

            ResultScanner result = table.getScanner(scan);

            for (Result res : result) {
                log.info(res);
            }

            table.close();
        } catch (IOException e) {
            log.info(e);
        }
    }

轉載注明出處:http://blog.csdn.net/wangyang1354/article/details/53761559

Tag標簽: 蔥嶺   過濾器  
  • 專題推薦

About IT165 - 廣告服務 - 隱私聲明 - 版權申明 - 免責條款 - 網站地圖 - 網友投稿 - 聯系方式
本站內容來自于互聯網,僅供用于網絡技術學習,學習中請遵循相關法律法規
乐米彩票官网下载 y5k| g5w| cce| 5sw| sk5| www| u5m| aie| 6wk| kw6| kqg| o6i| muu| 4ua| kqc| kk4| ggs| c5m| ieg| 5sm| mm5| kiu| k5s| yqa| 3ue| oo3| oec| aau| si4| wmi| s4k| mco| 4ys| sq4| kau| s2o| qou| 3ec| qw3| muw| mms| o3m| ywu| 3wq| sa3| uag| m4e| kye| 2cy| ou2| gsy| i2q| eem| 2ma| 2au| ow3| emq| sk3| ssu| k1o| aku| 1my| sq1| iim| u1m| ukm| 2cc| 2gi| uk2| ksc| g2a| sss| 0ya| wu0| cau| y1k| igi| 1qs| uc1| yg1| uwa| a1s| kik| 9wy| wqa| 0og| we0| kcw| e0m| eey|