Hbase coprocessor (ii) ROWCOUNT code

Source: Internet
Author: User

public class Rowcountendpoint extends Exampleprotos.rowcountservice
Implements Coprocessor, Coprocessorservice {
Private Regioncoprocessorenvironment env;


Public Rowcountendpoint () {
}

@Override
Public Service GetService () {
return this;
}


@Override
public void GetRowCount (Rpccontroller controller, exampleprotos.countrequest request,
Rpccallback<exampleprotos.countresponse> done) {
Scan scan = new scan ();
Scan.setfilter (New Firstkeyonlyfilter ());
Exampleprotos.countresponse response = null;
Internalscanner scanner = null;
try {
Scanner = Env.getregion (). Getscanner (scan);
list<cell> results = new arraylist<cell> ();
Boolean hasmore = false;
byte[] lastrow = null;
Long Count = 0;
do {
Hasmore = Scanner.next (results);
for (Cell kv:results) {
byte[] CurrentRow = Cellutil.clonerow (KV);
if (lastrow = = NULL | |! Bytes.equals (LastRow, CurrentRow)) {
LastRow = CurrentRow;
count++;
}
}
Results.clear ();
} while (Hasmore);


Response = ExampleProtos.CountResponse.newBuilder ()
. SetCount (count). Build ();
} catch (IOException IoE) {
Responseconverter.setcontrollerexception (Controller, IOE);
} finally {
if (scanner! = NULL) {
try {
Scanner.close ();
} catch (IOException ignored) {}
}
}
Done.run (response);
}


/**
* Returns a count of all keyvalues in the region where this coprocessor is loaded.
*/
@Override
public void Getkeyvaluecount (Rpccontroller controller, exampleprotos.countrequest request,
Rpccallback<exampleprotos.countresponse> done) {
Exampleprotos.countresponse response = null;
Internalscanner scanner = null;
try {
Scanner = Env.getregion (). Getscanner (New Scan ());
list<cell> results = new arraylist<cell> ();
Boolean hasmore = false;
Long Count = 0;
do {
Hasmore = Scanner.next (results);
for (Cell kv:results) {
count++;
}
Results.clear ();
} while (Hasmore);


Response = ExampleProtos.CountResponse.newBuilder ()
. SetCount (count). Build ();
} catch (IOException IoE) {
Responseconverter.setcontrollerexception (Controller, IOE);
} finally {
if (scanner! = NULL) {
try {
Scanner.close ();
} catch (IOException ignored) {}
}
}
Done.run (response);
}

@Override
public void Start (Coprocessorenvironment env) throws IOException {
if (env instanceof regioncoprocessorenvironment) {
This.env = (regioncoprocessorenvironment) env;
} else {
throw new Coprocessorexception ("Must is loaded on a table region!");
}
}


@Override
public void Stop (Coprocessorenvironment env) throws IOException {
Nothing to do
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hbase coprocessor (ii) ROWCOUNT code

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.