文档首页 > > 开发指南(适用于2.x及之前)> HBase应用开发> 开发程序> 使用过滤器Filter

使用过滤器Filter

分享
更新时间:2020/01/11 GMT+08:00

功能简介

HBase Filter主要在Scan和Get过程中进行数据过滤,通过设置一些过滤条件来实现,如设置RowKey、列名或者列值的过滤条件。

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testFilterList方法中

public void testFilterList() {
    LOG.info("Entering testFilterList.");

    Table table = null;

    // Instantiate a ResultScanner object.
    ResultScanner rScanner = null;

    try {
        // Create the Configuration instance.
        table = conn.getTable(tableName);

        // Instantiate a Get object.
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));

        // Instantiate a FilterList object in which filters have "and"
        // relationship with each other.
        FilterList list = new FilterList(Operator.MUST_PASS_ALL);
        // Obtain data with age of greater than or equal to 20. 
        //MRS 2.0.0及其之后版本使用CompareOperator替换CompareOp
        list.addFilter(new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"),
                CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(new Long(20))));
        // Obtain data with age of less than or equal to 29.
        //MRS 2.0.0及其之后版本使用CompareOperator替换CompareOp
        list.addFilter(new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"),
                CompareOp.LESS_OR_EQUAL, Bytes.toBytes(new Long(29))));

        scan.setFilter(list);

        // Submit a scan request.
        rScanner = table.getScanner(scan);
        // Print query results.
        for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
            for (Cell cell : r.rawCells()) {
                LOG.info(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                        + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                        + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                        + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        LOG.info("Filter list successfully.");
    } catch (IOException e) {
        LOG.error("Filter list failed ", e);
    } finally {
        if (rScanner != null) {
            // Close the scanner object.
            rScanner.close();
        }
        if (table != null) {
            try {
                // Close the HTable object.
                table.close();
            } catch (IOException e) {
                LOG.error("Close table failed ", e);
            }
        }
    }
    LOG.info("Exiting testFilterList.");
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问