Hbase DependentColumnFilter

php中文网
发布: 2016-06-07 16:29:53
原创
1069人浏览过

Here you have a more complex filter that does not simply filter out data based on directly available information. Rather, it lets you specify a dependent column—or reference column—that controls how other columns are filtered. It uses th

Here you have a more complex filter that does not simply filter out data based on directly available information. Rather, it lets you specify a dependent column—or reference column—that controls how other columns are filtered. It uses the timestamp of the reference column and includes all other columns that have the same timestamp.

尝试找到该列所在的每一行,并返回该行具有相同时间戳的全部键值对。如果某一行不包含指定的列,则该行的任何键值对都不返回。
如果dropDependentColumn=true,则从属列不返回。

via: http://abloz.com/2012/08/22/the-hbases-content-query-2.html

package com.fatkun.filter.comparison;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.DependentColumnFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.filter.WritableByteArrayComparable;
import org.apache.hadoop.hbase.util.Bytes;
public class TestHbaseDependentColumnFilter {
    String tableName = "test_value_filter";
    Configuration config = HBaseConfiguration.create();
    public void filter(boolean drop, CompareFilter.CompareOp operator,
            WritableByteArrayComparable comparator) throws IOException {
        HTable table = new HTable(config, tableName);
        //
        Filter filter;
        if (comparator != null) {
            // drop为true时,filter表示对"col1"列以外的所有"data1"列族数据做filter操作
            // drop为false时,表示对所有"data1"列族的数据做filter操作
            filter = new DependentColumnFilter(Bytes.toBytes("data1"),
                    Bytes.toBytes("col1"), drop, operator, comparator);
        } else {
            filter = new DependentColumnFilter(Bytes.toBytes("data1"),
                    Bytes.toBytes("col1"), drop);
        }
        // filter应用于scan
        Scan scan = new Scan();
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            for (KeyValue kv : result.list()) {
                System.out.println("kv=" + kv.toString() + ",value="
                        + Bytes.toString(kv.getValue()));
            }
        }
        scanner.close();
        table.close();
    }
    /**
     * 部分代码来自hbase权威指南
     * 
     * @throws IOException
     */
    public void testFilter() throws IOException {
        // The dropDependentColumn parameter is giving you additional control
        // over how the reference column is handled: it is either included or
        // dropped by the filter
        // 1.获取整个"data1"列族当前Version中的所有timestamp等于参照列"data1:col1"的数据
        System.out.println("drop=false");
        filter(false, CompareFilter.CompareOp.NO_OP, null);
        // 2.获取除了"col1"列以外的"data1"列族中的所有timestamp等于参照列"data1:col1"的数据
        System.out.println("drop=true");
        filter(true, CompareFilter.CompareOp.NO_OP, null);
        // 3.获取除了"col1"列以外的"data1"列族当前Version中的所有timestamp等于参照列"data1:col1"的,value以"data100"开头的所有数据
        System.out.println("比较");
        filter(true, CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(
                Bytes.toBytes("data100")));
    }
    /**
     * 初始化数据
     */
    public void init() {
        // 创建表和初始化数据
        try {
            HBaseAdmin admin = new HBaseAdmin(config);
            if (!admin.tableExists(tableName)) {
                HTableDescriptor htd = new HTableDescriptor(tableName);
                HColumnDescriptor hcd1 = new HColumnDescriptor("data1");
                htd.addFamily(hcd1);
                HColumnDescriptor hcd2 = new HColumnDescriptor("data2");
                htd.addFamily(hcd2);
                HColumnDescriptor hcd3 = new HColumnDescriptor("data3");
                htd.addFamily(hcd3);
                admin.createTable(htd);
            }
            HTable table = new HTable(config, tableName);
            table.setAutoFlush(false);
            int count = 50;
            for &#40;int i = 1; i <= count; ++i&#41; &#123;
                Put p = new Put&#40;String.format&#40;"row%03d", i&#41;.getBytes&#40;&#41;&#41;;
                p.add&#40;"data1".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41;
                        .getBytes&#40;&#41;, String.format&#40;"data1%03d", i&#41;.getBytes&#40;&#41;&#41;;
                p.add&#40;"data2".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41;
                        .getBytes&#40;&#41;, String.format&#40;"data2%03d", i&#41;.getBytes&#40;&#41;&#41;;
                p.add&#40;"data3".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41;
                        .getBytes&#40;&#41;, String.format&#40;"data3%03d", i&#41;.getBytes&#40;&#41;&#41;;
                table.put&#40;p&#41;;
            &#125;
            table.close&#40;&#41;;
        &#125; catch &#40;IOException e&#41; &#123;
            e.printStackTrace&#40;&#41;;
        &#125;
    &#125;
    /**
     * @param args
     * @throws IOException
     */
    public static void main&#40;String&#91;&#93; args&#41; throws IOException &#123;
        TestHbaseDependentColumnFilter test = new TestHbaseDependentColumnFilter&#40;&#41;;
        test.init&#40;&#41;;
        test.testFilter&#40;&#41;;
    &#125;
&#125;
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号