Hbase自定义过滤器

本文介绍了如何在Hbase中创建自定义过滤器,包括下载protobuf,配置环境变量,创建.proto文件,生成序列化类,编写自定义过滤器类,实现compareTo方法进行过滤逻辑,并将项目打包成jar文件添加到Hbase的lib目录中,最后展示了使用自定义过滤器查询结果的代码片段。
摘要由CSDN通过智能技术生成

1. 下载protobuf-2.5.0解压,如果是window下,额外下载protoc-2.5.0-win32,解压,将protoc.exe放在protobuf-2.5.0下的src目录下


2. 配置环境变量,添加path路径指向protobuf目录的src中


3. 查看当前版本,在命令提示符中输入命令


4. 创建一个空白的文本文件 命令为 CustomNumberComparator.proto  即 后缀文件类型为proto



5. 用记事本打开CustomNumberComparator.proto文件输入以下内容


 
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// This file contains protocol buffers that are used for filters

option java_package = "com.pateo.hbase.defined.comparator";//生成java代码的包名
option java_outer_classname = "MyComparatorProtos";//生成的类名
option java_generic_services = true;
option java_generate_equals_and_hash = true;
option optimize_for = SPEED;

// This file contains protocol buffers that are used for comparators (e.g. in filters)

message CustomNumberComparator {
    required bytes value = 1;     //自定义比较器中需序列化的字段
    required string fieldType = 2;//自定义比较器中需序列化的字段
} 


6. 进入命令提示符,使用命令读取CustomNumberComparator.proto的内容生成java代码,即自定义比较器的序列化类

  内容:   protoc.exe -I=C:/proto --java_out=C:/proto C:/proto/CustomNumberComparator.proto



输入后会在指定的/protoc中生成一个文件夹


得到自定义比较器的序列化类


7. 将生成的文件夹拷贝到idea编程工具中,注意粘贴的路径为java下


8. 新建一个自定义过滤器类CustomNumberComparator


9. CustomNumberComparator继承ByteArrayComparable类,重写方法,代码如下

package com.pateo.hbase.defined.comparator;

import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.util.Bytes;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.Locale;

/**
 * 自定义比较器:使用方法见 CompareTest
 *
 * @param : fieldType 传递数据格式的类型,支持的数据类型:double
 * @param : data 通过Bytes转换得到的字节数组 使用注意事项 : 使用的时候要注意数据类型的匹配问题
 */
public class CustomNumberComparator extends ByteArrayComparable {
    /**
     * 目前只支持 double类型
     */
    private String fieldType;
    private byte[] data;

    /**
     * Constructor
     *
     * @param value
     * 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值