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