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
- Option Java_package = "Edu.endpoint";
- 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. 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
- Package edu.endpoint;
- Import java.io.IOException;
- Import java.util.ArrayList;
- Import java.util.List;
- 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.RegionCoprocessorEnvironment;
- Import Org.apache.hadoop.hbase.regionserver.InternalScanner;
- Import org.apache.hadoop.hbase.util.Bytes;
- Import Com.google.protobuf.RpcCallback;
- Import Com.google.protobuf.RpcController;
- Import Com.google.protobuf.Service;
- Import Edu.endpoint.Sum.SumRequest;
- Import Edu.endpoint.Sum.SumResponse;
- Import Edu.endpoint.Sum.SumService;
- Public class Sumendpoint extends Sumservice implements coprocessor,coprocessorservice{
- private regioncoprocessorenvironment env;
- public void Getsum (Rpccontroller controller,sumrequest request,rpccallback<sumresponse> done) Throws ioexception{
- 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;
- try{
- Scanner = Env.getregion (). Getscanner (scan);
- list<cell> results = new arraylist<cell> ();
- Boolean hasmore = false;
- Long sum = 0;
- 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) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
- Done.run (response);
- }
- 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 arg0) throws IOException {
- }
- }
HBase coprocessor Combat