HBase coprocessor Combat

Source: Internet
Author: User

First, the coprocessor introduction

Definition: HBase provides a mechanism (framework) for computing some of the logic of the user on the data store side and the HBase server. The coprocessor allows users to run their own code on the HBase server.

Category: System coprocessor, table coprocessor

Observer, equivalent to a trigger inside a relational database

Endpoint, similar to stored procedures

Second, OBserver

The design intent of the observer is to allow the user to overload the Upcall method of the coprocessor framework by inserting code, and the specific event-triggering callback method is executed by the core code of HBase. Can be divided into Regionobserver, Regionserverobserver, Masterobserver, Walobserver

Regionobserver provides client-side data manipulation Events hooks: Get, Put, Delete, scan, etc.

Regionserverobserver specializes in dealing with some of the events on Regionserver

Masterobserver provides ddl-type of operation Hooks. such as creating, deleting, modifying data tables, etc.

Walobserver provides Wal-related operations

These interfaces can be used in the same place at the same time, in different priority order. Users can implement the complex HBase function layer arbitrarily based on the coprocessor. HBase has a number of events that can trigger the observer method, which is integrated in the HBase API from the HBase0.92 version. However, these APIs may vary for a variety of reasons

Changes, different versions of the interface changes relatively large. The Regionobserver works as shown in.

Third, Endpoint

The terminal is the interface of the dynamic RPC plug-in, and its implementation code is installed on the server side, thus being able to wake up via HBase RPC. The Client class library provides a very convenient way to invoke these dynamic interfaces, which can invoke a terminal at any time, and their implementation code will be executed remotely by the target region, and the result will be returned to the terminal. Users can combine the

Add new features to hbase with these powerful plug-in interfaces.

Endpoint service-Side authoring

1. Create a Endpoint.proto file, generate a Java file

  1. Option Java_package = "Edu.endpoint";
  2. Option Java_outer_classname = "Sum";
  3. Option java_generic_services = true;
  4. Option Java_generate_equals_and_hash = true;
  5. option optimize_for = speed;
  6. Message Sumrequest {
  7. Required String family = 1;
  8. Required String column = 2;
  9. }
  10. Message Sumresponse {
  11. Required Int64 sum = 1 [default = 0];
  12. }
  13. Service Sumservice {
  14. RPC Getsum (sumrequest)
  15. Returns (Sumresponse);
  16. }

2. Generate the Java code for the. Proto file.

Protoc endpoint.proto–java_out=./

Copy the generated. java files to the source code package file for the Java project that corresponds to eclipse. Add all dependent library files under HBase lib.

3. Writing the service-side Sumendpoint.java class

  1. Package edu.endpoint;
  2. Import java.io.IOException;
  3. Import java.util.ArrayList;
  4. Import java.util.List;
  5. Import Org.apache.hadoop.hbase.Coprocessor;
  6. Import org.apache.hadoop.hbase.CoprocessorEnvironment;
  7. Import Org.apache.hadoop.hbase.client.Scan;
  8. Import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
  9. Import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
  10. Import Org.apache.hadoop.hbase.regionserver.InternalScanner;
  11. Import org.apache.hadoop.hbase.util.Bytes;
  12. Import Com.google.protobuf.RpcCallback;
  13. Import Com.google.protobuf.RpcController;
  14. Import Com.google.protobuf.Service;
  15. Import Edu.endpoint.Sum.SumRequest;
  16. Import Edu.endpoint.Sum.SumResponse;
  17. Import Edu.endpoint.Sum.SumService;
  18. Public class Sumendpoint extends Sumservice implements coprocessor,coprocessorservice{
  19. private regioncoprocessorenvironment env;
  20. public void Getsum (Rpccontroller controller,sumrequest request,rpccallback<sumresponse> done) Throws ioexception{
  21. Scan scan = new Scan ();
  22. Scan.addfamily (Bytes.tobytes (request.getfamily ()));
  23. Scan.addcolumn (Bytes.tobytes (request.getfamily ()), Bytes.tobytes (Request.getcolumn ()));
  24. Sumresponse response = null;
  25. Internalscanner scanner = null;
  26. try{
  27. Scanner = Env.getregion (). Getscanner (scan);
  28. list<cell> results = new arraylist<cell> ();
  29. Boolean hasmore = false;
  30. Long sum = 0;
  31. Do {
  32. Hasmore = Scanner.next (results);
  33. For (Cell cell:results) {
  34. Sum + = Long.parselong (new String (Cellutil.clonevalue (cell)));
  35. }
  36. Results.clear ();
  37. } while (Hasmore);
  38. Response = Sumresponse.newbuilder (). Setsum (sum). Build ();
  39. }catch (IOException e) {
  40. Responseconverter.setcontrollerexception (controller,e);
  41. }finally {
  42. if (scanner!=null) {
  43. try {
  44. Scanner.close ();
  45. } catch (IOException e) {
  46. //TODO auto-generated catch block
  47. E.printstacktrace ();
  48. }
  49. }
  50. }
  51. Done.run (response);
  52. }
  53. Public Service GetService () {
  54. return this ;
  55. }
  56. @Override
  57. public void Start (Coprocessorenvironment env) throws IOException {
  58. if (env instanceof regioncoprocessorenvironment) {
  59. this.env = (regioncoprocessorenvironment) env;
  60. }Else {
  61. throw New Coprocessorexception ("No load Region");
  62. }
  63. }
  64. @Override
  65. public void Stop (coprocessorenvironment arg0) throws IOException {
  66. }
  67. }

HBase coprocessor Combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.