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,也可以自己编译生成,参见protobuf的编译安装
注意,编译的版本要与hadoop以及hbase使用的版本相同,或者略高,但最好不要过高,hadoop2.6.5 hbase1.2.4使用的都是protobuf2.5.0的版本,写此篇文章时的最新版为3.1.0
(高版本必须指定syntax,例如proto3的syntax在第一行非空白非注释行,必须写:syntax = "proto3",字段规则移除了 “required”,并把 “optional” 改名为 “singular”,移除了 default 选项。可搜索Protobuf 的 proto3 与 proto2 的区别进行了解。)下载的话选择带win或linux的版本,这是编译好的版本。有很多带具体语言的版本,是一些具体某种语言的发行版源码包。为了与hbase以及hadoop统一起来,此处用的是protoc-2.5.0-win32.zip。
解压文件:
使用windows命令行进入上面的目录,执行以下命令即可:
protoc.exe sum1.proto --java_out=./
高版本有编译好的适用于linux下的protoc程序文件,低版本没有。在linux下执行以下命令:
protoc sum.proto --java_out=./
结果都一样,生成的代码参见折叠部分,有很多,因为上面文件中指定java_outer_classname = "Sum",所以会生成Sum类,将这个类引入到项目中,注意项目的包名称与上面文件中指定(option java_package = "com.endpoint.test")的名称要一致。
1 // Generated by the protocol buffer compiler. DO NOT EDIT! 2 // source: sumcode.proto 3 4 package com.endpoint.test; 5 6 public final class Sum { 7 private Sum() {} 8 public static void registerAllExtensions( 9 com.google.protobuf.ExtensionRegistry registry) { 10 } 11 public interface SumRequestOrBuilder 12 extends com.google.protobuf.MessageOrBuilder { 13 14 // required string family = 1; 15 /** 16 * <code>required string family = 1;</code> 17 */ 18 boolean hasFamily(); 19 /** 20 * <code>required string family = 1;</code> 21 */ 22 java.lang.String getFamily(); 23 /** 24 * <code>required string family = 1;</code> 25 */ 26 com.google.protobuf.ByteString 27 getFamilyBytes(); 28 29 // required string column = 2; 30 /** 31 * <code>required string column = 2;</code> 32 */ 33 boolean hasColumn(); 34 /** 35 * <code>required string column = 2;</code> 36 */ 37 java.lang.String getColumn(); 38 /** 39 * <code>required string column = 2;</code> 40 */ 41 com.google.protobuf.ByteString 42 getColumnBytes(); 43 } 44 /** 45 * Protobuf type {@code SumRequest} 46 */ 47 public static final class SumRequest extends 48 com.google.protobuf.GeneratedMessage 49 implements SumRequestOrBuilder { 50 // Use SumRequest.newBuilder() to construct. 51 private SumRequest(com.google.protobuf.GeneratedMessage.Builder<?> builder) { 52 super(builder); 53 this.unknownFields = builder.getUnknownFields(); 54 } 55 private SumRequest(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } 56 57 private static final SumRequest defaultInstance; 58 public static SumRequest getDefaultInstance() { 59 return defaultInstance; 60 } 61 62 public SumRequest getDefaultInstanceForType() { 63 return defaultInstance; 64 } 65 66 private final com.google.protobuf.UnknownFieldSet unknownFields; 67 @java.lang.Override 68 public final com.google.protobuf.UnknownFieldSet 69 getUnknownFields() { 70 return this.unknownFields; 71 } 72 private SumRequest( 73 com.google.protobuf.CodedInputStream input, 74 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 75 throws com.google.protobuf.InvalidProtocolBufferException { 76 initFields(); 77 int mutable_bitField0_ = 0; 78 com.google.protobuf.UnknownFieldSet.Builder unknownFields = 79 com.google.protobuf.UnknownFieldSet.newBuilder(); 80 try { 81 boolean done = false; 82 while (!done) { 83 int tag = input.readTag(); 84 switch (tag) { 85 case 0: 86 done = true; 87 break; 88 default: { 89 if (!parseUnknownField(input, unknownFields, 90 extensionRegistry, tag)) { 91 done = true; 92 } 93 break; 94 } 95 case 10: { 96 bitField0_ |= 0x00000001; 97 family_ = input.readBytes(); 98 break; 99 } 100 case 18: { 101 bitField0_ |= 0x00000002; 102 column_ = input.readBytes(); 103 break; 104 } 105 } 106 } 107 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 108 throw e.setUnfinishedMessage(this); 109 } catch (java.io.IOException e) { 110 throw new com.google.protobuf.InvalidProtocolBufferException( 111 e.getMessage()).setUnfinishedMessage(this); 112 } finally { 113 this.unknownFields = unknownFields.build(); 114 makeExtensionsImmutable(); 115 } 116 } 117 public static final com.google.protobuf.Descriptors.Descriptor 118 getDescriptor() { 119 return com.endpoint.test.Sum.internal_static_SumRequest_descriptor; 120 } 121 122 protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 123 internalGetFieldAccessorTable() { 124 return com.endpoint.test.Sum.internal_static_SumRequest_fieldAccessorTable 125 .ensureFieldAccessorsInitialized( 126 com.endpoint.test.Sum.SumRequest.class, com.endpoint.test.Sum.SumRequest.Builder.class); 127 } 128 129 public static com.google.protobuf.Parser<SumRequest> PARSER = 130 new com.google.protobuf.AbstractParser<SumRequest>() { 131 public SumRequest parsePartialFrom( 132 com.google.protobuf.CodedInputStream input, 133 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 134 throws com.google.protobuf.InvalidProtocolBufferException { 135 return new SumRequest(input, extensionRegistry); 136 } 137 }; 138 139 @java.lang.Override 140 public com.google.protobuf.Parser<SumRequest> getParserForType() { 141 return PARSER; 142 } 143 144 private int bitField0_; 145 // required string family = 1; 146 public static final int FAMILY_FIELD_NUMBER = 1; 147 private java.lang.Object family_; 148 /** 149 * <code>required string family = 1;</code> 150 */ 151 public boolean hasFamily() { 152 return ((bitField0_ & 0x00000001) == 0x00000001); 153 } 154 /** 155 * <code>required string family = 1;</code> 156 */ 157 public java.lang.String getFamily() { 158 java.lang.Object ref = family_; 159 if (ref instanceof java.lang.String) { 160 return (java.lang.String) ref; 161 } else { 162 com.google.protobuf.ByteString bs = 163 (com.google.protobuf.ByteString) ref; 164 java.lang.String s = bs.toStringUtf8(); 165 if (bs.isValidUtf8()) { 166 family_ = s; 167 } 168 return s; 169 } 170 } 171 /** 172 * <code>required string family = 1;</code> 173 */ 174 public com.google.protobuf.ByteString 175 getFamilyBytes() { 176 java.lang.Object ref = family_; 177 if (ref instanceof java.lang.String) { 178 com.google.protobuf.ByteString b = 179 com.google.protobuf.ByteString.copyFromUtf8( 180 (java.lang.String) ref); 181 family_ = b; 182 return b; 183 } else { 184 return (com.google.protobuf.ByteString) ref; 185 } 186 } 187 188 // required string column = 2; 189 public static final int COLUMN_FIELD_NUMBER = 2; 190 private java.lang.Object column_; 191 /** 192 * <code>required string column = 2;</code> 193 */ 194 public boolean hasColumn() { 195 return ((bitField0_ & 0x00000002) == 0x00000002); 196 } 197 /** 198 * <code>required string column = 2;</code> 199 */ 200 public java.lang.String getColumn() { 201 java.lang.Object ref = column_; 202 if (ref instanceof java.lang.String) { 203 return (java.lang.String) ref; 204 } else { 205 com.google.protobuf.ByteString bs = 206 (com.google.protobuf.ByteString) ref; 207 java.lang.String s = bs.toStringUtf8(); 208 if (bs.isValidUtf8()) { 209 column_ = s; 210 } 211 return s; 212 } 213 } 214 /** 215 * <code>required string column = 2;</code> 216 */ 217 public com.google.protobuf.ByteString 218 getColumnBytes() { 219 java.lang.Object ref = column_; 220 if (ref instanceof java.lang.String) { 221 com.google.protobuf.ByteString b = 222 com.google.protobuf.ByteString.copyFromUtf8( 223 (java.lang.String) ref); 224 column_ = b; 225 return b; 226 } else { 227 return (com.google.protobuf.ByteString) ref; 228 } 229 } 230 231 private void initFields() { 232 family_ = ""; 233 column_ = ""; 234 } 235 private byte memoizedIsInitialized = -1; 236 public final boolean isInitialized() { 237 byte isInitialized = memoizedIsInitialized; 238 if (isInitialized != -1) return isInitialized == 1; 239 240 if (!hasFamily()) { 241 memoizedIsInitialized = 0; 242 return false; 243 } 244 if (!hasColumn()) { 245 memoizedIsInitialized = 0; 246 return false; 247 } 248 memoizedIsInitialized = 1; 249 return true; 250 } 251 252 public void writeTo(com.google.protobuf.CodedOutputStream output) 253 throws java.io.IOException { 254 getSerializedSize(); 255 if (((bitField0_ & 0x00000001) == 0x00000001)) { 256 output.writeBytes(1, getFamilyBytes()); 257 } 258 if (((bitField0_ & 0x00000002) == 0x00000002)) { 259 output.writeBytes(2, getColumnBytes()); 260 } 261 getUnknownFields().writeTo(output); 262 } 263 264 private int memoizedSerializedSize = -1; 265 public int getSerializedSize() { 266 int size = memoizedSerializedSize; 267 if (size != -1) return size; 268 269 size = 0; 270 if (((bitField0_ & 0x00000001) == 0x00000001)) { 271 size += com.google.protobuf.CodedOutputStream 272 .computeBytesSize(1, getFamilyBytes()); 273 } 274 if (((bitField0_ & 0x00000002) == 0x00000002)) { 275 size += com.google.protobuf.CodedOutputStream 276 .computeBytesSize(2, getColumnBytes()); 277 } 278 size += getUnknownFields().getSerializedSize(); 279 memoizedSerializedSize = size; 280 return size; 281 } 282 283 private static final long serialVersionUID = 0L; 284 @java.lang.Override 285 protected java.lang.Object writeReplace() 286 throws java.io.ObjectStreamException { 287 return super.writeReplace(); 288 } 289 290 @java.lang.Override 291 public boolean equals(final java.lang.Object obj) { 292 if (obj == this) { 293 return true; 294 } 295 if (!(obj instanceof com.endpoint.test.Sum.SumRequest)) { 296 return super.equals(obj); 297 } 298 com.endpoint.test.Sum.SumRequest other = (com.endpoint.test.Sum.SumRequest) obj; 299 300 boolean result = true; 301 result = result && (hasFamily() == other.hasFamily()); 302 if (hasFamily()) { 303 result = result && getFamily() 304 .equals(other.getFamily()); 305 } 306 result = result && (hasColumn() == other.hasColumn()); 307 if (hasColumn()) { 308 result = result && getColumn() 309 .equals(other.getColumn()); 310 } 311 result = result && 312 getUnknownFields().equals(other.getUnknownFields()); 313 return result; 314 } 315 316 private int memoizedHashCode = 0; 317 @java.lang.Override 318 public int hashCode() { 319 if (memoizedHashCode != 0) { 320 return memoizedHashCode; 321 } 322 int hash = 41; 323 hash = (19 * hash) + getDescriptorForType().hashCode(); 324 if (hasFamily()) { 325 hash = (37 * hash) + FAMILY_FIELD_NUMBER; 326 hash = (53 * hash) + getFamily().hashCode(); 327 } 328 if (hasColumn()) { 329 hash = (37 * hash) + COLUMN_FIELD_NUMBER; 330 hash = (53 * hash) + getColumn().hashCode(); 331 } 332 hash = (29 * hash) + getUnknownFields().hashCode(); 333 memoizedHashCode = hash; 334 return hash; 335 } 336 337 public static com.endpoint.test.Sum.SumRequest parseFrom( 338 com.google.protobuf.ByteString data) 339 throws com.google.protobuf.InvalidProtocolBufferException { 340 return PARSER.parseFrom(data); 341 } 342 public static com.endpoint.test.Sum.SumRequest parseFrom( 343 com.google.protobuf.ByteString data, 344 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 345 throws com.google.protobuf.InvalidProtocolBufferException { 346 return PARSER.parseFrom(data, extensionRegistry); 347 } 348 public static com.endpoint.test.Sum.SumRequest parseFrom(byte[] data) 349 throws com.google.protobuf.InvalidProtocolBufferException { 350 return PARSER.parseFrom(data); 351 } 352 public static com.endpoint.test.Sum.SumRequest parseFrom( 353 byte[] data, 354 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 355 throws com.google.protobuf.InvalidProtocolBufferException { 356 return PARSER.parseFrom(data, extensionRegistry); 357 } 358 public static com.endpoint.test.Sum.SumRequest parseFrom(java.io.InputStream input) 359 throws java.io.IOException { 360 return PARSER.parseFrom(input); 361 } 362 public static com.endpoint.test.Sum.SumRequest parseFrom( 363 java.io.InputStream input, 364 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 365 throws java.io.IOException { 366 return PARSER.parseFrom(input, extensionRegistry); 367 } 368 public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom(java.io.InputStream input) 369 throws java.io.IOException { 370 return PARSER.parseDelimitedFrom(input); 371 } 372 public static com.endpoint.test.Sum.SumRequest parseDelimitedFrom( 373 java.io.InputStream input, 374 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 375 throws java.io.IOException { 376 return PARSER.parseDelimitedFrom(input, extensionRegistry); 377 } 378 public static com.endpoint.test.Sum.SumRequest parseFrom( 379 com.google.protobuf.CodedInputStream input) 380 throws java.io.IOException { 381 return PARSER.parseFrom(input); 382 } 383 public static com.endpoint.test.Sum.SumRequest parseFrom( 384 com.google.protobuf.CodedInputStream input, 385 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 386 throws java.io.IOException { 387 return PARSER.parseFrom(input, extensionRegistry); 388 } 389 390 public static Builder newBuilder() { return Builder.create(); } 391 public Builder newBuilderForType() { return newBuilder(); } 392 public static Builder newBuilder(com.endpoint.test.Sum.SumRequest prototype) { 393 return newBuilder().mergeFrom(prototype); 394 } 395 public Builder toBuilder() { return newBuilder(this); } 396 397 @java.lang.Override 398 protected Builder newBuilderForType( 399 com.google.protobuf.GeneratedMessage.BuilderParent parent) { 400 Builder builder = new Builder(parent); 401 return builder; 402 } 403 /** 404 * Protobuf type {@code SumRequest} 405 */ 406 public static final class Builder extends 407 com.google.protobuf.GeneratedMessage.Builder<Builder> 408 implements com.endpoint.test.Sum.SumRequestOrBuilder { 409 public static final com.google.protobuf.Descriptors.Descriptor 410 getDescriptor() { 411 return com.endpoint.test.Sum.internal_static_SumRequest_descriptor; 412 } 413 414 protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 415 internalGetFieldAccessorTable() { 416 return com.endpoint.test.Sum.internal_static_SumRequest_fieldAccessorTable 417 .ensureFieldAccessorsInitialized( 418 com.endpoint.test.Sum.SumRequest.class, com.endpoint.test.Sum.SumRequest.Builder.class); 419 } 420 421 // Construct using com.endpoint.test.Sum.SumRequest.newBuilder() 422 private Builder() { 423 maybeForceBuilderInitialization(); 424 } 425 426 private Builder( 427 com.google.protobuf.GeneratedMessage.BuilderParent parent) { 428 super(parent); 429 maybeForceBuilderInitialization(); 430 } 431 private void maybeForceBuilderInitialization() { 432 if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { 433 } 434 } 435 private static Builder create() { 436 return new Builder(); 437 } 438 439 public Builder clear() { 440 super.clear(); 441 family_ = ""; 442 bitField0_ = (bitField0_ & ~0x00000001); 443 column_ = ""; 444 bitField0_ = (bitField0_ & ~0x00000002); 445 return this; 446 } 447 448 public Builder clone() { 449 return create().mergeFrom(buildPartial()); 450 } 451 452 public com.google.protobuf.Descriptors.Descriptor 453 getDescriptorForType() { 454 return com.endpoint.test.Sum.internal_static_SumRequest_descriptor; 455 } 456 457 public com.endpoint.test.Sum.SumRequest getDefaultInstanceForType() { 458 return com.endpoint.test.Sum.SumRequest.getDefaultInstance(); 459 } 460 461 public com.endpoint.test.Sum.SumRequest build() { 462 com.endpoint.test.Sum.SumRequest result = buildPartial(); 463 if (!result.isInitialized()) { 464 throw newUninitializedMessageException(result); 465 } 466 return result; 467 } 468 469 public com.endpoint.test.Sum.SumRequest buildPartial() { 470 com.endpoint.test.Sum.SumRequest result = new com.endpoint.test.Sum.SumRequest(this); 471 int from_bitField0_ = bitField0_; 472 int to_bitField0_ = 0; 473 if (((from_bitField0_ & 0x00000001) == 0x00000001)) { 474 to_bitField0_ |= 0x00000001; 475 } 476 result.family_ = family_; 477 if (((from_bitField0_ & 0x00000002) == 0x00000002)) { 478 to_bitField0_ |= 0x00000002; 479 } 480 result.column_ = column_; 481 result.bitField0_ = to_bitField0_; 482 onBuilt(); 483 return result; 484 } 485 486 public Builder mergeFrom(com.google.protobuf.Message other) { 487 if (other instanceof com.endpoint.test.Sum.SumRequest) { 488 return mergeFrom((com.endpoint.test.Sum.SumRequest)other); 489 } else { 490 super.mergeFrom(other); 491 return this; 492 } 493 } 494 495 public Builder mergeFrom(com.endpoint.test.Sum.SumRequest other) { 496 if (other == com.endpoint.test.Sum.SumRequest.getDefaultInstance()) return this; 497 if (other.hasFamily()) { 498 bitField0_ |= 0x00000001; 499 family_ = other.family_; 500 onChanged(); 501 } 502 if (other.hasColumn()) { 503 bitField0_ |= 0x00000002; 504 column_ = other.column_; 505 onChanged(); 506 } 507 this.mergeUnknownFields(other.getUnknownFields()); 508 return this; 509 } 510 511 public final boolean isInitialized() { 512 if (!hasFamily()) { 513 514 return false; 515 } 516 if (!hasColumn()) { 517 518 return false; 519 } 520 return true; 521 } 522 523 public Builder mergeFrom( 524 com.google.protobuf.CodedInputStream input, 525 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 526 throws java.io.IOException { 527 com.endpoint.test.Sum.SumRequest parsedMessage = null; 528 try { 529 parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 530 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 531 parsedMessage = (com.endpoint.test.Sum.SumRequest) e.getUnfinishedMessage(); 532 throw e; 533 } finally { 534 if (parsedMessage != null) { 535 mergeFrom(parsedMessage); 536 } 537 } 538 return this; 539 } 540 private int bitField0_; 541 542 // required string family = 1; 543 private java.lang.Object family_ = ""; 544 /** 545 * <code>required string family = 1;</code> 546 */ 547 public boolean hasFamily() { 548 return ((bitField0_ & 0x00000001) == 0x00000001); 549 } 550 /** 551 * <code>required string family = 1;</code> 552 */ 553 public java.lang.String getFamily() { 554 java.lang.Object ref = family_; 555 if (!(ref instanceof java.lang.String)) { 556 java.lang.String s = ((com.google.protobuf.ByteString) ref) 557 .toStringUtf8(); 558 family_ = s; 559 return s; 560 } else { 561 return (java.lang.String) ref; 562 } 563 } 564 /** 565 * <code>required string family = 1;</code> 566 */ 567 public com.google.protobuf.ByteString 568 getFamilyBytes() { 569 java.lang.Object ref = family_; 570 if (ref instanceof String) { 571 com.google.protobuf.ByteString b = 572 com.google.protobuf.ByteString.copyFromUtf8( 573 (java.lang.String) ref); 574 family_ = b; 575 return b; 576 } else { 577 return (com.google.protobuf.ByteString) ref; 578 } 579 } 580 /** 581 * <code>required string family = 1;</code> 582 */ 583 public Builder setFamily( 584 java.lang.String value) { 585 if (value == null) { 586 throw new NullPointerException(); 587 } 588 bitField0_ |= 0x00000001; 589 family_ = value; 590 onChanged(); 591 return this; 592 } 593 /** 594 * <code>required string family = 1;</code> 595 */ 596 public Builder clearFamily() { 597 bitField0_ = (bitField0_ & ~0x00000001); 598 family_ = getDefaultInstance().getFamily(); 599 onChanged(); 600 return this; 601 } 602 /** 603 * <code>required string family = 1;</code> 604 */ 605 public Builder setFamilyBytes( 606 com.google.protobuf.ByteString value) { 607 if (value == null) { 608 throw new NullPointerException(); 609 } 610 bitField0_ |= 0x00000001; 611 family_ = value; 612 onChanged(); 613 return this; 614 } 615 616 // required string column = 2; 617 private java.lang.Object column_ = ""; 618 /** 619 * <code>required string column = 2;</code> 620 */ 621 public boolean hasColumn() { 622 return ((bitField0_ & 0x00000002) == 0x00000002); 623 } 624 /** 625 * <code>required string column = 2;</code> 626 */ 627 public java.lang.String getColumn() { 628 java.lang.Object ref = column_; 629 if (!(ref instanceof java.lang.String)) { 630 java.lang.String s = ((com.google.protobuf.ByteString) ref) 631 .toStringUtf8(); 632 column_ = s; 633 return s; 634 } else { 635 return (java.lang.String) ref; 636 } 637 } 638 /** 639 * <code>required string column = 2;</code> 640 */ 641 public com.google.protobuf.ByteString 642 getColumnBytes() { 643 java.lang.Object ref = column_; 644 if (ref instanceof String) { 645 com.google.protobuf.ByteString b = 646 com.google.protobuf.ByteString.copyFromUtf8( 647 (java.lang.String) ref); 648 column_ = b; 649 return b; 650 } else { 651 return (com.google.protobuf.ByteString) ref; 652 } 653 } 654 /** 655 * <code>required string column = 2;</code> 656 */ 657 public Builder setColumn( 658 java.lang.String value) { 659 if (value == null) { 660 throw new NullPointerException(); 661 } 662 bitField0_ |= 0x00000002; 663 column_ = value; 664 onChanged(); 665 return this; 666 } 667 /** 668 * <code>required string column = 2;</code> 669 */ 670 public Builder clearColumn() { 671 bitField0_ = (bitField0_ & ~0x00000002); 672 column_ = getDefaultInstance().getColumn(); 673 onChanged(); 674 return this; 675 } 676 /** 677 * <code>required string column = 2;</code> 678 */ 679 public Builder setColumnBytes( 680 com.google.protobuf.ByteString value) { 681 if (value == null) { 682 throw new NullPointerException(); 683 } 684 bitField0_ |= 0x00000002; 685 column_ = value; 686 onChanged(); 687 return this; 688 } 689 690 // @@protoc_insertion_point(builder_scope:SumRequest) 691 } 692 693 static { 694 defaultInstance = new SumRequest(true); 695 defaultInstance.initFields(); 696 } 697 698 // @@protoc_insertion_point(class_scope:SumRequest) 699 } 700 701 public interface SumResponseOrBuilder 702 extends com.google.protobuf.MessageOrBuilder { 703 704 // required int64 sum = 1 [default = 0]; 705 /** 706 * <code>required int64 sum = 1 [default = 0];</code> 707 */ 708 boolean hasSum(); 709 /** 710 * <code>required int64 sum = 1 [default = 0];</code> 711 */ 712 long getSum(); 713 } 714 /** 715 * Protobuf type {@code SumResponse} 716 */ 717 public static final class SumResponse extends 718 com.google.protobuf.GeneratedMessage 719 implements SumResponseOrBuilder { 720 // Use SumResponse.newBuilder() to construct. 721 private SumResponse(com.google.protobuf.GeneratedMessage.Builder<?> builder) { 722 super(builder); 723 this.unknownFields = builder.getUnknownFields(); 724 } 725 private SumResponse(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); } 726 727 private static final SumResponse defaultInstance; 728 public static SumResponse getDefaultInstance() { 729 return defaultInstance; 730 } 731 732 public SumResponse getDefaultInstanceForType() { 733 return defaultInstance; 734 } 735 736 private final com.google.protobuf.UnknownFieldSet unknownFields; 737 @java.lang.Override 738 public final com.google.protobuf.UnknownFieldSet 739 getUnknownFields() { 740 return this.unknownFields; 741 } 742 private SumResponse( 743 com.google.protobuf.CodedInputStream input, 744 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 745 throws com.google.protobuf.InvalidProtocolBufferException { 746 initFields(); 747 int mutable_bitField0_ = 0; 748 com.google.protobuf.UnknownFieldSet.Builder unknownFields = 749 com.google.protobuf.UnknownFieldSet.newBuilder(); 750 try { 751 boolean done = false; 752 while (!done) { 753 int tag = input.readTag(); 754 switch (tag) { 755 case 0: 756 done = true; 757 break; 758 default: { 759 if (!parseUnknownField(input, unknownFields, 760 extensionRegistry, tag)) { 761 done = true; 762 } 763 break; 764 } 765 case 8: { 766 bitField0_ |= 0x00000001; 767 sum_ = input.readInt64(); 768 break; 769 } 770 } 771 } 772 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 773 throw e.setUnfinishedMessage(this); 774 } catch (java.io.IOException e) { 775 throw new com.google.protobuf.InvalidProtocolBufferException( 776 e.getMessage()).setUnfinishedMessage(this); 777 } finally { 778 this.unknownFields = unknownFields.build(); 779 makeExtensionsImmutable(); 780 } 781 } 782 public static final com.google.protobuf.Descriptors.Descriptor 783 getDescriptor() { 784 return com.endpoint.test.Sum.internal_static_SumResponse_descriptor; 785 } 786 787 protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 788 internalGetFieldAccessorTable() { 789 return com.endpoint.test.Sum.internal_static_SumResponse_fieldAccessorTable 790 .ensureFieldAccessorsInitialized( 791 com.endpoint.test.Sum.SumResponse.class, com.endpoint.test.Sum.SumResponse.Builder.class); 792 } 793 794 public static com.google.protobuf.Parser<SumResponse> PARSER = 795 new com.google.protobuf.AbstractParser<SumResponse>() { 796 public SumResponse parsePartialFrom( 797 com.google.protobuf.CodedInputStream input, 798 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 799 throws com.google.protobuf.InvalidProtocolBufferException { 800 return new SumResponse(input, extensionRegistry); 801 } 802 }; 803 804 @java.lang.Override 805 public com.google.protobuf.Parser<SumResponse> getParserForType() { 806 return PARSER; 807 } 808 809 private int bitField0_; 810 // required int64 sum = 1 [default = 0]; 811 public static final int SUM_FIELD_NUMBER = 1; 812 private long sum_; 813 /** 814 * <code>required int64 sum = 1 [default = 0];</code> 815 */ 816 public boolean hasSum() { 817 return ((bitField0_ & 0x00000001) == 0x00000001); 818 } 819 /** 820 * <code>required int64 sum = 1 [default = 0];</code> 821 */ 822 public long getSum() { 823 return sum_; 824 } 825 826 private void initFields() { 827 sum_ = 0L; 828 } 829 private byte memoizedIsInitialized = -1; 830 public final boolean isInitialized() { 831 byte isInitialized = memoizedIsInitialized; 832 if (isInitialized != -1) return isInitialized == 1; 833 834 if (!hasSum()) { 835 memoizedIsInitialized = 0; 836 return false; 837 } 838 memoizedIsInitialized = 1; 839 return true; 840 } 841 842 public void writeTo(com.google.protobuf.CodedOutputStream output) 843 throws java.io.IOException { 844 getSerializedSize(); 845 if (((bitField0_ & 0x00000001) == 0x00000001)) { 846 output.writeInt64(1, sum_); 847 } 848 getUnknownFields().writeTo(output); 849 } 850 851 private int memoizedSerializedSize = -1; 852 public int getSerializedSize() { 853 int size = memoizedSerializedSize; 854 if (size != -1) return size; 855 856 size = 0; 857 if (((bitField0_ & 0x00000001) == 0x00000001)) { 858 size += com.google.protobuf.CodedOutputStream 859 .computeInt64Size(1, sum_); 860 } 861 size += getUnknownFields().getSerializedSize(); 862 memoizedSerializedSize = size; 863 return size; 864 } 865 866 private static final long serialVersionUID = 0L; 867 @java.lang.Override 868 protected java.lang.Object writeReplace() 869 throws java.io.ObjectStreamException { 870 return super.writeReplace(); 871 } 872 873 @java.lang.Override 874 public boolean equals(final java.lang.Object obj) { 875 if (obj == this) { 876 return true; 877 } 878 if (!(obj instanceof com.endpoint.test.Sum.SumResponse)) { 879 return super.equals(obj); 880 } 881 com.endpoint.test.Sum.SumResponse other = (com.endpoint.test.Sum.SumResponse) obj; 882 883 boolean result = true; 884 result = result && (hasSum() == other.hasSum()); 885 if (hasSum()) { 886 result = result && (getSum() 887 == other.getSum()); 888 } 889 result = result && 890 getUnknownFields().equals(other.getUnknownFields()); 891 return result; 892 } 893 894 private int memoizedHashCode = 0; 895 @java.lang.Override 896 public int hashCode() { 897 if (memoizedHashCode != 0) { 898 return memoizedHashCode; 899 } 900 int hash = 41; 901 hash = (19 * hash) + getDescriptorForType().hashCode(); 902 if (hasSum()) { 903 hash = (37 * hash) + SUM_FIELD_NUMBER; 904 hash = (53 * hash) + hashLong(getSum()); 905 } 906 hash = (29 * hash) + getUnknownFields().hashCode(); 907 memoizedHashCode = hash; 908 return hash; 909 } 910 911 public static com.endpoint.test.Sum.SumResponse parseFrom( 912 com.google.protobuf.ByteString data) 913 throws com.google.protobuf.InvalidProtocolBufferException { 914 return PARSER.parseFrom(data); 915 } 916 public static com.endpoint.test.Sum.SumResponse parseFrom( 917 com.google.protobuf.ByteString data, 918 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 919 throws com.google.protobuf.InvalidProtocolBufferException { 920 return PARSER.parseFrom(data, extensionRegistry); 921 } 922 public static com.endpoint.test.Sum.SumResponse parseFrom(byte[] data) 923 throws com.google.protobuf.InvalidProtocolBufferException { 924 return PARSER.parseFrom(data); 925 } 926 public static com.endpoint.test.Sum.SumResponse parseFrom( 927 byte[] data, 928 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 929 throws com.google.protobuf.InvalidProtocolBufferException { 930 return PARSER.parseFrom(data, extensionRegistry); 931 } 932 public static com.endpoint.test.Sum.SumResponse parseFrom(java.io.InputStream input) 933 throws java.io.IOException { 934 return PARSER.parseFrom(input); 935 } 936 public static com.endpoint.test.Sum.SumResponse parseFrom( 937 java.io.InputStream input, 938 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 939 throws java.io.IOException { 940 return PARSER.parseFrom(input, extensionRegistry); 941 } 942 public static com.endpoint.test.Sum.SumResponse parseDelimitedFrom(java.io.InputStream input) 943 throws java.io.IOException { 944 return PARSER.parseDelimitedFrom(input); 945 } 946 public static com.endpoint.test.Sum.SumResponse parseDelimitedFrom( 947 java.io.InputStream input, 948 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 949 throws java.io.IOException { 950 return PARSER.parseDelimitedFrom(input, extensionRegistry); 951 } 952 public static com.endpoint.test.Sum.SumResponse parseFrom( 953 com.google.protobuf.CodedInputStream input) 954 throws java.io.IOException { 955 return PARSER.parseFrom(input); 956 } 957 public static com.endpoint.test.Sum.SumResponse parseFrom( 958 com.google.protobuf.CodedInputStream input, 959 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 960 throws java.io.IOException { 961 return PARSER.parseFrom(input, extensionRegistry); 962 } 963 964 public static Builder newBuilder() { return Builder.create(); } 965 public Builder newBuilderForType() { return newBuilder(); } 966 public static Builder newBuilder(com.endpoint.test.Sum.SumResponse prototype) { 967 return newBuilder().mergeFrom(prototype); 968 } 969 public Builder toBuilder() { return newBuilder(this); } 970 971 @java.lang.Override 972 protected Builder newBuilderForType( 973 com.google.protobuf.GeneratedMessage.BuilderParent parent) { 974 Builder builder = new Builder(parent); 975 return builder; 976 } 977 /** 978 * Protobuf type {@code SumResponse} 979 */ 980 public static final class Builder extends 981 com.google.protobuf.GeneratedMessage.Builder<Builder> 982 implements com.endpoint.test.Sum.SumResponseOrBuilder { 983 public static final com.google.protobuf.Descriptors.Descriptor 984 getDescriptor() { 985 return com.endpoint.test.Sum.internal_static_SumResponse_descriptor; 986 } 987 988 protected com.google.protobuf.GeneratedMessage.FieldAccessorTable 989 internalGetFieldAccessorTable() { 990 return com.endpoint.test.Sum.internal_static_SumResponse_fieldAccessorTable 991 .ensureFieldAccessorsInitialized( 992 com.endpoint.test.Sum.SumResponse.class, com.endpoint.test.Sum.SumResponse.Builder.class); 993 } 994 995 // Construct using com.endpoint.test.Sum.SumResponse.newBuilder() 996 private Builder() { 997 maybeForceBuilderInitialization(); 998 } 999 1000 private Builder( 1001 com.google.protobuf.GeneratedMessage.BuilderParent parent) { 1002 super(parent); 1003 maybeForceBuilderInitialization(); 1004 } 1005 private void maybeForceBuilderInitialization() { 1006 if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { 1007 } 1008 } 1009 private static Builder create() { 1010 return new Builder(); 1011 } 1012 1013 public Builder clear() { 1014 super.clear(); 1015 sum_ = 0L; 1016 bitField0_ = (bitField0_ & ~0x00000001); 1017 return this; 1018 } 1019 1020 public Builder clone() { 1021 return create().mergeFrom(buildPartial()); 1022 } 1023 1024 public com.google.protobuf.Descriptors.Descriptor 1025 getDescriptorForType() { 1026 return com.endpoint.test.Sum.internal_static_SumResponse_descriptor; 1027 } 1028 1029 public com.endpoint.test.Sum.SumResponse getDefaultInstanceForType() { 1030 return com.endpoint.test.Sum.SumResponse.getDefaultInstance(); 1031 } 1032 1033 public com.endpoint.test.Sum.SumResponse build() { 1034 com.endpoint.test.Sum.SumResponse result = buildPartial(); 1035 if (!result.isInitialized()) { 1036 throw newUninitializedMessageException(result); 1037 } 1038 return result; 1039 } 1040 1041 public com.endpoint.test.Sum.SumResponse buildPartial() { 1042 com.endpoint.test.Sum.SumResponse result = new com.endpoint.test.Sum.SumResponse(this); 1043 int from_bitField0_ = bitField0_; 1044 int to_bitField0_ = 0; 1045 if (((from_bitField0_ & 0x00000001) == 0x00000001)) { 1046 to_bitField0_ |= 0x00000001; 1047 } 1048 result.sum_ = sum_; 1049 result.bitField0_ = to_bitField0_; 1050 onBuilt(); 1051 return result; 1052 } 1053 1054 public Builder mergeFrom(com.google.protobuf.Message other) { 1055 if (other instanceof com.endpoint.test.Sum.SumResponse) { 1056 return mergeFrom((com.endpoint.test.Sum.SumResponse)other); 1057 } else { 1058 super.mergeFrom(other); 1059 return this; 1060 } 1061 } 1062 1063 public Builder mergeFrom(com.endpoint.test.Sum.SumResponse other) { 1064 if (other == com.endpoint.test.Sum.SumResponse.getDefaultInstance()) return this; 1065 if (other.hasSum()) { 1066 setSum(other.getSum()); 1067 } 1068 this.mergeUnknownFields(other.getUnknownFields()); 1069 return this; 1070 } 1071 1072 public final boolean isInitialized() { 1073 if (!hasSum()) { 1074 1075 return false; 1076 } 1077 return true; 1078 } 1079 1080 public Builder mergeFrom( 1081 com.google.protobuf.CodedInputStream input, 1082 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 1083 throws java.io.IOException { 1084 com.endpoint.test.Sum.SumResponse parsedMessage = null; 1085 try { 1086 parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 1087 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 1088 parsedMessage = (com.endpoint.test.Sum.SumResponse) e.getUnfinishedMessage(); 1089 throw e; 1090 } finally { 1091 if (parsedMessage != null) { 1092 mergeFrom(parsedMessage); 1093 } 1094 } 1095 return this; 1096 } 1097 private int bitField0_; 1098 1099 // required int64 sum = 1 [default = 0]; 1100 private long sum_ ; 1101 /** 1102 * <code>required int64 sum = 1 [default = 0];</code> 1103 */ 1104 public boolean hasSum() { 1105 return ((bitField0_ & 0x00000001) == 0x00000001); 1106 } 1107 /** 1108 * <code>required int64 sum = 1 [default = 0];</code> 1109 */ 1110 public long getSum() { 1111 return sum_; 1112 } 1113 /** 1114 * <code>required int64 sum = 1 [default = 0];</code> 1115 */ 1116 public Builder setSum(long value) { 1117 bitField0_ |= 0x00000001; 1118 sum_ = value; 1119 onChanged(); 1120 return this; 1121 } 1122 /** 1123 * <code>required int64 sum = 1 [default = 0];</code> 1124 */ 1125 public Builder clearSum() { 1126 bitField0_ = (bitField0_ & ~0x00000001); 1127 sum_ = 0L; 1128 onChanged(); 1129 return this; 1130 } 1131 1132 // @@protoc_insertion_point(builder_scope:SumResponse) 1133 } 1134 1135 static { 1136 defaultInstance = new SumResponse(true); 1137 defaultInstance.initFields(); 1138 } 1139 1140 // @@protoc_insertion_point(class_scope:SumResponse) 1141 } 1142 1143 /** 1144 * Protobuf service {@code SumService} 1145 */ 1146 public static abstract class SumService 1147 implements com.google.protobuf.Service { 1148 protected SumService() {} 1149 1150 public interface Interface { 1151 /** 1152 * <code>rpc getSum(.SumRequest) returns (.SumResponse);</code> 1153 */ 1154 public abstract void getSum( 1155 com.google.protobuf.RpcController controller, 1156 com.endpoint.test.Sum.SumRequest request, 1157 com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done); 1158 1159 } 1160 1161 public static com.google.protobuf.Service newReflectiveService( 1162 final Interface impl) { 1163 return new SumService() { 1164 @java.lang.Override 1165 public void getSum( 1166 com.google.protobuf.RpcController controller, 1167 com.endpoint.test.Sum.SumRequest request, 1168 com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done) { 1169 impl.getSum(controller, request, done); 1170 } 1171 1172 }; 1173 } 1174 1175 public static com.google.protobuf.BlockingService 1176 newReflectiveBlockingService(final BlockingInterface impl) { 1177 return new com.google.protobuf.BlockingService() { 1178 public final com.google.protobuf.Descriptors.ServiceDescriptor 1179 getDescriptorForType() { 1180 return getDescriptor(); 1181 } 1182 1183 public final com.google.protobuf.Message callBlockingMethod( 1184 com.google.protobuf.Descriptors.MethodDescriptor method, 1185 com.google.protobuf.RpcController controller, 1186 com.google.protobuf.Message request) 1187 throws com.google.protobuf.ServiceException { 1188 if (method.getService() != getDescriptor()) { 1189 throw new java.lang.IllegalArgumentException( 1190 "Service.callBlockingMethod() given method descriptor for " + 1191 "wrong service type."); 1192 } 1193 switch(method.getIndex()) { 1194 case 0: 1195 return impl.getSum(controller, (com.endpoint.test.Sum.SumRequest)request); 1196 default: 1197 throw new java.lang.AssertionError("Can't get here."); 1198 } 1199 } 1200 1201 public final com.google.protobuf.Message 1202 getRequestPrototype( 1203 com.google.protobuf.Descriptors.MethodDescriptor method) { 1204 if (method.getService() != getDescriptor()) { 1205 throw new java.lang.IllegalArgumentException( 1206 "Service.getRequestPrototype() given method " + 1207 "descriptor for wrong service type."); 1208 } 1209 switch(method.getIndex()) { 1210 case 0: 1211 return com.endpoint.test.Sum.SumRequest.getDefaultInstance(); 1212 default: 1213 throw new java.lang.AssertionError("Can't get here."); 1214 } 1215 } 1216 1217 public final com.google.protobuf.Message 1218 getResponsePrototype( 1219 com.google.protobuf.Descriptors.MethodDescriptor method) { 1220 if (method.getService() != getDescriptor()) { 1221 throw new java.lang.IllegalArgumentException( 1222 "Service.getResponsePrototype() given method " + 1223 "descriptor for wrong service type."); 1224 } 1225 switch(method.getIndex()) { 1226 case 0: 1227 return com.endpoint.test.Sum.SumResponse.getDefaultInstance(); 1228 default: 1229 throw new java.lang.AssertionError("Can't get here."); 1230 } 1231 } 1232 1233 }; 1234 } 1235 1236 /** 1237 * <code>rpc getSum(.SumRequest) returns (.SumResponse);</code> 1238 */ 1239 public abstract void getSum( 1240 com.google.protobuf.RpcController controller, 1241 com.endpoint.test.Sum.SumRequest request, 1242 com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done); 1243 1244 public static final 1245 com.google.protobuf.Descriptors.ServiceDescriptor 1246 getDescriptor() { 1247 return com.endpoint.test.Sum.getDescriptor().getServices().get(0); 1248 } 1249 public final com.google.protobuf.Descriptors.ServiceDescriptor 1250 getDescriptorForType() { 1251 return getDescriptor(); 1252 } 1253 1254 public final void callMethod( 1255 com.google.protobuf.Descriptors.MethodDescriptor method, 1256 com.google.protobuf.RpcController controller, 1257 com.google.protobuf.Message request, 1258 com.google.protobuf.RpcCallback< 1259 com.google.protobuf.Message> done) { 1260 if (method.getService() != getDescriptor()) { 1261 throw new java.lang.IllegalArgumentException( 1262 "Service.callMethod() given method descriptor for wrong " + 1263 "service type."); 1264 } 1265 switch(method.getIndex()) { 1266 case 0: 1267 this.getSum(controller, (com.endpoint.test.Sum.SumRequest)request, 1268 com.google.protobuf.RpcUtil.<com.endpoint.test.Sum.SumResponse>specializeCallback( 1269 done)); 1270 return; 1271 default: 1272 throw new java.lang.AssertionError("Can't get here."); 1273 } 1274 } 1275 1276 public final com.google.protobuf.Message 1277 getRequestPrototype( 1278 com.google.protobuf.Descriptors.MethodDescriptor method) { 1279 if (method.getService() != getDescriptor()) { 1280 throw new java.lang.IllegalArgumentException( 1281 "Service.getRequestPrototype() given method " + 1282 "descriptor for wrong service type."); 1283 } 1284 switch(method.getIndex()) { 1285 case 0: 1286 return com.endpoint.test.Sum.SumRequest.getDefaultInstance(); 1287 default: 1288 throw new java.lang.AssertionError("Can't get here."); 1289 } 1290 } 1291 1292 public final com.google.protobuf.Message 1293 getResponsePrototype( 1294 com.google.protobuf.Descriptors.MethodDescriptor method) { 1295 if (method.getService() != getDescriptor()) { 1296 throw new java.lang.IllegalArgumentException( 1297 "Service.getResponsePrototype() given method " + 1298 "descriptor for wrong service type."); 1299 } 1300 switch(method.getIndex()) { 1301 case 0: 1302 return com.endpoint.test.Sum.SumResponse.getDefaultInstance(); 1303 default: 1304 throw new java.lang.AssertionError("Can't get here."); 1305 } 1306 } 1307 1308 public static Stub newStub( 1309 com.google.protobuf.RpcChannel channel) { 1310 return new Stub(channel); 1311 } 1312 1313 public static final class Stub extends com.endpoint.test.Sum.SumService implements Interface { 1314 private Stub(com.google.protobuf.RpcChannel channel) { 1315 this.channel = channel; 1316 } 1317 1318 private final com.google.protobuf.RpcChannel channel; 1319 1320 public com.google.protobuf.RpcChannel getChannel() { 1321 return channel; 1322 } 1323 1324 public void getSum( 1325 com.google.protobuf.RpcController controller, 1326 com.endpoint.test.Sum.SumRequest request, 1327 com.google.protobuf.RpcCallback<com.endpoint.test.Sum.SumResponse> done) { 1328 channel.callMethod( 1329 getDescriptor().getMethods().get(0), 1330 controller, 1331 request, 1332 com.endpoint.test.Sum.SumResponse.getDefaultInstance(), 1333 com.google.protobuf.RpcUtil.generalizeCallback( 1334 done, 1335 com.endpoint.test.Sum.SumResponse.class, 1336 com.endpoint.test.Sum.SumResponse.getDefaultInstance())); 1337 } 1338 } 1339 1340 public static BlockingInterface newBlockingStub( 1341 com.google.protobuf.BlockingRpcChannel channel) { 1342 return new BlockingStub(channel); 1343 } 1344 1345 public interface BlockingInterface { 1346 public com.endpoint.test.Sum.SumResponse getSum( 1347 com.google.protobuf.RpcController controller, 1348 com.endpoint.test.Sum.SumRequest request) 1349 throws com.google.protobuf.ServiceException; 1350 } 1351 1352 private static final class BlockingStub implements BlockingInterface { 1353 private BlockingStub(com.google.protobuf.BlockingRpcChannel channel) { 1354 this.channel = channel; 1355 } 1356 1357 private final com.google.protobuf.BlockingRpcChannel channel; 1358 1359 public com.endpoint.test.Sum.SumResponse getSum( 1360 com.google.protobuf.RpcController controller, 1361 com.endpoint.test.Sum.SumRequest request) 1362 throws com.google.protobuf.ServiceException { 1363 return (com.endpoint.test.Sum.SumResponse) channel.callBlockingMethod( 1364 getDescriptor().getMethods().get(0), 1365 controller, 1366 request, 1367 com.endpoint.test.Sum.SumResponse.getDefaultInstance()); 1368 } 1369 1370 } 1371 1372 // @@protoc_insertion_point(class_scope:SumService) 1373 } 1374 1375 private static com.google.protobuf.Descriptors.Descriptor 1376 internal_static_SumRequest_descriptor; 1377 private static 1378 com.google.protobuf.GeneratedMessage.FieldAccessorTable 1379 internal_static_SumRequest_fieldAccessorTable; 1380 private static com.google.protobuf.Descriptors.Descriptor 1381 internal_static_SumResponse_descriptor; 1382 private static 1383 com.google.protobuf.GeneratedMessage.FieldAccessorTable 1384 internal_static_SumResponse_fieldAccessorTable; 1385 1386 public static com.google.protobuf.Descriptors.FileDescriptor 1387 getDescriptor() { 1388 return descriptor; 1389 } 1390 private static com.google.protobuf.Descriptors.FileDescriptor 1391 descriptor; 1392 static { 1393 java.lang.String[] descriptorData = { 1394 "\n\rsumcode.proto\",\n\nSumRequest\022\016\n\006family\030" + 1395 "\001 \002(\t\022\016\n\006column\030\002 \002(\t\"\035\n\013SumResponse\022\016\n\003" + 1396 "sum\030\001 \002(\003:\001021\n\nSumService\022#\n\006getSum\022\013.S" + 1397 "umRequest\032\014.SumResponseB \n\021com.endpoint." + 1398 "testB\003SumH\001\210\001\001\240\001\001" 1399 }; 1400 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 1401 new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { 1402 public com.google.protobuf.ExtensionRegistry assignDescriptors( 1403 com.google.protobuf.Descriptors.FileDescriptor root) { 1404 descriptor = root; 1405 internal_static_SumRequest_descriptor = 1406 getDescriptor().getMessageTypes().get(0); 1407 internal_static_SumRequest_fieldAccessorTable = new 1408 com.google.protobuf.GeneratedMessage.FieldAccessorTable( 1409 internal_static_SumRequest_descriptor, 1410 new java.lang.String[] { "Family", "Column", }); 1411 internal_static_SumResponse_descriptor = 1412 getDescriptor().getMessageTypes().get(1); 1413 internal_static_SumResponse_fieldAccessorTable = new 1414 com.google.protobuf.GeneratedMessage.FieldAccessorTable( 1415 internal_static_SumResponse_descriptor, 1416 new java.lang.String[] { "Sum", }); 1417 return null; 1418 } 1419 }; 1420 com.google.protobuf.Descriptors.FileDescriptor 1421 .internalBuildGeneratedFileFrom(descriptorData, 1422 new com.google.protobuf.Descriptors.FileDescriptor[] { 1423 }, assigner); 1424 } 1425 1426 // @@protoc_insertion_point(outer_class_scope) 1427 }
2> 编写服务器端的代码
1 package com.endpoint.test; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 import org.apache.hadoop.hbase.Cell; 7 import org.apache.hadoop.hbase.CellUtil; 8 import org.apache.hadoop.hbase.Coprocessor; 9 import org.apache.hadoop.hbase.CoprocessorEnvironment; 10 import org.apache.hadoop.hbase.client.Scan; 11 import org.apache.hadoop.hbase.coprocessor.CoprocessorException; 12 import org.apache.hadoop.hbase.coprocessor.CoprocessorService; 13 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 14 import org.apache.hadoop.hbase.protobuf.ResponseConverter; 15 import org.apache.hadoop.hbase.regionserver.InternalScanner; 16 import org.apache.hadoop.hbase.util.Bytes; 17 import com.endpoint.test.Sum.SumRequest; 18 import com.endpoint.test.Sum.SumResponse; 19 import com.endpoint.test.Sum.SumService; 20 import com.google.protobuf.RpcCallback; 21 import com.google.protobuf.RpcController; 22 import com.google.protobuf.Service; 23 24 public class SumEndPoint extends SumService implements Coprocessor,CoprocessorService{ 25 26 private RegionCoprocessorEnvironment env; // 定义环境 27 @Override 28 public Service getService() { 29 30 return this; 31 } 32 33 @Override 34 public void start(CoprocessorEnvironment env) throws IOException { 35 if (env instanceof RegionCoprocessorEnvironment) { 36 this.env = (RegionCoprocessorEnvironment)env; 37 } else { 38 throw new CoprocessorException("no load region"); 39 } 40 41 } 42 43 @Override 44 public void stop(CoprocessorEnvironment env) throws IOException { 45 46 } 47 48 @Override 49 public void getSum(RpcController controller, SumRequest request, RpcCallback<SumResponse> done) { 50 51 // 设置扫描对象 52 Scan scan = new Scan(); 53 scan.addFamily(Bytes.toBytes(request.getFamily())); 54 scan.addColumn(Bytes.toBytes(request.getFamily()), Bytes.toBytes(request.getColumn())); 55 56 // 定义变量 57 SumResponse response = null; 58 InternalScanner scanner = null; 59 60 // 扫描每个region,取值后求和 61 try { 62 scanner = env.getRegion().getScanner(scan); 63 List<Cell> results = new ArrayList<Cell>(); 64 boolean hasMore = false; 65 Long sum = 0L; 66 do { 67 hasMore = scanner.next(results); 68 for (Cell cell : results) { 69 sum += Long.parseLong(new String(CellUtil.cloneValue(cell))); 70 } 71 results.clear(); 72 } while (hasMore); 73 // 设置返回结果 74 response = SumResponse.newBuilder().setSum(sum).build(); 75 } catch (IOException e) { 76 ResponseConverter.setControllerException(controller, e); 77 } finally { 78 if (scanner != null) { 79 try { 80 scanner.close(); 81 } catch (IOException e) { 82 //e.printStackTrace(); 83 } 84 } 85 } 86 // 将rpc结果返回给客户端 87 done.run(response); 88 89 } 90 91 }
3> 客户端测试代码
1 package com.endpoint.test; 2 3 import java.io.IOException; 4 import java.util.Map; 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.hbase.HBaseConfiguration; 7 import org.apache.hadoop.hbase.TableName; 8 import org.apache.hadoop.hbase.client.Connection; 9 import org.apache.hadoop.hbase.client.ConnectionFactory; 10 import org.apache.hadoop.hbase.client.HTable; 11 import org.apache.hadoop.hbase.client.coprocessor.Batch; 12 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback; 13 import com.endpoint.test.Sum.SumRequest; 14 import com.endpoint.test.Sum.SumResponse; 15 import com.endpoint.test.Sum.SumService; 16 import com.google.protobuf.ServiceException; 17 18 public class TestClient { 19 20 public static void main(String[] args) throws Exception { 21 22 // 配置HBse 23 Configuration conf = HBaseConfiguration.create(); 24 conf.set("hbase.zookeeper.quorum", "master,data1,data2"); 25 conf.set("hbase.zookeeper.property.clientPort", "2181"); 26 conf.setLong("hbase.rpc.timeout", 600000); 27 System.setProperty("hadoop.home.dir", "C:/hadoopfiles/hadoop-common-2.2.0-bin-master"); 28 29 // 建立一个数据库的连接 30 Connection conn = ConnectionFactory.createConnection(conf); 31 // 获取表 32 HTable table = (HTable) conn.getTable(TableName.valueOf("etable")); 33 34 long sum = 0L; 35 36 // 设置请求对象 37 final SumRequest request = SumRequest.newBuilder().setFamily("cf").setColumn("value").build(); 38 39 try { 40 // 获得返回值 41 Map<byte[], Long> result = table.coprocessorService(SumService.class, null, null, 42 new Batch.Call<SumService, Long>() { 43 44 @Override 45 public Long call(SumService service) throws IOException { 46 BlockingRpcCallback<SumResponse> rpcCallback = new BlockingRpcCallback<SumResponse>(); 47 service.getSum(null, request, rpcCallback); 48 SumResponse response = (SumResponse) rpcCallback.get(); 49 return response.hasSum() ? response.getSum() : 0L; 50 } 51 }); 52 // 将返回值进行迭代相加 53 for (Long v : result.values()) { 54 sum += v; 55 } 56 // 结果输出 57 System.out.println("sum: " + sum); 58 59 } catch (ServiceException e) { 60 e.printStackTrace(); 61 }catch (Throwable e) { 62 e.printStackTrace(); 63 } 64 table.close(); 65 conn.close(); 66 67 } 68 69 }
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表
要将协处理器加载到这个表上
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> 编写服务端代码
1 package com.observer.test; 2 3 import java.io.IOException; 4 import java.util.List; 5 import org.apache.hadoop.hbase.Cell; 6 import org.apache.hadoop.hbase.CellUtil; 7 import org.apache.hadoop.hbase.TableName; 8 import org.apache.hadoop.hbase.client.Durability; 9 import org.apache.hadoop.hbase.client.HTableInterface; 10 import org.apache.hadoop.hbase.client.Put; 11 import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver; 12 import org.apache.hadoop.hbase.coprocessor.ObserverContext; 13 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 14 import org.apache.hadoop.hbase.regionserver.wal.WALEdit; 15 import org.apache.hadoop.hbase.util.Bytes; 16 17 public class TestObserver extends BaseRegionObserver { 18 @Override 19 public void postPut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) 20 throws IOException { 21 // indextable作为二级索引表 22 HTableInterface table = e.getEnvironment().getTable(TableName.valueOf("indextable")); 23 // 获取值 24 List<Cell> cellList1 = put.get(Bytes.toBytes("cf"), Bytes.toBytes("name")); 25 List<Cell> cellList2 = put.get(Bytes.toBytes("cf"), Bytes.toBytes("value")); 26 // 写入数据 27 for (Cell cell1 : cellList1) { 28 // 原表的列cf:name的值作为indextable的rowkey,添加行 29 Put indexPut = new Put(CellUtil.cloneValue(cell1)); 30 for (Cell cell2 : cellList2) { 31 // 原表的列cf:value的值作为indextable表中列cf:value的值 。 32 indexPut.add(Bytes.toBytes("cf"), Bytes.toBytes("value"), CellUtil.cloneValue(cell2)); 33 } 34 35 table.put(indexPut); 36 } 37 38 table.close(); 39 } 40 41 }
2> 编写客户段代码
1 package com.observer.test; 2 3 import java.io.IOException; 4 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.hbase.HBaseConfiguration; 7 import org.apache.hadoop.hbase.TableName; 8 import org.apache.hadoop.hbase.client.Connection; 9 import org.apache.hadoop.hbase.client.ConnectionFactory; 10 import org.apache.hadoop.hbase.client.HTable; 11 import org.apache.hadoop.hbase.client.Put; 12 import org.apache.hadoop.hbase.util.Bytes; 13 14 public class DataClient { 15 16 public static void main(String[] args) throws IOException { 17 //配置 18 Configuration conf = HBaseConfiguration.create(); 19 conf.set("hbase.zookeeper.quorum", "master,data1,data2"); 20 conf.set("hbase.zookeeper.property.clientPort", "2181"); 21 //连接 22 Connection conn = ConnectionFactory.createConnection(conf); 23 HTable table = (HTable) conn.getTable(TableName.valueOf("initialtable")); 24 // 写入数据 25 Put put = new Put(Bytes.toBytes("row01")); 26 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("E")); 27 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("value"), Bytes.toBytes("151")); 28 table.put(put); 29 // 关闭资源 30 table.close(); 31 conn.close(); 32 33 } 34 35 }
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> 执行客户端代码,显示结果