HBase 统计方法
使用 Hive 统计
-
建一张 Hive 表映射 HBase 表
CREATE EXTERNAL TABLE LJKTEST( ID STRING , AGE STRING , NAME STRING , COMPANY STRING , SCHOOL STRING )STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,0:AGE,0:NAME,0:COMPANY,0:SCHOOL") TBLPROPERTIES("hbase.table.name" = "LJKTEST");
- 执行 hive 统计 sql 即可
这边使用 COUNT(1) 和 COUNT(*) 都不起作用,统计出来是 0. 只能使用 COUNT(字段)。应该跟映射机制有关系。
SELECT COUNT(ID) FROM LJKTEST;
使用 phoenix 统计
SELECT COUNT(1) FROM LJKTEST;
使用 HBase 原生接口统计
HADOOP_CLASSPATH=`hbase classpath` hadoop jar /usr/hdp/current/hbase-client/lib/hbase-server.jar rowcounter LJKTEST
使用 spark 统计
spark 统计 hbase
pom 文件引入依赖,<font color=”red”> 必须加上 exclusion,否则会报错 class “javax.servlet.FilterRegistration”‘s signer information does not match signer information of other classes in the same package</font>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.1.2.2.5.0.0-1245</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>jetty</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
<exclusion>
<artifactId>jetty-util</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api-2.5</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.6.2.2.5.0.0-1245</version>
</dependency>
写 spark 统计 HBase 代码
@Test
def sparkCountHBase(): Unit = {val sc = new SparkContext("local","hbase-test")
val conf = HBaseConfiguration.create()
conf.set(TableInputFormat.INPUT_TABLE, "LJKTEST")
val hbaseRDD = sc.newAPIHadoopRDD(conf,classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result])
val count = hbaseRDD.count()
println(s"总共有 $count 条数据!")
}
Spark 统计 Phoenix
pom 引入依赖
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-spark</artifactId>
<version>4.7.0-HBase-1.1</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>jetty</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
<exclusion>
<artifactId>jetty-util</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api-2.5</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
</exclusions>
</dependency>
<!--https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-spark-->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>1.6.2.2.5.0.0-1245</version>
</dependency>
spark 统计 Phoenix 代码
@Test
def sparkCountPhoenix(): Unit = {val sc = new SparkContext("local","phoenix-test")
val sqlContext = new SQLContext(sc)
val df = sqlContext.load(
"org.apache.phoenix.spark",
Map("table" -> "LJKTEST", "zkUrl" -> "dn1:2181")
)
// df.show()
println(s"总共有 ${df.count} 条数据!")
}
附录
- HBase 表映射到 hive 表
- Phoenix Apache Spark Plugin