hbase协处理器编码实例

这篇具有很好参考价值的文章主要介绍了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下载:Releases · protocolbuffers/protobuf · GitHub,也可以自己编译生成,参见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。

解压文件:

hbase协处理器编码实例

使用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")的名称要一致。

// Generated by the protocol buffer compiler.  DO NOT EDIT!
 // source: sumcode.proto
 
 package com.endpoint.test;
 
 public final class Sum {
   private Sum() {}
   public static void registerAllExtensions(
       com.google.protobuf.ExtensionRegistry registry) {
   }
   public interface SumRequestOrBuilder
       extends com.google.protobuf.MessageOrBuilder {
 
     // required string family = 1;
     /**
      * <code>required string family = 1;</code>
      */
     boolean hasFamily();
     /**
      * <code>required string family = 1;</code>
      */
     java.lang.String getFamily();
     /**
      * <code>required string family = 1;</code>
      */
     com.google.protobuf.ByteString
         getFamilyBytes();
 
     // required string column = 2;
     /**
      * <code>required string column = 2;</code>
      */
     boolean hasColumn();
     /**
      * <code>required string column = 2;</code>
      */
     java.lang.String getColumn();
     /**
      * <code>required string column = 2;</code>
      */
     com.google.protobuf.ByteString
         getColumnBytes();
   }
   /**
    * Protobuf type {@code SumRequest}
    */
   public static final class SumRequest extends
       com.google.protobuf.GeneratedMessage
       implements SumRequestOrBuilder {
     // Use SumRequest.newBuilder() to construct.
     private SumRequest(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
       super(builder);
       this.unknownFields = builder.getUnknownFields();
     }
     private SumRequest(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
 
     private static final SumRequest defaultInstance;
     public static SumRequest getDefaultInstance() {
       return defaultInstance;
     }
 
     public SumRequest getDefaultInstanceForType() {
       return defaultInstance;
     }
 
     private final com.google.protobuf.UnknownFieldSet unknownFields;
     @java.lang.Override
     public final com.google.protobuf.UnknownFieldSet
         getUnknownFields() {
       return this.unknownFields;
     }
     private SumRequest(
         com.google.protobuf.CodedInputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       initFields();
       int mutable_bitField0_ = 0;
       com.google.protobuf.UnknownFieldSet.Builder unknownFields =
           com.google.protobuf.UnknownFieldSet.newBuilder();
       try {
         boolean done = false;
         while (!done) {
           int tag = input.readTag();
           switch (tag) {
             case 0:
               done = true;
               break;
             default: {
               if (!parseUnknownField(input, unknownFields,
                                      extensionRegistry, tag)) {
                 done = true;
               }
               break;
             }
             case 10: {
               bitField0_ |= 0x00000001;
               family_ = input.readBytes();
               break;
             }
             case 18: {
               bitField0_ |= 0x00000002;
               column_ = input.readBytes();
               break;
             }
           }
         }
       } catch (com.google.protobuf.InvalidProtocolBufferException e) {
         throw e.setUnfinishedMessage(this);
       } catch (java.io.IOException e) {
         throw new com.google.protobuf.InvalidProtocolBufferException(
             e.getMessage()).setUnfinishedMessage(this);
       } finally {
         this.unknownFields = unknownFields.build();
         makeExtensionsImmutable();
       }
     }
     public static final com.google.protobuf.Descriptors.Descriptor
         getDescriptor() {
       return com.endpoint.test.Sum.internal_static_SumRequest_descriptor;
     }
 
     protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
         internalGetFieldAccessorTable() {
       return com.endpoint.test.Sum.internal_static_SumRequest_fieldAccessorTable
           .ensureFieldAccessorsInitialized(
               com.endpoint.test.Sum.SumRequest.class, com.endpoint.test.Sum.SumRequest.Builder.class);
     }
 
     public static com.google.protobuf.Parser<SumRequest> PARSER =
         new com.google.protobuf.AbstractParser<SumRequest>() {
       public SumRequest parsePartialFrom(
           com.google.protobuf.CodedInputStream input,
           com.google.protobuf.ExtensionRegistryLite extensionRegistry)
           throws com.google.protobuf.InvalidProtocolBufferException {
         return new SumRequest(input, extensionRegistry);
       }
     };
 
     @java.lang.Override
     public com.google.protobuf.Parser<SumRequest> getParserForType() {
       return PARSER;
     }
 
     private int bitField0_;
     // required string family = 1;
     public static final int FAMILY_FIELD_NUMBER = 1;
     private java.lang.Object family_;
     /**
      * <code>required string family = 1;</code>
      */
     public boolean hasFamily() {
       return ((bitField0_ & 0x00000001) == 0x00000001);
     }
     /**
      * <code>required string family = 1;</code>
      */
     public java.lang.String getFamily() {
       java.lang.Object ref = family_;
       if (ref instanceof java.lang.String) {
         return (java.lang.String) ref;
       } else {
         com.google.protobuf.ByteString bs =
             (com.google.protobuf.ByteString) ref;
         java.lang.String s = bs.toStringUtf8();
         if (bs.isValidUtf8()) {
           family_ = s;
         }
         return s;
       }
     }
     /**
      * <code>required string family = 1;</code>
      */
     public com.google.protobuf.ByteString
         getFamilyBytes() {
       java.lang.Object ref = family_;
       if (ref instanceof java.lang.String) {
         com.google.protobuf.ByteString b =
             com.google.protobuf.ByteString.copyFromUtf8(
                 (java.lang.String) ref);
         family_ = b;
         return b;
       } else {
         return (com.google.protobuf.ByteString) ref;
       }
     }
 
     // required string column = 2;
     public static final int COLUMN_FIELD_NUMBER = 2;
     private java.lang.Object column_;
     /**
      * <code>required string column = 2;</code>
      */
     public boolean hasColumn() {
       return ((bitField0_ & 0x00000002) == 0x00000002);
     }
     /**
      * <code>required string column = 2;</code>
      */
     public java.lang.String getColumn() {
       java.lang.Object ref = column_;
       if (ref instanceof java.lang.String) {
         return (java.lang.String) ref;
       } else {
         com.google.protobuf.ByteString bs =
             (com.google.protobuf.ByteString) ref;
         java.lang.String s = bs.toStringUtf8();
         if (bs.isValidUtf8()) {
           column_ = s;
         }
         return s;
       }
     }
     /**
      * <code>required string column = 2;</code>
      */
     public com.google.protobuf.ByteString
         getColumnBytes() {
       java.lang.Object ref = column_;
       if (ref instanceof java.lang.String) {
         com.google.protobuf.ByteString b =
             com.google.protobuf.ByteString.copyFromUtf8(
                 (java.lang.String) ref);
         column_ = b;
         return b;
       } else {
         return (com.google.protobuf.ByteString) ref;
       }
     }
 
     private void initFields() {
       family_ = "";
       column_ = "";
     }
     private byte memoizedIsInitialized = -1;
     public final boolean isInitialized() {
       byte isInitialized = memoizedIsInitialized;
       if (isInitialized != -1) return isInitialized == 1;
 
       if (!hasFamily()) {
         memoizedIsInitialized = 0;
         return false;
       }
       if (!hasColumn()) {
         memoizedIsInitialized = 0;
         return false;
       }
       memoizedIsInitialized = 1;
       return true;
     }
 
     public void writeTo(com.google.protobuf.CodedOutputStream output)
                         throws java.io.IOException {
       getSerializedSize();
       if (((bitField0_ & 0x00000001) == 0x00000001)) {
         output.writeBytes(1, getFamilyBytes());
       }
       if (((bitField0_ & 0x00000002) == 0x00000002)) {
         output.writeBytes(2, getColumnBytes());
       }
       getUnknownFields().writeTo(output);
     }
 
     private int memoizedSerializedSize = -1;
     public int getSerializedSize() {
       int size = memoizedSerializedSize;
       if (size != -1) return size;
 
       size = 0;
       if (((bitField0_ & 0x00000001) == 0x00000001)) {
         size += com.google.protobuf.CodedOutputStream
           .computeBytesSize(1, getFamilyBytes());
       }
       if (((bitField0_ & 0x00000002) == 0x00000002)) {
         size += com.google.protobuf.CodedOutputStream
           .computeBytesSize(2, getColumnBytes());
       }
       size += getUnknownFields().getSerializedSize();
       memoizedSerializedSize = size;
       return size;
     }
 
     private static final long serialVersionUID = 0L;
     @java.lang.Override
     protected java.lang.Object writeReplace()
         throws java.io.ObjectStreamException {
       return super.writeReplace();
     }
 
     @java.lang.Override
     public boolean equals(final java.lang.Object obj) {
       if (obj == this) {
        return true;
       }
       if (!(obj instanceof com.endpoint.test.Sum.SumRequest)) {
         return super.equals(obj);
       }
       com.endpoint.test.Sum.SumRequest other = (com.endpoint.test.Sum.SumRequest) obj;
 
       boolean result = true;
       result = result && (hasFamily() == other.hasFamily());
       if (hasFamily()) {
         result = result && getFamily()
             .equals(other.getFamily());
       }
       result = result && (hasColumn() == other.hasColumn());
       if (hasColumn()) {
         result = result && getColumn()
             .equals(other.getColumn());
       }
       result = result &&
           getUnknownFields().equals(other.getUnknownFields());
       return result;
     }
 
     private int memoizedHashCode = 0;
     @java.lang.Override
     public int hashCode() {
       if (memoizedHashCode != 0) {
         return memoizedHashCode;
       }
       int hash = 41;
       hash = (19 * hash) + getDescriptorForType().hashCode();
       if (hasFamily()) {
         hash = (37 * hash) + FAMILY_FIELD_NUMBER;
         hash = (53 * hash) + getFamily().hashCode();
       }
       if (hasColumn()) {
         hash = (37 * hash) + COLUMN_FIELD_NUMBER;
         hash = (53 * hash) + getColumn().hashCode();
       }
       hash = (29 * hash) + getUnknownFields().hashCode();
       memoizedHashCode = hash;
       return hash;
     }
 
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         com.google.protobuf.ByteString data)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         com.google.protobuf.ByteString data,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(byte[] data)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         byte[] data,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(java.io.InputStream input)
         throws java.io.IOException {
       return PARSER.parseFrom(input);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         java.io.InputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseFrom(input, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom(java.io.InputStream input)
         throws java.io.IOException {
       return PARSER.parseDelimitedFrom(input);
     }
     public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom(
         java.io.InputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseDelimitedFrom(input, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         com.google.protobuf.CodedInputStream input)
         throws java.io.IOException {
       return PARSER.parseFrom(input);
     }
     public static com.endpoint.test.Sum.SumRequest parseFrom(
         com.google.protobuf.CodedInputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseFrom(input, extensionRegistry);
     }
 
     public static Builder newBuilder() { return Builder.create(); }
     public Builder newBuilderForType() { return newBuilder(); }
     public static Builder newBuilder(com.endpoint.test.Sum.SumRequest prototype) {
       return newBuilder().mergeFrom(prototype);
     }
     public Builder toBuilder() { return newBuilder(this); }
 
     @java.lang.Override
     protected Builder newBuilderForType(
         com.google.protobuf.GeneratedMessage.BuilderParent parent) {
       Builder builder = new Builder(parent);
       return builder;
     }
     /**
      * Protobuf type {@code SumRequest}
      */
     public static final class Builder extends
         com.google.protobuf.GeneratedMessage.Builder<Builder>
        implements com.endpoint.test.Sum.SumRequestOrBuilder {
       public static final com.google.protobuf.Descriptors.Descriptor
           getDescriptor() {
         return com.endpoint.test.Sum.internal_static_SumRequest_descriptor;
       }
 
       protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
           internalGetFieldAccessorTable() {
         return com.endpoint.test.Sum.internal_static_SumRequest_fieldAccessorTable
             .ensureFieldAccessorsInitialized(
                 com.endpoint.test.Sum.SumRequest.class, com.endpoint.test.Sum.SumRequest.Builder.class);
       }
 
       // Construct using com.endpoint.test.Sum.SumRequest.newBuilder()
       private Builder() {
         maybeForceBuilderInitialization();
       }
 
       private Builder(
           com.google.protobuf.GeneratedMessage.BuilderParent parent) {
         super(parent);
         maybeForceBuilderInitialization();
       }
       private void maybeForceBuilderInitialization() {
         if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
         }
       }
       private static Builder create() {
         return new Builder();
       }
 
       public Builder clear() {
         super.clear();
         family_ = "";
         bitField0_ = (bitField0_ & ~0x00000001);
         column_ = "";
         bitField0_ = (bitField0_ & ~0x00000002);
         return this;
       }
 
       public Builder clone() {
         return create().mergeFrom(buildPartial());
       }
 
       public com.google.protobuf.Descriptors.Descriptor
           getDescriptorForType() {
         return com.endpoint.test.Sum.internal_static_SumRequest_descriptor;
       }
 
       public com.endpoint.test.Sum.SumRequest getDefaultInstanceForType() {
         return com.endpoint.test.Sum.SumRequest.getDefaultInstance();
       }
 
       public com.endpoint.test.Sum.SumRequest build() {
         com.endpoint.test.Sum.SumRequest result = buildPartial();
         if (!result.isInitialized()) {
           throw newUninitializedMessageException(result);
         }
         return result;
       }
 
       public com.endpoint.test.Sum.SumRequest buildPartial() {
         com.endpoint.test.Sum.SumRequest result = new com.endpoint.test.Sum.SumRequest(this);
         int from_bitField0_ = bitField0_;
         int to_bitField0_ = 0;
         if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
           to_bitField0_ |= 0x00000001;
         }
         result.family_ = family_;
         if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
           to_bitField0_ |= 0x00000002;
         }
         result.column_ = column_;
         result.bitField0_ = to_bitField0_;
         onBuilt();
         return result;
       }
 
       public Builder mergeFrom(com.google.protobuf.Message other) {
         if (other instanceof com.endpoint.test.Sum.SumRequest) {
           return mergeFrom((com.endpoint.test.Sum.SumRequest)other);
         } else {
           super.mergeFrom(other);
           return this;
         }
       }
 
       public Builder mergeFrom(com.endpoint.test.Sum.SumRequest other) {
         if (other == com.endpoint.test.Sum.SumRequest.getDefaultInstance()) return this;
         if (other.hasFamily()) {
           bitField0_ |= 0x00000001;
           family_ = other.family_;
           onChanged();
         }
         if (other.hasColumn()) {
           bitField0_ |= 0x00000002;
           column_ = other.column_;
           onChanged();
         }
         this.mergeUnknownFields(other.getUnknownFields());
         return this;
       }
 
       public final boolean isInitialized() {
         if (!hasFamily()) {
 
           return false;
         }
         if (!hasColumn()) {
 
           return false;
         }
         return true;
       }
 
       public Builder mergeFrom(
           com.google.protobuf.CodedInputStream input,
           com.google.protobuf.ExtensionRegistryLite extensionRegistry)
           throws java.io.IOException {
         com.endpoint.test.Sum.SumRequest parsedMessage = null;
         try {
           parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
         } catch (com.google.protobuf.InvalidProtocolBufferException e) {
           parsedMessage = (com.endpoint.test.Sum.SumRequest) e.getUnfinishedMessage();
           throw e;
         } finally {
           if (parsedMessage != null) {
             mergeFrom(parsedMessage);
           }
         }
         return this;
       }
       private int bitField0_;
 
       // required string family = 1;
       private java.lang.Object family_ = "";
       /**
        * <code>required string family = 1;</code>
        */
       public boolean hasFamily() {
         return ((bitField0_ & 0x00000001) == 0x00000001);
       }
       /**
        * <code>required string family = 1;</code>
        */
       public java.lang.String getFamily() {
         java.lang.Object ref = family_;
         if (!(ref instanceof java.lang.String)) {
           java.lang.String s = ((com.google.protobuf.ByteString) ref)
               .toStringUtf8();
           family_ = s;
           return s;
         } else {
           return (java.lang.String) ref;
         }
       }
       /**
        * <code>required string family = 1;</code>
        */
       public com.google.protobuf.ByteString
           getFamilyBytes() {
         java.lang.Object ref = family_;
         if (ref instanceof String) {
           com.google.protobuf.ByteString b =
               com.google.protobuf.ByteString.copyFromUtf8(
                   (java.lang.String) ref);
           family_ = b;
           return b;
         } else {
           return (com.google.protobuf.ByteString) ref;
         }
       }
       /**
        * <code>required string family = 1;</code>
        */
       public Builder setFamily(
           java.lang.String value) {
         if (value == null) {
     throw new NullPointerException();
   }
   bitField0_ |= 0x00000001;
         family_ = value;
         onChanged();
         return this;
       }
       /**
        * <code>required string family = 1;</code>
        */
       public Builder clearFamily() {
         bitField0_ = (bitField0_ & ~0x00000001);
         family_ = getDefaultInstance().getFamily();
         onChanged();
         return this;
       }
       /**
        * <code>required string family = 1;</code>
        */
       public Builder setFamilyBytes(
           com.google.protobuf.ByteString value) {
         if (value == null) {
     throw new NullPointerException();
   }
   bitField0_ |= 0x00000001;
         family_ = value;
         onChanged();
         return this;
       }
 
       // required string column = 2;
       private java.lang.Object column_ = "";
       /**
        * <code>required string column = 2;</code>
        */
       public boolean hasColumn() {
         return ((bitField0_ & 0x00000002) == 0x00000002);
       }
       /**
        * <code>required string column = 2;</code>
        */
       public java.lang.String getColumn() {
         java.lang.Object ref = column_;
         if (!(ref instanceof java.lang.String)) {
           java.lang.String s = ((com.google.protobuf.ByteString) ref)
               .toStringUtf8();
           column_ = s;
           return s;
         } else {
           return (java.lang.String) ref;
         }
       }
       /**
        * <code>required string column = 2;</code>
        */
       public com.google.protobuf.ByteString
           getColumnBytes() {
         java.lang.Object ref = column_;
         if (ref instanceof String) {
           com.google.protobuf.ByteString b =
               com.google.protobuf.ByteString.copyFromUtf8(
                   (java.lang.String) ref);
           column_ = b;
           return b;
         } else {
           return (com.google.protobuf.ByteString) ref;
         }
       }
       /**
        * <code>required string column = 2;</code>
        */
       public Builder setColumn(
           java.lang.String value) {
         if (value == null) {
     throw new NullPointerException();
   }
   bitField0_ |= 0x00000002;
         column_ = value;
         onChanged();
         return this;
       }
       /**
        * <code>required string column = 2;</code>
        */
       public Builder clearColumn() {
         bitField0_ = (bitField0_ & ~0x00000002);
         column_ = getDefaultInstance().getColumn();
         onChanged();
         return this;
       }
       /**
        * <code>required string column = 2;</code>
        */
       public Builder setColumnBytes(
           com.google.protobuf.ByteString value) {
         if (value == null) {
     throw new NullPointerException();
   }
   bitField0_ |= 0x00000002;
         column_ = value;
         onChanged();
         return this;
       }
 
       // @@protoc_insertion_point(builder_scope:SumRequest)
     }
 
     static {
       defaultInstance = new SumRequest(true);
       defaultInstance.initFields();
     }
 
     // @@protoc_insertion_point(class_scope:SumRequest)
   }
 
   public interface SumResponseOrBuilder
       extends com.google.protobuf.MessageOrBuilder {
 
     // required int64 sum = 1 [default = 0];
     /**
      * <code>required int64 sum = 1 [default = 0];</code>
      */
     boolean hasSum();
     /**
      * <code>required int64 sum = 1 [default = 0];</code>
      */
     long getSum();
   }
   /**
    * Protobuf type {@code SumResponse}
    */
   public static final class SumResponse extends
       com.google.protobuf.GeneratedMessage
       implements SumResponseOrBuilder {
     // Use SumResponse.newBuilder() to construct.
     private SumResponse(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
       super(builder);
       this.unknownFields = builder.getUnknownFields();
     }
     private SumResponse(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
 
     private static final SumResponse defaultInstance;
     public static SumResponse getDefaultInstance() {
       return defaultInstance;
     }
 
     public SumResponse getDefaultInstanceForType() {
       return defaultInstance;
     }
 
     private final com.google.protobuf.UnknownFieldSet unknownFields;
     @java.lang.Override
     public final com.google.protobuf.UnknownFieldSet
         getUnknownFields() {
       return this.unknownFields;
     }
     private SumResponse(
         com.google.protobuf.CodedInputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       initFields();
       int mutable_bitField0_ = 0;
       com.google.protobuf.UnknownFieldSet.Builder unknownFields =
           com.google.protobuf.UnknownFieldSet.newBuilder();
       try {
         boolean done = false;
         while (!done) {
           int tag = input.readTag();
           switch (tag) {
             case 0:
               done = true;
               break;
             default: {
               if (!parseUnknownField(input, unknownFields,
                                      extensionRegistry, tag)) {
                 done = true;
               }
               break;
             }
             case 8: {
               bitField0_ |= 0x00000001;
               sum_ = input.readInt64();
               break;
             }
           }
         }
       } catch (com.google.protobuf.InvalidProtocolBufferException e) {
         throw e.setUnfinishedMessage(this);
       } catch (java.io.IOException e) {
         throw new com.google.protobuf.InvalidProtocolBufferException(
             e.getMessage()).setUnfinishedMessage(this);
       } finally {
         this.unknownFields = unknownFields.build();
         makeExtensionsImmutable();
       }
     }
     public static final com.google.protobuf.Descriptors.Descriptor
         getDescriptor() {
       return com.endpoint.test.Sum.internal_static_SumResponse_descriptor;
     }
 
     protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
         internalGetFieldAccessorTable() {
       return com.endpoint.test.Sum.internal_static_SumResponse_fieldAccessorTable
           .ensureFieldAccessorsInitialized(
               com.endpoint.test.Sum.SumResponse.class, com.endpoint.test.Sum.SumResponse.Builder.class);
     }
 
     public static com.google.protobuf.Parser<SumResponse> PARSER =
         new com.google.protobuf.AbstractParser<SumResponse>() {
       public SumResponse parsePartialFrom(
           com.google.protobuf.CodedInputStream input,
           com.google.protobuf.ExtensionRegistryLite extensionRegistry)
           throws com.google.protobuf.InvalidProtocolBufferException {
         return new SumResponse(input, extensionRegistry);
       }
     };
 
     @java.lang.Override
     public com.google.protobuf.Parser<SumResponse> getParserForType() {
       return PARSER;
     }
 
     private int bitField0_;
     // required int64 sum = 1 [default = 0];
     public static final int SUM_FIELD_NUMBER = 1;
     private long sum_;
     /**
      * <code>required int64 sum = 1 [default = 0];</code>
      */
     public boolean hasSum() {
       return ((bitField0_ & 0x00000001) == 0x00000001);
     }
     /**
      * <code>required int64 sum = 1 [default = 0];</code>
      */
     public long getSum() {
       return sum_;
     }
 
     private void initFields() {
       sum_ = 0L;
     }
     private byte memoizedIsInitialized = -1;
     public final boolean isInitialized() {
       byte isInitialized = memoizedIsInitialized;
       if (isInitialized != -1) return isInitialized == 1;
 
       if (!hasSum()) {
         memoizedIsInitialized = 0;
         return false;
       }
       memoizedIsInitialized = 1;
       return true;
     }
 
     public void writeTo(com.google.protobuf.CodedOutputStream output)
                         throws java.io.IOException {
       getSerializedSize();
       if (((bitField0_ & 0x00000001) == 0x00000001)) {
         output.writeInt64(1, sum_);
       }
       getUnknownFields().writeTo(output);
     }
 
     private int memoizedSerializedSize = -1;
     public int getSerializedSize() {
       int size = memoizedSerializedSize;
       if (size != -1) return size;
 
       size = 0;
       if (((bitField0_ & 0x00000001) == 0x00000001)) {
         size += com.google.protobuf.CodedOutputStream
           .computeInt64Size(1, sum_);
       }
       size += getUnknownFields().getSerializedSize();
       memoizedSerializedSize = size;
       return size;
     }
 
     private static final long serialVersionUID = 0L;
     @java.lang.Override
     protected java.lang.Object writeReplace()
         throws java.io.ObjectStreamException {
       return super.writeReplace();
     }
 
     @java.lang.Override
     public boolean equals(final java.lang.Object obj) {
       if (obj == this) {
        return true;
       }
       if (!(obj instanceof com.endpoint.test.Sum.SumResponse)) {
         return super.equals(obj);
       }
       com.endpoint.test.Sum.SumResponse other = (com.endpoint.test.Sum.SumResponse) obj;
 
       boolean result = true;
       result = result && (hasSum() == other.hasSum());
       if (hasSum()) {
         result = result && (getSum()
             == other.getSum());
       }
       result = result &&
           getUnknownFields().equals(other.getUnknownFields());
       return result;
     }
 
     private int memoizedHashCode = 0;
     @java.lang.Override
     public int hashCode() {
       if (memoizedHashCode != 0) {
         return memoizedHashCode;
       }
       int hash = 41;
       hash = (19 * hash) + getDescriptorForType().hashCode();
       if (hasSum()) {
         hash = (37 * hash) + SUM_FIELD_NUMBER;
         hash = (53 * hash) + hashLong(getSum());
       }
       hash = (29 * hash) + getUnknownFields().hashCode();
       memoizedHashCode = hash;
       return hash;
     }
 
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         com.google.protobuf.ByteString data)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         com.google.protobuf.ByteString data,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(byte[] data)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         byte[] data,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws com.google.protobuf.InvalidProtocolBufferException {
       return PARSER.parseFrom(data, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(java.io.InputStream input)
         throws java.io.IOException {
       return PARSER.parseFrom(input);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         java.io.InputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseFrom(input, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumResponse parseDelimitedFrom(java.io.InputStream input)
         throws java.io.IOException {
       return PARSER.parseDelimitedFrom(input);
     }
     public static com.endpoint.test.Sum.SumResponse parseDelimitedFrom(
         java.io.InputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseDelimitedFrom(input, extensionRegistry);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         com.google.protobuf.CodedInputStream input)
         throws java.io.IOException {
       return PARSER.parseFrom(input);
     }
     public static com.endpoint.test.Sum.SumResponse parseFrom(
         com.google.protobuf.CodedInputStream input,
         com.google.protobuf.ExtensionRegistryLite extensionRegistry)
         throws java.io.IOException {
       return PARSER.parseFrom(input, extensionRegistry);
     }
 
     public static Builder newBuilder() { return Builder.create(); }
     public Builder newBuilderForType() { return newBuilder(); }
     public static Builder newBuilder(com.endpoint.test.Sum.SumResponse prototype) {
       return newBuilder().mergeFrom(prototype);
     }
     public Builder toBuilder() { return newBuilder(this); }
 
     @java.lang.Override
     protected Builder newBuilderForType(
         com.google.protobuf.GeneratedMessage.BuilderParent parent) {
       Builder builder = new Builder(parent);
       return builder;
     }
     /**
      * Protobuf type {@code SumResponse}
      */
     public static final class Builder extends
         com.google.protobuf.GeneratedMessage.Builder<Builder>
        implements com.endpoint.test.Sum.SumResponseOrBuilder {
       public static final com.google.protobuf.Descriptors.Descriptor
           getDescriptor() {
         return com.endpoint.test.Sum.internal_static_SumResponse_descriptor;
       }
 
       protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
           internalGetFieldAccessorTable() {
         return com.endpoint.test.Sum.internal_static_SumResponse_fieldAccessorTable
             .ensureFieldAccessorsInitialized(
                 com.endpoint.test.Sum.SumResponse.class, com.endpoint.test.Sum.SumResponse.Builder.class);
       }
 
       // Construct using com.endpoint.test.Sum.SumResponse.newBuilder()
       private Builder() {
         maybeForceBuilderInitialization();
       }
 
       private Builder(
           com.google.protobuf.GeneratedMessage.BuilderParent parent) {
         super(parent);
         maybeForceBuilderInitialization();
       }
       private void maybeForceBuilderInitialization() {
         if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
         }
       }
       private static Builder create() {
         return new Builder();
       }
 
       public Builder clear() {
         super.clear();
         sum_ = 0L;
         bitField0_ = (bitField0_ & ~0x00000001);
         return this;
       }
 
       public Builder clone() {
         return create().mergeFrom(buildPartial());
       }
 
       public com.google.protobuf.Descriptors.Descriptor
           getDescriptorForType() {
         return com.endpoint.test.Sum.internal_static_SumResponse_descriptor;
       }
 
       public com.endpoint.test.Sum.SumResponse getDefaultInstanceForType() {
         return com.endpoint.test.Sum.SumResponse.getDefaultInstance();
       }
 
       public com.endpoint.test.Sum.SumResponse build() {
         com.endpoint.test.Sum.SumResponse result = buildPartial();
         if (!result.isInitialized()) {
           throw newUninitializedMessageException(result);
         }
         return result;
       }
 
       public com.endpoint.test.Sum.SumResponse buildPartial() {
         com.endpoint.test.Sum.SumResponse result = new com.endpoint.test.Sum.SumResponse(this);
         int from_bitField0_ = bitField0_;
         int to_bitField0_ = 0;
         if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
           to_bitField0_ |= 0x00000001;
         }
         result.sum_ = sum_;
         result.bitField0_ = to_bitField0_;
         onBuilt();
         return result;
       }
 
       public Builder mergeFrom(com.google.protobuf.Message other) {
         if (other instanceof com.endpoint.test.Sum.SumResponse) {
           return mergeFrom((com.endpoint.test.Sum.SumResponse)other);
         } else {
           super.mergeFrom(other);
           return this;
         }
       }
 
       public Builder mergeFrom(com.endpoint.test.Sum.SumResponse other) {
         if (other == com.endpoint.test.Sum.SumResponse.getDefaultInstance()) return this;
         if (other.hasSum()) {
           setSum(other.getSum());
         }
         this.mergeUnknownFields(other.getUnknownFields());
         return this;
       }
 
       public final boolean isInitialized() {
         if (!hasSum()) {
 
           return false;
         }
         return true;
       }
 
       public Builder mergeFrom(
           com.google.protobuf.CodedInputStream input,
           com.google.protobuf.ExtensionRegistryLite extensionRegistry)
           throws java.io.IOException {
         com.endpoint.test.Sum.SumResponse parsedMessage = null;
         try {
           parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
         } catch (com.google.protobuf.InvalidProtocolBufferException e) {
           parsedMessage = (com.endpoint.test.Sum.SumResponse) e.getUnfinishedMessage();
           throw e;
         } finally {
           if (parsedMessage != null) {
             mergeFrom(parsedMessage);
           }
         }
         return this;
       }
       private int bitField0_;
 
       // required int64 sum = 1 [default = 0];
       private long sum_ ;
       /**
        * <code>required int64 sum = 1 [default = 0];</code>
        */
       public boolean hasSum() {
         return ((bitField0_ & 0x00000001) == 0x00000001);
       }
       /**
        * <code>required int64 sum = 1 [default = 0];</code>
        */
       public long getSum() {
         return sum_;
       }
       /**
        * <code>required int64 sum = 1 [default = 0];</code>
        */
       public Builder setSum(long value) {
         bitField0_ |= 0x00000001;
         sum_ = value;
         onChanged();
         return this;
       }
       /**
        * <code>required int64 sum = 1 [default = 0];</code>
        */
       public Builder clearSum() {
         bitField0_ = (bitField0_ & ~0x00000001);
         sum_ = 0L;
         onChanged();
         return this;
       }
 
       // @@protoc_insertion_point(builder_scope:SumResponse)
     }
 
     static {
       defaultInstance = new SumResponse(true);
       defaultInstance.initFields();
     }
 
     // @@protoc_insertion_point(class_scope:SumResponse)
   }
 
   /**
    * Protobuf service {@code SumService}
    */
   public static abstract class SumService
       implements com.google.protobuf.Service {
     protected SumService() {}
 
     public interface Interface {
       /**
        * <code>rpc getSum(.SumRequest) returns (.SumResponse);</code>
        */
       public abstract void getSum(
           com.google.protobuf.RpcController controller,
           com.endpoint.test.Sum.SumRequest request,
           com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done);
 
     }
 
     public static com.google.protobuf.Service newReflectiveService(
         final Interface impl) {
       return new SumService() {
         @java.lang.Override
         public  void getSum(
             com.google.protobuf.RpcController controller,
             com.endpoint.test.Sum.SumRequest request,
             com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done) {
           impl.getSum(controller, request, done);
         }
 
       };
     }
 
     public static com.google.protobuf.BlockingService
         newReflectiveBlockingService(final BlockingInterface impl) {
       return new com.google.protobuf.BlockingService() {
         public final com.google.protobuf.Descriptors.ServiceDescriptor
             getDescriptorForType() {
           return getDescriptor();
         }
 
         public final com.google.protobuf.Message callBlockingMethod(
             com.google.protobuf.Descriptors.MethodDescriptor method,
             com.google.protobuf.RpcController controller,
             com.google.protobuf.Message request)
             throws com.google.protobuf.ServiceException {
           if (method.getService() != getDescriptor()) {
             throw new java.lang.IllegalArgumentException(
               "Service.callBlockingMethod() given method descriptor for " +
               "wrong service type.");
           }
           switch(method.getIndex()) {
             case 0:
               return impl.getSum(controller, (com.endpoint.test.Sum.SumRequest)request);
             default:
               throw new java.lang.AssertionError("Can't get here.");
           }
         }
 
         public final com.google.protobuf.Message
             getRequestPrototype(
             com.google.protobuf.Descriptors.MethodDescriptor method) {
           if (method.getService() != getDescriptor()) {
             throw new java.lang.IllegalArgumentException(
               "Service.getRequestPrototype() given method " +
               "descriptor for wrong service type.");
           }
           switch(method.getIndex()) {
             case 0:
               return com.endpoint.test.Sum.SumRequest.getDefaultInstance();
             default:
               throw new java.lang.AssertionError("Can't get here.");
           }
         }
 
         public final com.google.protobuf.Message
             getResponsePrototype(
             com.google.protobuf.Descriptors.MethodDescriptor method) {
           if (method.getService() != getDescriptor()) {
             throw new java.lang.IllegalArgumentException(
               "Service.getResponsePrototype() given method " +
               "descriptor for wrong service type.");
           }
           switch(method.getIndex()) {
             case 0:
               return com.endpoint.test.Sum.SumResponse.getDefaultInstance();
             default:
               throw new java.lang.AssertionError("Can't get here.");
           }
         }
 
       };
     }
 
     /**
      * <code>rpc getSum(.SumRequest) returns (.SumResponse);</code>
      */
     public abstract void getSum(
         com.google.protobuf.RpcController controller,
         com.endpoint.test.Sum.SumRequest request,
         com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done);
 
     public static final
         com.google.protobuf.Descriptors.ServiceDescriptor
         getDescriptor() {
       return com.endpoint.test.Sum.getDescriptor().getServices().get(0);
     }
     public final com.google.protobuf.Descriptors.ServiceDescriptor
         getDescriptorForType() {
       return getDescriptor();
     }
 
     public final void callMethod(
         com.google.protobuf.Descriptors.MethodDescriptor method,
         com.google.protobuf.RpcController controller,
         com.google.protobuf.Message request,
         com.google.protobuf.RpcCallback<
           com.google.protobuf.Message> done) {
       if (method.getService() != getDescriptor()) {
         throw new java.lang.IllegalArgumentException(
           "Service.callMethod() given method descriptor for wrong " +
           "service type.");
       }
       switch(method.getIndex()) {
         case 0:
           this.getSum(controller, (com.endpoint.test.Sum.SumRequest)request,
             com.google.protobuf.RpcUtil.<com.endpoint.test.Sum.SumResponse>specializeCallback(
               done));
           return;
         default:
           throw new java.lang.AssertionError("Can't get here.");
       }
     }
 
     public final com.google.protobuf.Message
         getRequestPrototype(
         com.google.protobuf.Descriptors.MethodDescriptor method) {
       if (method.getService() != getDescriptor()) {
         throw new java.lang.IllegalArgumentException(
           "Service.getRequestPrototype() given method " +
           "descriptor for wrong service type.");
       }
       switch(method.getIndex()) {
         case 0:
           return com.endpoint.test.Sum.SumRequest.getDefaultInstance();
         default:
           throw new java.lang.AssertionError("Can't get here.");
       }
     }
 
     public final com.google.protobuf.Message
         getResponsePrototype(
         com.google.protobuf.Descriptors.MethodDescriptor method) {
       if (method.getService() != getDescriptor()) {
         throw new java.lang.IllegalArgumentException(
           "Service.getResponsePrototype() given method " +
           "descriptor for wrong service type.");
       }
       switch(method.getIndex()) {
         case 0:
           return com.endpoint.test.Sum.SumResponse.getDefaultInstance();
         default:
           throw new java.lang.AssertionError("Can't get here.");
       }
     }
 
     public static Stub newStub(
         com.google.protobuf.RpcChannel channel) {
       return new Stub(channel);
     }
 
     public static final class Stub extends com.endpoint.test.Sum.SumService implements Interface {
       private Stub(com.google.protobuf.RpcChannel channel) {
         this.channel = channel;
       }
 
       private final com.google.protobuf.RpcChannel channel;
 
       public com.google.protobuf.RpcChannel getChannel() {
         return channel;
       }
 
       public  void getSum(
           com.google.protobuf.RpcController controller,
           com.endpoint.test.Sum.SumRequest request,
           com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done) {
         channel.callMethod(
           getDescriptor().getMethods().get(0),
           controller,
           request,
           com.endpoint.test.Sum.SumResponse.getDefaultInstance(),
           com.google.protobuf.RpcUtil.generalizeCallback(
             done,
             com.endpoint.test.Sum.SumResponse.class,
             com.endpoint.test.Sum.SumResponse.getDefaultInstance()));
       }
     }
 
     public static BlockingInterface newBlockingStub(
         com.google.protobuf.BlockingRpcChannel channel) {
       return new BlockingStub(channel);
     }
 
     public interface BlockingInterface {
       public com.endpoint.test.Sum.SumResponse getSum(
           com.google.protobuf.RpcController controller,
           com.endpoint.test.Sum.SumRequest request)
           throws com.google.protobuf.ServiceException;
     }
 
     private static final class BlockingStub implements BlockingInterface {
       private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) {
         this.channel = channel;
       }
 
       private final com.google.protobuf.BlockingRpcChannel channel;
 
       public com.endpoint.test.Sum.SumResponse getSum(
           com.google.protobuf.RpcController controller,
           com.endpoint.test.Sum.SumRequest request)
           throws com.google.protobuf.ServiceException {
         return (com.endpoint.test.Sum.SumResponse) channel.callBlockingMethod(
           getDescriptor().getMethods().get(0),
           controller,
           request,
           com.endpoint.test.Sum.SumResponse.getDefaultInstance());
       }
 
     }
 
     // @@protoc_insertion_point(class_scope:SumService)
   }
 
   private static com.google.protobuf.Descriptors.Descriptor
     internal_static_SumRequest_descriptor;
   private static
     com.google.protobuf.GeneratedMessage.FieldAccessorTable
       internal_static_SumRequest_fieldAccessorTable;
   private static com.google.protobuf.Descriptors.Descriptor
     internal_static_SumResponse_descriptor;
   private static
     com.google.protobuf.GeneratedMessage.FieldAccessorTable
       internal_static_SumResponse_fieldAccessorTable;
 
   public static com.google.protobuf.Descriptors.FileDescriptor
       getDescriptor() {
     return descriptor;
   }
   private static com.google.protobuf.Descriptors.FileDescriptor
       descriptor;
   static {
     java.lang.String[] descriptorData = {
       "\n\rsumcode.proto\",\n\nSumRequest\022\016\n\006family\030" +
       "\001 \002(\t\022\016\n\006column\030\002 \002(\t\"\035\n\013SumResponse\022\016\n\003" +
       "sum\030\001 \002(\003:\001021\n\nSumService\022#\n\006getSum\022\013.S" +
       "umRequest\032\014.SumResponseB \n\021com.endpoint." +
       "testB\003SumH\001\210\001\001\240\001\001"
     };
     com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
       new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() {
         public com.google.protobuf.ExtensionRegistry assignDescriptors(
             com.google.protobuf.Descriptors.FileDescriptor root) {
           descriptor = root;
           internal_static_SumRequest_descriptor =
             getDescriptor().getMessageTypes().get(0);
           internal_static_SumRequest_fieldAccessorTable = new
             com.google.protobuf.GeneratedMessage.FieldAccessorTable(
               internal_static_SumRequest_descriptor,
               new java.lang.String[] { "Family", "Column", });
           internal_static_SumResponse_descriptor =
             getDescriptor().getMessageTypes().get(1);
           internal_static_SumResponse_fieldAccessorTable = new
             com.google.protobuf.GeneratedMessage.FieldAccessorTable(
               internal_static_SumResponse_descriptor,
               new java.lang.String[] { "Sum", });
           return null;
         }
       };
     com.google.protobuf.Descriptors.FileDescriptor
       .internalBuildGeneratedFileFrom(descriptorData,
         new com.google.protobuf.Descriptors.FileDescriptor[] {
         }, assigner);
   }
 
   // @@protoc_insertion_point(outer_class_scope)
 }

2> 编写服务器端的代码

 package com.endpoint.test;
 
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.CellUtil;
 import org.apache.hadoop.hbase.Coprocessor;
 import org.apache.hadoop.hbase.CoprocessorEnvironment;
 import org.apache.hadoop.hbase.client.Scan;
 import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
 import org.apache.hadoop.hbase.coprocessor.CoprocessorService;
 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
 import org.apache.hadoop.hbase.regionserver.InternalScanner;
 import org.apache.hadoop.hbase.util.Bytes;
 import com.endpoint.test.Sum.SumRequest;
 import com.endpoint.test.Sum.SumResponse;
 import com.endpoint.test.Sum.SumService;
 import com.google.protobuf.RpcCallback;
 import com.google.protobuf.RpcController;
 import com.google.protobuf.Service;
 
 public class SumEndPoint extends SumService implements Coprocessor,CoprocessorService{
 
     private RegionCoprocessorEnvironment env;   // 定义环境
     @Override
     public Service getService() {
 
         return this;
     }
 
     @Override
     public void start(CoprocessorEnvironment env) throws IOException {
          if (env instanceof RegionCoprocessorEnvironment) {
                 this.env = (RegionCoprocessorEnvironment)env;
             } else {
                 throw new CoprocessorException("no load region");
             }  
 
     }
 
     @Override
     public void stop(CoprocessorEnvironment env) throws IOException {
 
     }
 
     @Override
     public void getSum(RpcController controller, SumRequest request, RpcCallback<SumResponse> done) {
 
         // 设置扫描对象
         Scan scan = new Scan();
         scan.addFamily(Bytes.toBytes(request.getFamily()));
         scan.addColumn(Bytes.toBytes(request.getFamily()), Bytes.toBytes(request.getColumn()));  
 
         // 定义变量
         SumResponse response = null;
         InternalScanner scanner = null;  
 
         // 扫描每个region,取值后求和
         try {
             scanner = env.getRegion().getScanner(scan);
             List<Cell> results = new ArrayList<Cell>();
             boolean hasMore = false;
             Long sum = 0L;
             do {
                 hasMore = scanner.next(results);
                 for (Cell cell : results) {
                     sum += Long.parseLong(new String(CellUtil.cloneValue(cell)));
                 }
                 results.clear();
             } while (hasMore);
             // 设置返回结果
             response = SumResponse.newBuilder().setSum(sum).build();
         } catch (IOException e) {
             ResponseConverter.setControllerException(controller, e);
         } finally {
             if (scanner != null) {
                 try {
                     scanner.close();
                 } catch (IOException e) {
                     //e.printStackTrace();
                 }
             }
         }
         // 将rpc结果返回给客户端
         done.run(response);  
 
     }
 
 }

3> 客户端测试代码

 package com.endpoint.test;
 
 import java.io.IOException;
 import java.util.Map;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.client.Connection;
 import org.apache.hadoop.hbase.client.ConnectionFactory;
 import org.apache.hadoop.hbase.client.HTable;
 import org.apache.hadoop.hbase.client.coprocessor.Batch;
 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
 import com.endpoint.test.Sum.SumRequest;
 import com.endpoint.test.Sum.SumResponse;
 import com.endpoint.test.Sum.SumService;
 import com.google.protobuf.ServiceException;
 
 public class TestClient {
 
     public static void main(String[] args) throws Exception {
 
             // 配置HBse
             Configuration conf = HBaseConfiguration.create();
             conf.set("hbase.zookeeper.quorum", "master,data1,data2");
             conf.set("hbase.zookeeper.property.clientPort", "2181");
             conf.setLong("hbase.rpc.timeout", 600000);
             System.setProperty("hadoop.home.dir", "C:/hadoopfiles/hadoop-common-2.2.0-bin-master");
 
             // 建立一个数据库的连接
             Connection conn = ConnectionFactory.createConnection(conf);
             // 获取表
             HTable table = (HTable) conn.getTable(TableName.valueOf("etable"));  
 
             long sum = 0L;                
 
             // 设置请求对象
             final SumRequest request = SumRequest.newBuilder().setFamily("cf").setColumn("value").build();  
 
             try {
                 // 获得返回值
                 Map<byte[], Long> result = table.coprocessorService(SumService.class, null, null,
                         new Batch.Call<SumService, Long>() {  
 
                             @Override
                             public Long call(SumService service) throws IOException {
                                 BlockingRpcCallback<SumResponse> rpcCallback = new BlockingRpcCallback<SumResponse>();
                                 service.getSum(null, request, rpcCallback);
                                 SumResponse response = (SumResponse) rpcCallback.get();
                                 return response.hasSum() ? response.getSum() : 0L;
                             }
                 });
                 // 将返回值进行迭代相加
                 for (Long v : result.values()) {
                     sum += v;
                 }
                 // 结果输出
                 System.out.println("sum: " + sum);                        
 
             } catch (ServiceException e) {
                 e.printStackTrace();
             }catch (Throwable e) {
                 e.printStackTrace();
             }
             table.close();
             conn.close();  
 
     }
 
 }

System.setProperty("hadoop.home.dir", "C:/hadoopfiles/hadoop-common-2.2.0-bin-master"); 这句代码是防错误用的,不具有实际意义,在hadoop-common-2.2.0-bin-master下建立bin目录放一个winutils.exe文件即可,否则会出现提示“Could not locate executable null\bin\winutils.exe in the Hadoop binaries”

此外,需要在windows下设置一下hosts文件,因为conf.set("hbase.zookeeper.quorum", "master,data1,data2");

4> 使用Endpoint协处理器

将上面的Sum类文件与用于服务端的SumEndPoint 类文件打包上传到服务器

chown hadoop:hadoop datacode.jar
chmod g+w  datacode.jar 

先改一下权限,之后

hadoop fs -copyFromLocal sumtest.jar /input/

下面是要使用协处理器的hbase表        

hbase协处理器编码实例

要将协处理器加载到这个表上

disable 'etable'
# 包名.类名|权重 com.endpoint.test.SumEndPoint|100
alter'etable',METHOD =>'table_att','coprocessor' =>'/input/sumcode.jar|com.endpoint.test.SumEndPoint|100'
enable 'etable'

 

  1. 包名.类名|权重 com.endpoint.test.SumEndPoint|100
    # 这样也是可以的,但是在集群变换主节点的情况下,不是很好
    # alter'etable',METHOD =>'table_att','coprocessor' =>'hdfs://192.168.1.215:9000/input/sumcode.jar|com.endpoint.test.SumEndPoint|100'

    此外,值得注意的一点,在集群中,最好在hbase-site.xml中设置以下属性

    <property>
            <name>hbase.coprocessor.abortonerror</name>
            <value>false</value>
    </property> 

    设置为false目的在于提高容错性,如果这个属性没有设置为false,则在上传的jar包存在错误的情况下,会导致表不能enable或disable,从而导致集群中的这张表无法使用,甚至会影响到其他表。

  2. 表无法使用,甚至会影响到其他表。

    hbase协处理器编码实例

    在windows中的客户端运行客户端的代码,结果如下:

    hbase协处理器编码实例

     2、Observer实例
    这个是一个二级索引实例,即假定在initialtable表中的数据格式是这样的

    row1    E   151
    row2    Y   158

    在向initialtable表中写入数据时,自动将以下数据写入indextable表作为二级索引,indextable第二列成为indextable的键

    Y    158

    1> 编写服务端代码

     package com.observer.test;
     
     import java.io.IOException;
     import java.util.List;
     import org.apache.hadoop.hbase.Cell;
     import org.apache.hadoop.hbase.CellUtil;
     import org.apache.hadoop.hbase.TableName;
     import org.apache.hadoop.hbase.client.Durability;
     import org.apache.hadoop.hbase.client.HTableInterface;
     import org.apache.hadoop.hbase.client.Put;
     import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
     import org.apache.hadoop.hbase.coprocessor.ObserverContext;
     import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
     import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
     import org.apache.hadoop.hbase.util.Bytes;
     
     public class TestObserver extends BaseRegionObserver {
         @Override
         public void postPut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability)
                 throws IOException {
             // indextable作为二级索引表
             HTableInterface table = e.getEnvironment().getTable(TableName.valueOf("indextable"));
             // 获取值
             List<Cell> cellList1 = put.get(Bytes.toBytes("cf"), Bytes.toBytes("name"));
             List<Cell> cellList2 = put.get(Bytes.toBytes("cf"), Bytes.toBytes("value"));
             // 写入数据
             for (Cell cell1 : cellList1) {
                 // 原表的列cf:name的值作为indextable的rowkey,添加行
                 Put indexPut = new Put(CellUtil.cloneValue(cell1));
                 for (Cell cell2 : cellList2) {
                     // 原表的列cf:value的值作为indextable表中列cf:value的值 。
                     indexPut.add(Bytes.toBytes("cf"), Bytes.toBytes("value"), CellUtil.cloneValue(cell2));
                 }  
     
                 table.put(indexPut);
             }  
     
             table.close();
         }
     
     }

    2> 编写客户段代码

     package com.observer.test;
     
     import java.io.IOException;
     
     import org.apache.hadoop.conf.Configuration;
     import org.apache.hadoop.hbase.HBaseConfiguration;
     import org.apache.hadoop.hbase.TableName;
     import org.apache.hadoop.hbase.client.Connection;
     import org.apache.hadoop.hbase.client.ConnectionFactory;
     import org.apache.hadoop.hbase.client.HTable;
     import org.apache.hadoop.hbase.client.Put;
     import org.apache.hadoop.hbase.util.Bytes;
     
     public class DataClient {
     
         public static void main(String[] args) throws IOException {
             //配置
             Configuration conf = HBaseConfiguration.create();
             conf.set("hbase.zookeeper.quorum", "master,data1,data2");
             conf.set("hbase.zookeeper.property.clientPort", "2181");
             //连接
             Connection conn = ConnectionFactory.createConnection(conf);
             HTable table = (HTable) conn.getTable(TableName.valueOf("initialtable"));
             // 写入数据
             Put put = new Put(Bytes.toBytes("row01"));
             put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("E"));
             put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("value"), Bytes.toBytes("151"));
             table.put(put);
             // 关闭资源
             table.close();
             conn.close();  
     
         }
     
     }

    3> 创建需要的表

    hbase协处理器编码实例

    4> 加载协处理器
    将服务端代码打包上传集群服务器的hdfs上

    chown hadoop:hadoop datacode.jar
    chmod g+w  datacode.jar
    hadoop dfs -put datacode.jar /input/
    

     之后,将协处理器加载到初始表中

    disable 'initialtable'
    alter'initialtable',METHOD =>'table_att','coprocessor' =>'/input/datacode.jar|com.observer.test.TestObserver|100'
    enable 'initialtable'

    5> 执行客户端代码,显示结果hbase协处理器编码实例文章来源地址https://www.toymoban.com/news/detail-501138.html

