Hive脚本化运行

大量的hive查询任务,如果用交互式shell来进行输入的话,显然效率及其低下,因此,生产中更多的是使用脚本化运行机制:
该机制的核心点是:hive可以用一次性命令的方式来执行给定的hql语句

执行简单hql语句

直接在终端输入 hive -e "hql语句;"
eg:

[root@hadoop01 ~]# hive -e “insert into table t_dest select * from t_src;”

优化:可以将上述命令写入shell脚本中,以便于脚本化运行hive任务,并控制、调度众多hive任务。
eg:

[root@hadoop01 ~]# vi test.sh
#!/bin/bash
hive -e "select * from db_test.t_user"
hive -e "select * from default.t_user"
#将hql语句赋值给一个变量,hive -e "$变量" 执行该变量
hql="create table  default.t_bash as select * from db_order.t_order"
hive -e "$hql"

执行复杂hql语句

将hql语句写入一个.hql文件:

[root@hadoop01 ~]# vi test.hql
select * from db_test.t_user;
select count(1) from db_order.t_user;
create table  default.t_bash as select * from db_order.t_order;

然后,用[root@hadoop01 ~]# hive -f /root/x.hql 来执行
执行该命令时会出现一堆连接数据库的日志信息,如果嫌麻烦可以在hive配置文件中做修改:
hive安装目录下/conf/ 下找到hive-site.xml文件(这个文件是我自己创建的,详情请见https://blog.csdn.net/amin_hui/article/details/82143267),添加useSSL=false。
这里写图片描述
&amp为HTML编程格式中的“&”

定时执行脚本

如果数据需要每天执行脚本,手动去执行很麻烦,可以采用定时器crontab定时执行脚本
[root@hadoop01 ~]# crontab -e 进行编写任务,编辑操作和规划与vi相同
定时器书写格式:
0 1 * * * /usr/local/apache-hive-1.2.2-bin/bin/hive -f /root/test.hql

猜你喜欢

转载自blog.csdn.net/amin_hui/article/details/82261041
今日推荐