承接上一篇文档《新增访客数量MR统计之MR数据输出到MySQL》
hive-1.2.1的版本可以直接映射HBase已经存在的表
如果说想在hive创建表,同时HBase不存在对应的表,也想做映射,那么采用编译后的hive版本hive-1.2.1-hbase
1. Hive中创建外部表,关联hbase
CREATE EXTERNAL TABLE event_log_20180728(key string,pl string,ver string,s_time string,u_ud string,u_sd string,en string)STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:pl,info:ver,info:s_time,info:u_ud,info:u_sd,info:en")TBLPROPERTIES("hbase.table.name" = "event_log_20180728");
![54e11d6a3111b5ed89630dce81bc39c3.png](https://i-blog.csdnimg.cn/blog_migrate/33a9b8cfc88d3719ea925b2a07c87bd6.jpeg)
统计多少个新用户:
select count(*) from event_log_20180728 where en="e_l";
![acf5ad2ee7ad764bf86ac437f0c1ff50.png](https://i-blog.csdnimg.cn/blog_migrate/215674fc490ae69a5eb10996e9010cdf.jpeg)
![f5a924b2b5c69c80277036df955c3dbd.png](https://i-blog.csdnimg.cn/blog_migrate/20ca211e5542ea789764790d9d77ba8e.jpeg)
2. 提取数据,进行初步的数据过滤操作,最终将数据保存到临时表
创建临时表
CREATE TABLE stats_hourly_tmp01(pl string,ver string,s_time string,u_ud string,u_sd string,en string,`date` string,hour int);
![c6ac1fe32c58cbe44248713589be01ca.png](https://i-blog.csdnimg.cn/blog_migrate/f2eda29ac5e3c46815fe651d565b75a0.jpeg)
将原始数据提取到临时表中
INSERT OVERWRITE TABLE stats_hourly_tmp01SELECT pl,ver,s_time,u_ud,u_sd,en,from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd'), hour(from_unixtime(cast(s_time/1000 as int),'yyyy-MM-dd HH:mm:ss'))FROM event_log_20200510WHERE en="e_l" or en="e_pv";