2021-05-12 14:32:11
HBase協處理器編碼範例
Observer協處理器通常在一個特定的事件(諸如Get
或Put
)之前或之後發生,相當於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,也可以自己編譯生成,參見 CentOS 7下protobuf的原始碼編譯安裝 見 http://www.linuxidc.com/Linux/2016-12/138716.htm。
注意,編譯的版本要與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")的名稱要一致。
// 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 = {
"nrsumcode.proto",nnSumRequest 22 16n 06family 30" +
" 01 02(t 22 16n 06column 30 02 02(t" 35n 13SumResponse 22 16n 03" +
"sum 30 01 02( 03: 01021nnSumService 22#n 06getSum 22 13.S" +
"umRequest 32 14.SumResponseB n 21com.endpoint." +
"testB 03SumH 01210 01 01240 01 01"
};
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 nullbinwinutils.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表
要將協處理器載入到這個表上
disable 'etable'
# 包名.類名|權重 com.endpoint.test.SumEndPoint|100
alter'etable',METHOD =>'table_att','coprocessor' =>'/input/sumcode.jar|com.endpoint.test.SumEndPoint|100' enable 'etable'
包名.類名|權重 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,從而導致叢集中的這張表無法使用,甚至會影響到其他表。
在windows中的用戶端執行用戶端的程式碼,結果如下:
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> 建立需要的表
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> 執行用戶端程式碼,顯示結果
Hadoop+HBase搭建雲端儲存總結 PDF http://www.linuxidc.com/Linux/2013-05/83844.htm
Ubuntu Server 14.04 下 Hbase資料庫安裝 http://www.linuxidc.com/Linux/2016-05/131499.htm
HBase 結點之間時間不一致造成regionserver啟動失敗 http://www.linuxidc.com/Linux/2013-06/86655.htm
CentOS 6.3下HBase偽分散式平台搭建 http://www.linuxidc.com/Linux/2016-11/136830.htm
Hadoop叢集安裝&HBase實驗環境搭建 http://www.linuxidc.com/Linux/2013-04/83560.htm
基於Hadoop叢集的HBase叢集的設定 http://www.linuxidc.com/Linux/2013-03/80815.htm‘
Hadoop安裝部署筆記之-HBase完全分布模式安裝 http://www.linuxidc.com/Linux/2012-12/76947.htm
CentOS 6.4下HBase叢集安裝 http://www.linuxidc.com/Linux/2016-11/137303.htm
相關文章