hbase协处理器编码实例

Posted learn21cn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase协处理器编码实例相关的知识,希望对你有一定的参考价值。

Observer协处理器通常在一个特定的事件(诸如GetPut)之前或之后发生,相当于RDBMS中的触发器。Endpoint协处理器则类似于RDBMS中的存储过程,因为它可以让你在RegionServer上对数据执行自定义计算,而不是在客户端上执行计算。

本文是以上两者的简单实例,使用的环境:环境 jdk1.8 hadoop2.6.5 hbase1.2.4。

1、Endpoint实例 
1> 编写适用于protobuf的proto文件,如下,尽量不要带注释,因为编译时可能出现乱码

option java_package = "com.endpoint.test";
option java_outer_classname = "Sum";
option java_generic_services = true;
option java_generate_equals_and_hash = true;
option optimize_for = SPEED;

message SumRequest {
    required string family = 1;    
    required string column = 2;    
}
message SumResponse {
    required int64 sum = 1 [default = 0];
}
service SumService {
    rpc getSum(SumRequest)
        returns (SumResponse);
}

 2> 编译上面的proto文件
使用protoc程序进行编译,linux下或者windows均可,protoc程序可以直接从github下载:https://github.com/google/protobuf/releases,也可以自己编译生成,参见protobuf的编译安装

注意,编译的版本要与hadoop以及hbase使用的版本相同,或者略高,但最好不要过高,hadoop2.6.5 hbase1.2.4使用的都是protobuf2.5.0的版本,写此篇文章时的最新版为3.1.0

(高版本必须指定syntax,例如proto3的syntax在第一行非空白非注释行,必须写:syntax = "proto3",字段规则移除了 “required”,并把 “optional” 改名为 “singular”,移除了 default 选项。可搜索Protobuf 的 proto3 与 proto2 的区别进行了解。)下载的话选择带win或linux的版本,这是编译好的版本。有很多带具体语言的版本,是一些具体某种语言的发行版源码包。为了与hbase以及hadoop统一起来,此处用的是protoc-2.5.0-win32.zip。

解压文件:

 

 使用windows命令行进入上面的目录,执行以下命令即可:

protoc.exe sum1.proto --java_out=./

高版本有编译好的适用于linux下的protoc程序文件,低版本没有。在linux下执行以下命令:

