package endPointBatchDel;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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.AggregateImplementation;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/*************************************
*Class Name:rowcountBycoprocessor
*Description:<fast get rowcount>
*@author:Administrator
*@create:2019/4/30
*@since 1.0.0
*************************************/
public class rowcountBycoprocessor {
/**
* HBase API添加協處理器
* */
public static void addCoprocessor(Configuration conf, String tableName) {
try {
byte[] tableNameBytes = Bytes.toBytes(tableName);
Connection conn = ConnectionFactory.createConnection(conf);
HBaseAdmin hbaseAdmin = (HBaseAdmin) conn.getAdmin();//得到管理員
//HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
HTableDescriptor htd = hbaseAdmin.getTableDescriptor(tableNameBytes);
if (!htd.hasCoprocessor(AggregateImplementation.class.getName())) {
hbaseAdmin.disableTable(tableNameBytes);
htd.addCoprocessor(AggregateImplementation.class.getName());
hbaseAdmin.modifyTable(tableNameBytes, htd);
hbaseAdmin.enableTable(tableNameBytes);
}
hbaseAdmin.close();
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 統計表數量
*
*/
public static void exeCount(Configuration conf, String tableName, String family) {
try {
// 使用hbase提供的聚合coprocessor
AggregationClient aggregationClient = new AggregationClient(conf);
Scan scan = new Scan();
// 指定掃描列族,唯一值
scan.addFamily(Bytes.toBytes(family));
long start = System.currentTimeMillis();
long rowCount = aggregationClient.rowCount(TableName.valueOf(tableName), new LongColumnInterpreter(), scan);
System.out.println("Row count: " + rowCount + "; time cost: " + (System.currentTimeMillis() - start) + "ms");
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// Configuration config = HBaseConfiguration.create();
// String zkAddress = "host1:2181,host2:2181,host3:2181";
// config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
// Connection connection = null;
// try{
// connection = ConnectionFactory.createConnection(config);
// } catch (Exception e) {
// e.printStackTrace();
// }
//-----------------------
String zkAddress = "host1:2181,host2:2181,host3:2181";
String tableName = "am_bk";
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", zkAddress);
//conf.set("hbase.rootdir", "hdfs://host:8020/hbase");
// 提高RPC通信時長
conf.setLong("hbase.rpc.timeout", 600000);
// 設置Scan緩存
conf.setLong("hbase.client.scanner.caching", 1000);
addCoprocessor(conf, tableName);
//exeCount(conf, tableName, "c");
exeCount(conf, tableName, "photo");
}
}
代碼加載協處理器統計hbase錶行數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.