使用的技巧:TRANSFORM
在hive中使用shell或python,主要优点是减少用java编写udf的工作。这样就十分方便与字段一些处理。
官方参考对python的说明比较详细,shell较少,先上传一个shell的示例;
SHELL
#!/bin/bash
# file name : convertDate.sh
# function : convert date to a custom format date,i.e. 2015-12-10 15:23:23 to 2015-12-10 15:23:15, map seconds to 00,15,30,45;
while read line; do
dt=`echo $line|awk -F'[:]' {s=$3-$3%15;if(s==0)s="00";print $1":"$2":"s}`
echo $dt
done
hive shell
hive << EOF
add file convertDate.sh
from(
from online_behavior
select transform(online_behavior.time)
using 'convertDate.sh'
as (ntime)
where online_behavior.dt='2015-06-22'
)ob
insert overwrite local directory '/home/user/etlProj/flowProj/t1.log'
select ntime,count(*) group by ntime;
EOF
注意:如果hive是按装在window上,using 之后要加上 cmd cmd.sh
,linux上可以只写cmd.sh