Environment: cdh5.1.0
Enable CoprocessorMethod 1.
Enable Coprocessor Aggregation (enable coprocessor Aggregation)
We have two methods: 1. Start the global aggregation and be able to manipulate the data on all the tables. By modifying the Hbase-site.xml file, you only need to add the following code:
<property> <name>hbase.coprocessor.user.region.classes</name> <value> Org.apache.hadoop.hbase.coprocessor.aggregateimplementation</value> </property>
Enable CoprocessorMethod 2.
Enable table aggregation, which only takes effect for a specific table. Implemented through the HBase Shell.
(1) Disable the specified table. hbase> Disable ' mytable '
(2) Add aggregation hbase> alter ' mytable ', METHOD = ' Table_att ', ' coprocessor ' and ' = ' | org.apache.hadoop.hbase.coprocessor.aggregateimplementation| | '
(3) Restart the specified table hbase> enable ' mytable '
Code:
Package Com.jamesfen.hbase;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.hbase.hbaseconfiguration;import Org.apache.hadoop.hbase.tablename;import Org.apache.hadoop.hbase.client.scan;import Org.apache.hadoop.hbase.client.coprocessor.aggregationclient;import Org.apache.hadoop.hbase.client.coprocessor.longcolumninterpreter;import Org.apache.hadoop.hbase.coprocessor.columninterpreter;import Org.apache.hadoop.hbase.util.bytes;public Class myaggregationclient {private static final byte[] table_name = bytes.tobytes ("bigtable1w");p rivate static final byte[] CF = Bytes.tobytes ("BD");p ublic static void Main (string[] args) throws Throwable {Configuration customconf = new configuration (); Customconf.set ("Hbase.zookeeper.quorum", "192.168.58.101");//Increase RPC communication duration customconf.setlong ("Hbase.rpc.timeout" , 600000);//Set Scan cache Customconf.setlong ("hbase.client.scanner.caching", 1000); Configuration configuration = Hbaseconfiguration.create (customconf); Aggregationclient aggregationclient = NEW aggregationclient (configuration); Scan scan = new scan ();//Specifies the Scan column family, unique value scan.addfamily (CF);//long rowCount = aggregationclient.rowcount (table_name, NULL, scan); Long RowCount = Aggregationclient.rowcount (tablename.valueof ("bigtable1w"), New Longcolumninterpreter (), scan) ; System.out.println ("Row count is" + RowCount);}}
HBase coprocessor Statistics Row count