到了这里,关于hbase协处理器编码实例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring MVC异常处理【单个控制异常处理器、全局异常处理器、自定义异常处理器】

    目录 一、单个控制器异常处理 1.1 控制器方法 1.2 编写出错页面 1.3 测试结果 二、全局异常处理 2.1 一个有异常的控制器类 2.2 全局异常处理器类 2.3 测试结果  三、自定义异常处理器 3.1 自定义异常处理器 3.2 测试结果 往期专栏文章相关导读  1. Maven系列专栏文章 2. Mybatis系列

    2024年02月16日
    浏览(43)
  • Jmeter前置处理器和后置处理器

    1. 后置处理器(Post Processor) 本质上是⼀种对sampler发出请求后接受到的响应数据进⾏处理 (后处理)的⽅法  正则表达式后置处理器 (1)引⽤名称:下⼀个请求要引⽤的参数名称,如填写title,则可⽤${title}引⽤它 (2)正则表达式: ():括起来的部分就是要提取的。 .:匹配

    2023年04月21日
    浏览(42)
  • DP读书:鲲鹏处理器 架构与编程(八)3.1鲲鹏处理器片上系统与Taishan处理器内核架构

    处理器体系结构,是一个偏底层的内容,但这是任一计算机系统的底层。 系统的性能、生态和功能很大程度上都依赖于计算机系统底层——处理器体系结构。任何一个系统程序员、固件设计者、应用程序员 甚至 服务器管理员,如果想要充分利用现代高性能处理器的硬件性能

    2024年02月12日
    浏览(53)
  • SkyEye处理器仿真系列:龙芯2K1000处理器

    天目全数字实时仿真软件SkyEye作为基于可视化建模的硬件行为级仿真平台,能够为嵌入式软件提供虚拟化运行环境,开发、测试人员可在该虚拟运行环境上进行软件开发、软件测试和软件验证活动。小到芯片,大到系统,SkyEye均可进行模拟。 1936年,被誉为“计算机科学与人

    2024年02月12日
    浏览(58)
  • DP读书:鲲鹏处理器 架构与编程(九)鲲鹏920处理器片上系统

    停更了两天,我做了一个本专业相关的孤岛问题的论文复现,可并没有什么太大进展,就像当初最开始跑Aspen一样,我要面对的是一个相当复杂的多参系统,这种情况下只能啃着技术文档一步一步的去调。 再次返回我的鲲鹏920处理器,无疑是舒服的所以我只能尽我所能的在做

    2024年02月12日
    浏览(50)
  • 第三十二章 开发Productions - ObjectScript Productions - 定义警报处理器 - 使用路由警报处理器

    如果需要通过多种输出机制联系用户,警报处理器应该是一个业务流程,用于确定如何在消息中路由 Ens.AlertReques 。在这种情况下, Productions 必须为每个输出机制包含一个额外的业务操作,并且警报处理器将消息转发到这些业务操作。 要将警报处理器定义为路由流程,请创建

    2024年02月08日
    浏览(46)
  • 全局异常处理器

    前言:由于 Controller 调用 Services ,最后调用 Mapper 来操作数据库,若 Mapper 操作数据库出问题了,此时页面报错会按照调用的原路径层层上报,最后未经处理的异常会上报至框架,最后服务器会向前端返回一个 JSON 的报错数据,而前端接收的是对 Result 封装过的 data 对象中的

    2024年02月11日
    浏览(47)
  • 【并行计算】多核处理器

    这张图连接了几个并行计算的思想。 从上往下。 1.两个fetch/decode部件,是 superscalar 技术,每个cycle可以发射多个指令。 2.多个执行单元,支持乱序执行,是ILP, 指令级并行 。 3.每个执行单元里还支持 SIMD 操作。 4.有多个execution context,就相当于是有多套线程的状态,类似寄

    2024年02月05日
    浏览(40)
  • ARM处理器概述

    RISC处理器和CISC处理器 首先了解一下两种处理器名字: RISC(Reduced Instruction Set Computer): 精简指令集 处理器 与之相对应的是: CISC(Complex Instruction Set Computer): 复杂指令集 处理器 顾名思义,RISC比CISC更加简单,那么对于处理器来说什么能称为简单呢?可以联想为生活中可

    2024年02月13日
    浏览(49)
  • 处理器(计组课程)

    31~26 25~21 20~16 15~11 ---    (从流水线寄存器通过旁路传回数据 也叫作  转发 ) 若产生冒险,则更前面的指令中 需要写入的目的寄存器刚好是 当前指令需要读取的源寄存器,此时对于当前源寄存器而言,这个寄存器内部的数据并不是准确的,因为它需要用到前一个指令产

    2024年02月07日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包