栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

13、Hive数据仓库——结合shell脚本企业实战用法,定时调度

13、Hive数据仓库——结合shell脚本企业实战用法,定时调度

文章目录

Hive shell脚本企业实战用法,定时调度

建立分区表增加分区查看某个表的所有分区往分区中插入数据在shell命令中运行Hive的SQL

使用hive -e使用hive -f 使用Shell脚本定时调度

新建.sh脚本文件编辑hql.sh修改hql.sh权限新建并编辑stu_pt.sql文件新建logs目录 编辑定时器运行查看logs目录下的日志文件

Hive shell脚本企业实战用法,定时调度

  在我们日常的工作中,肯定不会一条一条的使用SQL去取出你想要的数据,这个时候我们就可以用Linux中的定时器Shell脚本。我们可以编写一个定时任务,一小时或者一天将SQL写入脚本,让SQL自动执行然后取出数据即可,这样我们就可以实现了数据的定时调度。

建立分区表
create external table students_pt1
(
    id bigint
    ,name string
    ,age int
    ,gender string
    ,clazz string
)
PARTITIonED BY(pt string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/student/input1';
增加分区
alter table students_pt1 add partition(pt='20220220');

alter table students_pt1 add partition(pt='20220219');

alter table students_pt1 add partition(pt='20220218');

alter table students_pt1 add partition(pt='20220221');

alter table students_pt1 add partition(pt='20220222');

alter table students_pt1 add partition(pt='20220223');

alter table students_pt1 add partition(pt='20220224');
查看某个表的所有分区
show partitions students_pt1;
往分区中插入数据
insert into table students_pt1 partition(pt='20220220') select * from student1;
load data local inpath '/usr/local/soft/data/students.txt' into table students_pt1 partition(pt='20200221');
在shell命令中运行Hive的SQL 使用hive -e
hive -e "select * from test1.students limit 10"
使用hive -f

  在/usr/local/soft/目录下创建scripts目录,并创建stu.sql文件

cd /usr/local/soft/
mkdir scripts
vim stu.sql

  在stu.sql里编辑一条SQL(这里写最简单的),注意这里不带分号;

select * from test1.students limit 10
[root@master scripts]# hive -f stu.sql
使用Shell脚本定时调度 新建.sh脚本文件
vim hql.sh
编辑hql.sh
#!/bin/sh
#date="2022-02-22"
date=$(date  "+%Y%m%d")
#sql1="select * from test1.students_pt1 where pt='${date}'"
#hive -e "${sql1}"
sed -i "s/!everydate!/${date}/" /usr/local/soft/scripts/stu_pt.sql
cat  /usr/local/soft/scripts/stu_pt.sql
hive -f  /usr/local/soft/scripts/stu_pt.sql
sed -i "s/${date}/!everydate!/" /usr/local/soft/scripts/stu_pt.sql
cat /usr/local/soft/scripts/stu_pt.sql
修改hql.sh权限
chmod a+x hql.sh
新建并编辑stu_pt.sql文件
vim stu_pt.sql
select * from test1.students_pt where pt='!everydate!';
新建logs目录
[root@master scripts]# mkdir logs
编辑定时器
crontab -e
*/1 * * * * /usr/local/soft/scripts/hql.sh >> /usr/local/soft/scripts/logs/1.log
运行

在scripts目录下运行脚本

sh hql.sh

或

./hql.sh
查看logs目录下的日志文件
cd /usr/local/soft/scripts/logs/

cat 1.log

到底啦!关注靓仔学习更多的大数据技术!( •̀ ω •́ )✧

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/745721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号