Hive脚本以及用Hive脚本的Crontab任务调度

本文详细介绍了Hive SQL的多种执行方式,包括通过命令行执行SQL语句、执行SQL脚本文件以及如何在Hive模式下与Linux和HDFS进行交互。此外,还探讨了如何使用crontab进行Hive任务的定时调度,提供了实用的shell脚本示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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"];thendate1=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=(date−dlast−day+month=(date -d last-day +%Y) month=(datedlastday+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=lastdate−hiveconfenterschooldate1=last_date -hiveconf enter_school_date1=lastdatehiveconfenterschooldate1=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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值