HBase 協處理器統計行數
阿新 • • 發佈:2019-02-14
環境:cdh5.1.0
啟用協處理器方法1.
啟用協處理器 Aggregation(Enable Coprocessor Aggregation)
我們有兩個方法:1.啟動全域性aggregation,能過操縱所有的表上的資料。通過修改hbase-site.xml這個檔案來實現,只需要新增如下程式碼:
<property> <name>hbase.coprocessor.user.region.classes</name> <value>org.apache.hadoop.hbase.coprocessor.AggregateImplementation</value> </property>
啟用協處理器方法2.
啟用表aggregation,只對特定的表生效。通過HBase Shell 來實現。
(1)disable指定表。hbase> disable 'mytable'
(2)新增aggregation hbase> alter 'mytable', METHOD => 'table_att','coprocessor'=>'|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||'
(3)重啟指定表 hbase> enable 'mytable'
程式碼:
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"); private static final byte[] CF = Bytes.toBytes("bd"); public static void main(String[] args) throws Throwable { Configuration customConf = new Configuration(); customConf.set("hbase.zookeeper.quorum", "192.168.58.101"); //提高RPC通訊時長 customConf.setLong("hbase.rpc.timeout", 600000); //設定Scan快取 customConf.setLong("hbase.client.scanner.caching", 1000); Configuration configuration = HBaseConfiguration.create(customConf); AggregationClient aggregationClient = new AggregationClient( configuration); Scan scan = new Scan(); //指定掃描列族,唯一值 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); } }