protoc sum.proto --java_out=./

 结果都一样,生成的代码参见折叠部分,有很多,因为上面文件中指定java_outer_classname = "Sum",所以会生成Sum类,将这个类引入到项目中,注意项目的包名称与上面文件中指定(option java_package = "com.endpoint.test")的名称要一致。

   1 // Generated by the protocol buffer compiler.  DO NOT EDIT!
   2 // source: sumcode.proto
   3 
   4 package com.endpoint.test;
   5 
   6 public final class Sum {
   7   private Sum() {}
   8   public static void registerAllExtensions(
   9       com.google.protobuf.ExtensionRegistry registry) {
  10   }
  11   public interface SumRequestOrBuilder
  12       extends com.google.protobuf.MessageOrBuilder {
  13 
  14     // required string family = 1;
  15     /**
  16      * <code>required string family = 1;</code>
  17      */
  18     boolean hasFamily();
  19     /**
  20      * <code>required string family = 1;</code>
  21      */
  22     java.lang.String getFamily();
  23     /**
  24      * <code>required string family = 1;</code>
  25      */
  26     com.google.protobuf.ByteString
  27         getFamilyBytes();
  28 
  29     // required string column = 2;
  30     /**
  31      * <code>required string column = 2;</code>
  32      */
  33     boolean hasColumn();
  34     /**
  35      * <code>required string column = 2;</code>
  36      */
  37     java.lang.String getColumn();
  38     /**
  39      * <code>required string column = 2;</code>
  40      */
  41     com.google.protobuf.ByteString
  42         getColumnBytes();
  43   }
  44   /**
  45    * Protobuf type {@code SumRequest}
  46    */
  47   public static final class SumRequest extends
  48       com.google.protobuf.GeneratedMessage
  49       implements SumRequestOrBuilder {
  50     // Use SumRequest.newBuilder() to construct.
  51     private SumRequest(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
  52       super(builder);
  53       this.unknownFields = builder.getUnknownFields();
  54     }
  55     private SumRequest(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
  56 
  57     private static final SumRequest defaultInstance;
  58     public static SumRequest getDefaultInstance() {
  59       return defaultInstance;
  60     }
  61 
  62     public SumRequest getDefaultInstanceForType() {
  63       return defaultInstance;
  64     }
  65 
  66     private final com.google.protobuf.UnknownFieldSet unknownFields;
  67     @java.lang.Override
  68     public final com.google.protobuf.UnknownFieldSet
  69         getUnknownFields() {
  70       return this.unknownFields;
  71     }
  72     private SumRequest(
  73         com.google.protobuf.CodedInputStream input,
  74         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
  75         throws com.google.protobuf.InvalidProtocolBufferException {
  76       initFields();
  77       int mutable_bitField0_ = 0;
  78       com.google.protobuf.UnknownFieldSet.Builder unknownFields =
  79           com.google.protobuf.UnknownFieldSet.newBuilder();
  80       try {
  81         boolean done = false;
  82         while (!done) {
  83           int tag = input.readTag();
  84           switch (tag) {
  85             case 0:
  86               done = true;
  87               break;
  88             default: {
  89               if (!parseUnknownField(input, unknownFields,
  90                                      extensionRegistry, tag)) {
  91                 done = true;
  92               }
  93               break;
  94             }
  95             case 10: {
  96               bitField0_ |= 0x00000001;
  97               family_ = input.readBytes();
  98               break;
  99             }
 100             case 18: {
 101               bitField0_ |= 0x00000002;
 102               column_ = input.readBytes();
 103               break;
 104             }
 105           }
 106         }
 107       } catch (com.google.protobuf.InvalidProtocolBufferException e) {
 108         throw e.setUnfinishedMessage(this);
 109       } catch (java.io.IOException e) {
 110         throw new com.google.protobuf.InvalidProtocolBufferException(
 111             e.getMessage()).setUnfinishedMessage(this);
 112       } finally {
 113         this.unknownFields = unknownFields.build();
 114         makeExtensionsImmutable();
 115       }
 116     }
 117     public static final com.google.protobuf.Descriptors.Descriptor
 118         getDescriptor() {
 119       return com.endpoint.test.Sum.internal_static_SumRequest_descriptor;
 120     }
 121 
 122     protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
 123         internalGetFieldAccessorTable() {
 124       return com.endpoint.test.Sum.internal_static_SumRequest_fieldAccessorTable
 125           .ensureFieldAccessorsInitialized(
 126               com.endpoint.test.Sum.SumRequest.class, com.endpoint.test.Sum.SumRequest.Builder.class);
 127     }
 128 
 129     public static com.google.protobuf.Parser<SumRequest> PARSER =
 130         new com.google.protobuf.AbstractParser<SumRequest>() {
 131       public SumRequest parsePartialFrom(
 132           com.google.protobuf.CodedInputStream input,
 133           com.google.protobuf.ExtensionRegistryLite extensionRegistry)
 134           throws com.google.protobuf.InvalidProtocolBufferException {
 135         return new SumRequest(input, extensionRegistry);
 136       }
 137     };
 138 
 139     @java.lang.Override
 140     public com.google.protobuf.Parser<SumRequest> getParserForType() {
 141       return PARSER;
 142     }
 143 
 144     private int bitField0_;
 145     // required string family = 1;
 146     public static final int FAMILY_FIELD_NUMBER = 1;
 147     private java.lang.Object family_;
 148     /**
 149      * <code>required string family = 1;</code>
 150      */
 151     public boolean hasFamily() {
 152       return ((bitField0_ & 0x00000001) == 0x00000001);
 153     }
 154     /**
 155      * <code>required string family = 1;</code>
 156      */
 157     public java.lang.String getFamily() {
 158       java.lang.Object ref = family_;
 159       if (ref instanceof java.lang.String) {
 160         return (java.lang.String) ref;
 161       } else {
 162         com.google.protobuf.ByteString bs = 
 163             (com.google.protobuf.ByteString) ref;
 164         java.lang.String s = bs.toStringUtf8();
 165         if (bs.isValidUtf8()) {
 166           family_ = s;
 167         }
 168         return s;
 169       }
 170     }
 171     /**
 172      * <code>required string family = 1;</code>
 173      */
 174     public com.google.protobuf.ByteString
 175         getFamilyBytes() {
 176       java.lang.Object ref = family_;
 177       if (ref instanceof java.lang.String) {
 178         com.google.protobuf.ByteString b = 
 179             com.google.protobuf.ByteString.copyFromUtf8(
 180                 (java.lang.String) ref);
 181         family_ = b;
 182         return b;
 183       } else {
 184         return (com.google.protobuf.ByteString) ref;
 185       }
 186     }
 187 
 188     // required string column = 2;
 189     public static final int COLUMN_FIELD_NUMBER = 2;
 190     private java.lang.Object column_;
 191     /**
 192      * <code>required string column = 2;</code>
 193      */
 194     public boolean hasColumn() {
 195       return ((bitField0_ & 0x00000002) == 0x00000002);
 196     }
 197     /**
 198      * <code>required string column = 2;</code>
 199      */
 200     public java.lang.String getColumn() {
 201       java.lang.Object ref = column_;
 202       if (ref instanceof java.lang.String) {
 203         return (java.lang.String) ref;
 204       } else {
 205         com.google.protobuf.ByteString bs = 
 206             (com.google.protobuf.ByteString) ref;
 207         java.lang.String s = bs.toStringUtf8();
 208         if (bs.isValidUtf8()) {
 209           column_ = s;
 210         }
 211         return s;
 212       }
 213     }
 214     /**
 215      * <code>required string column = 2;</code>
 216      */
 217     public com.google.protobuf.ByteString
 218         getColumnBytes() {
 219       java.lang.Object ref = column_;
 220       if (ref instanceof java.lang.String) {
 221         com.google.protobuf.ByteString b = 
 222             com.google.protobuf.ByteString.copyFromUtf8(
 223                 (java.lang.String) ref);
 224         column_ = b;
 225         return b;
 226       } else {
 227         return (com.google.protobuf.ByteString) ref;
 228       }
 229     }
 230 
 231     private void initFields() {
 232       family_ = "";
 233       column_ = "";
 234     }
 235     private byte memoizedIsInitialized = -1;
 236     public final boolean isInitialized() {
 237       byte isInitialized = memoizedIsInitialized;
 238       if (isInitialized != -1) return isInitialized == 1;
 239 
 240       if (!hasFamily()) {
 241         memoizedIsInitialized = 0;
 242         return false;
 243       }
 244       if (!hasColumn()) {
 245         memoizedIsInitialized = 0;
 246         return false;
 247       }
 248       memoizedIsInitialized = 1;
 249       return true;
 250     }
 251 
 252     public void writeTo(com.google.protobuf.CodedOutputStream output)
 253                         throws java.io.IOException {
 254       getSerializedSize();
 255       if (((bitField0_ & 0x00000001) == 0x00000001)) {
 256         output.writeBytes(1, getFamilyBytes());
 257       }
 258       if (((bitField0_ & 0x00000002) == 0x00000002)) {
 259         output.writeBytes(2, getColumnBytes());
 260       }
 261       getUnknownFields().writeTo(output);
 262     }
 263 
 264     private int memoizedSerializedSize = -1;
 265     public int getSerializedSize() {
 266       int size = memoizedSerializedSize;
 267       if (size != -1) return size;
 268 
 269       size = 0;
 270       if (((bitField0_ & 0x00000001) == 0x00000001)) {
 271         size += com.google.protobuf.CodedOutputStream
 272           .computeBytesSize(1, getFamilyBytes());
 273       }
 274       if (((bitField0_ & 0x00000002) == 0x00000002)) {
 275         size += com.google.protobuf.CodedOutputStream
 276           .computeBytesSize(2, getColumnBytes());
 277       }
 278       size += getUnknownFields().getSerializedSize();
 279       memoizedSerializedSize = size;
 280       return size;
 281     }
 282 
 283     private static final long serialVersionUID = 0L;
 284     @java.lang.Override
 285     protected java.lang.Object writeReplace()
 286         throws java.io.ObjectStreamException {
 287       return super.writeReplace();
 288     }
 289 
 290     @java.lang.Override
 291     public boolean equals(final java.lang.Object obj) {
 292       if (obj == this) {
 293        return true;
 294       }
 295       if (!(obj instanceof com.endpoint.test.Sum.SumRequest)) {
 296         return super.equals(obj);
 297       }
 298       com.endpoint.test.Sum.SumRequest other = (com.endpoint.test.Sum.SumRequest) obj;
 299 
 300       boolean result = true;
 301       result = result && (hasFamily() == other.hasFamily());
 302       if (hasFamily()) {
 303         result = result && getFamily()
 304             .equals(other.getFamily());
 305       }
 306       result = result && (hasColumn() == other.hasColumn());
 307       if (hasColumn()) {
 308         result = result && getColumn()
 309             .equals(other.getColumn());
 310       }
 311       result = result &&
 312           getUnknownFields().equals(other.getUnknownFields());
 313       return result;
 314     }
 315 
 316     private int memoizedHashCode = 0;
 317     @java.lang.Override
 318     public int hashCode() {
 319       if (memoizedHashCode != 0) {
 320         return memoizedHashCode;
 321       }
 322       int hash = 41;
 323       hash = (19 * hash) + getDescriptorForType().hashCode();
 324       if (hasFamily()) {
 325         hash = (37 * hash) + FAMILY_FIELD_NUMBER;
 326         hash = (53 * hash) + getFamily().hashCode();
 327       }
 328       if (hasColumn()) {
 329         hash = (37 * hash) + COLUMN_FIELD_NUMBER;
 330         hash = (53 * hash) + getColumn().hashCode();
 331       }
 332       hash = (29 * hash) + getUnknownFields().hashCode();
 333       memoizedHashCode = hash;
 334       return hash;
 335     }
 336 
 337     public static com.endpoint.test.Sum.SumRequest parseFrom(
 338         com.google.protobuf.ByteString data)
 339         throws com.google.protobuf.InvalidProtocolBufferException {
 340       return PARSER.parseFrom(data);
 341     }
 342     public static com.endpoint.test.Sum.SumRequest parseFrom(
 343         com.google.protobuf.ByteString data,
 344         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
 345         throws com.google.protobuf.InvalidProtocolBufferException {
 346       return PARSER.parseFrom(data, extensionRegistry);
 347     }
 348     public static com.endpoint.test.Sum.SumRequest parseFrom(byte[] data)
 349         throws com.google.protobuf.InvalidProtocolBufferException {
 350       return PARSER.parseFrom(data);
 351     }
 352     public static com.endpoint.test.Sum.SumRequest parseFrom(
 353         byte[] data,
 354         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
 355         throws com.google.protobuf.InvalidProtocolBufferException {
 356       return PARSER.parseFrom(data, extensionRegistry);
 357     }
 358     public static com.endpoint.test.Sum.SumRequest parseFrom(java.io.InputStream input)
 359         throws java.io.IOException {
 360       return PARSER.parseFrom(input);
 361     }
 362     public static com.endpoint.test.Sum.SumRequest parseFrom(
 363         java.io.InputStream input,
 364         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
 365         throws java.io.IOException {
 366       return PARSER.parseFrom(input, extensionRegistry);
 367     }
 368     public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom(java.io.InputStream input)
 369         throws java.io.IOException {
 370       return PARSER.parseDelimitedFrom(input);
 371     }
 372     public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom(
 373         java.io.InputStream input,
 374         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
 375         throws java.io.IOException {
 376       return PARSER.parseDelimitedFrom(input, extensionRegistry);
 377     }
 378     public static com.endpoint.test.Sum.SumRequest parseFrom(
 379         com.google.protobuf.CodedInputStream input)
 380         throws java.io.IOException {
 381       return PARSER.parseFrom(input);
 382     }
 383     public static com.endpoint.test.Sum.SumRequest parseFrom(
[How to] 使用HBase协处理器---Endpoint客户端代码的实现

HBase总结(10)--协处理器

HBase-协处理器详解及实现

HBase 协处理器入门及实战

hbase协处理器Coprocessor(简介)

HBase 中加盐之后的表如何读取:协处理器篇