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 *