关于HIVE数据仓库的基本操作,hive数据仓库


[Author]: kwu 

关于HIVE数据仓库的基本操作


1、数据库划分:

default : 默认库,测试库。对应路径 /hdfs/hive/default
stage : 中转库 对应路径 /hdfs/dw/stage
ods :  正式库 对应路径 /hdfs/dw/ods


2、创建表
create EXTERNAL table test_kwu (
dateday string comment "日期:如2015-01-01", 
datetime string comment "时间 : 如 11:30:01:123",
ip string comment "IP:用户本机IP或用户所在网段对外路由IP",
cookieid string comment "用户cookie:统一在用户端生成的唯一标志",
userid string comment "用户和讯注册ID :用户在和讯网的注册ID", 
logserverip string comment "记录日志服务器IP :日志收集服务器IP",
referer string comment "来源 :用户浏览网页的REFER",
requesturl string comment "访问网址 : 当前访问网址",
remark1 string comment "【暂时没用】 :该数据无意义,由于早期加入目前不能去除",
remark2 string comment "【暂时没用】 : 该数据无意义,由于早期加入目前不能去除",
alexaflag string comment "ALEXA标志  :这个字段也是早期加入,当用户安装alexa工具时值为1,否则为0.早期加入,目前来看应该没有任何意义了。",
ua string comment "UA :用户浏览器UA",
wirelessflag string comment "无线频道标志:给无线频道专用的,一个单词,表示该文章对应和讯哪一个频道"

comment "浏览轨迹日志"
partitioned by(day string comment "按天的分区表字段")
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '
STORED AS TEXTFILE 
location '/hive/default/test_kwu';  --注意:此处的路径对应到数据库的路径(去掉 "/hdfs"),后缀加上table的名称。


3、装载数据
load data local inpath '/home/kwu/data/20150512.dat' overwrite into table test_kwu partition (day='20150512');
insert into table test_kwu PARTITION (day='20150507')  select  dateday, datetime,ip,cookieid,userid, logserverip,referer,
requesturl ,remark1,remark2,alexaflag,ua,wirelessflag  from test_kwu  ;


4、压缩处理
set hive.enforce.bucketing=true;
set hive.exec.compress.output=true;
set mapred.output.compress=true;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
set io.compression.codecs=org.apache.hadoop.io.compress.GzipCodec;
insert overwrite table test_kwu PARTITION (day='20150507')  select  dateday, datetime,ip,cookieid,userid, logserverip,referer,
requesturl ,remark1,remark2,alexaflag,ua,wirelessflag  from test_kwu  ;


5、基本查询语句
查询每天的PV
select dateday,count(*) from tracklog group by dateday;


尽量避全表的聚合函数
select count(*) as cnt from tracklog group by cookieid having cnt=1 ;  


可采用子查询代替
select dateday,count(t.cookieid) from (
select count(cookieid) as cnt,cookieid,dateday
from tracklog  
group by cookieid,dateday  having cnt=1 
) t group by dateday;


相关内容