一、hive执行脚本
1、hive -e “sql语句”
会将查询的结果打印在控制台上。
2、hive -e “sql语句” >> xxx >xxx
如果是“>>xxx”会将查询的结果重定向到xxx文件中,会显示OK和抓取的数据条数 ,并且后续的日志都会追加在前一条日志后面。
如果是“ >xxx”会将查询的结果重定向到xxx文件中,会显示OK和抓取的数据条数,并且每次执行的日志会覆盖前面的日志
3、hive -S -e “sql语句” >> xxx
会将查询的结果重定向到xxx文件中,不会显示OK和抓取的数据条数
4、hive -f sqlfile
执行文件中的sql(用于sql比较长的时候), 执行完后就退出
5、hive -i sqlfile
执行文件中的sql(用于sql比较长的时候), 执行完后就不退出,还可继续执行其他sql。在这种模式下若还要去执行其他文件中的sql, 需要在hive模式中使用 source sqlfile 来配合。
6、hive与linux交互
例如:当我们使用hive -i sqlfile时,已经进入hive模式中,这时候需要去执行其他的sqlfile,但是不知道sqlfile在什么路径下,这样就显得很尴尬了。
在hive模式下使用 ! + linux 命令;:
7、hive与hdfs交互
由于hive已经是运行在hadoop上所以直接可以使用 dfs + hdfs命令:
二、用hive脚本来任务调度
使用crontab -e :
使用crontab -l 可以查看自己的crontab的调度任务
写的hive shell脚本如下:
三、hive -f test.sql(test.sql里面写的是sql语句)的补充:
#!/bin/bash
if [ ! -n “$1” ] ;then
date=date -d today +%Y%m%d
else
date=$1
fi
if [ ! -n "
2
"
]
;
t
h
e
n
d
a
t
e
1
=
2" ] ;then date1=
2"];thendate1=date
else
date1=$2
fi
to_date=date +%Y%m%d -d "$date +1 day"
last_date=date +%Y%m%d -d "$date -1 day"
year=
(
d
a
t
e
−
d
l
a
s
t
−
d
a
y
+
m
o
n
t
h
=
(date -d last-day +%Y) month=
(date−dlast−day+month=(date -d last-day +%m)
day=$(date -d last-day +%d)
d=$date
to_date=date +%Y%m%d -d "$d +1 day"
last_date=date +%Y-%m-%d -d "$d -1 day"
last2_date=date +%Y%m%d -d "$d -7 day"
echo " 程序开始时间是:" $(date +%Y-%m-%d-%H:%M:%S)
hive -hiveconf enter_school_date= l a s t d a t e − h i v e c o n f e n t e r s c h o o l d a t e 1 = last_date -hiveconf enter_school_date1= lastdate−hiveconfenterschooldate1=last2_date -f /home/hive/dw/mds_engine_nssa_info_new_meishan.sql
echo " 程序结束时间是:" $(date +%Y-%m-%d-%H:%M:%S)
备注:这个脚本是把enter_school_date这个参数的变量传入到mds_engine_nssa_info_new_meishan.sql文件的sql语句中去,
mds_engine_nssa_info_new_meishan.sql文件的sql语句如下:
drop table hue_engine_nssa_info1;
create table hue_engine_nssa_info1 as
select a.,
(case when b.device is null then ‘’ else b.risk_info end) mal_info
from
(select a.,(case when channel in (‘leshitv’,‘chonghongtv’,‘whaleytv’,‘hisensetv’) then ‘1’ else ‘0’ end) intel_channel
from mds_engine_nssa_info a where dt=’${hiveconf:enter_school_date}’)a
left outer join
(select * from ods_dim_device)b
on a.device=b.device;