分享

Shell脚本实现hive增量加载

J20_果农 2016-10-13 19:47:03 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 5 19780
实现思路:
1.每天凌晨将前一天增量的数据从业务系统导出到文本,并FTP到hadoop集群某个主节点上
上传路径默认为:/mnt/data/crawler/
2.主节点上通过shell脚本调用hive命令加载本地增量文件到hive临时表
3.shell脚本中,使用hive sql 实现临时表中的增量数据更新或者新增增量数据到hive主数据表中

实现步骤:
1.建表语句, 分别创建两张表test_temp, test 表
[mw_shl_code=sql,false]
create table crawler.test_temp(
a.id         string,
a.name       string,
a.email      string,
create_time  string
)
row format delimited
fields terminated by ','
stored as textfile
;
+++++++++++++++++++++++++++++++++
create table crawler.test(
a.id         string,
a.name       string,
a.email      string,
create_time  string
)
partitioned by (dt string)
row format delimited
fields terminated by '\t'
stored as orc
;
[/mw_shl_code]

2.编写处理加载本地增量数据到hive临时表的shell脚本test_temp.sh
[mw_shl_code=shell,false]
#! /bin/bash
##################################
# 调用格式:                      #
# 脚本名称 [yyyymmdd]            #
# 日期参数可选,默认是系统日期-1 #
##################################
dt=''
table=test_temp
#获取当前系统日期
sysdate=`date +%Y%m%d`
#获取昨日日期,格式: YYYYMMDD
yesterday=`date -d yesterday +%Y%m%d`
#数据文件地址
file_path=/mnt/data/crawler/
if [ $# -eq 1 ]; then
  dt=$1
elif [ $# -eq 0 ]; then
  dt=$yesterday
else
  echo "非法参数!"
  #0-成功,非0-失败
  exit 1
fi
filename=$file_path$table'_'$dt'.txt'
if [ ! -e $filename ]; then
  echo "$filename 数据文件不存在!"
  exit 1
fi  
hive<<EOF
load data local inpath '$filename' overwrite into table crawler.$table;
EOF
if [ $? -eq 0 ]; then
  echo ""
  echo $dt "$table 加载成功!"
else
  echo ""
  echo $dt "$table 加载失败!"
fi
[/mw_shl_code]

3.增量加载临时数据到主数据表的shell脚本test.sh
[mw_shl_code=shell,false]
#! /bin/bash
##################################
table=test
#获取当前系统日期
sysdate=`date +%Y%m%d`

#实现增量覆盖
hive<<EOF
set hive.exec.dynamic.partition=true;  
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table crawler.test partition (dt)
select a.id, a.name, a.email, a.create_time, a.create_time as dt
from (
  select id, name, email, create_time from crawler.test_temp
  union all
  select t.id, t.name, t.email, t.create_time
  from  crawler.test t
  left outer join crawler.test_temp t1
  on t.id = t1.id
  where t1.id is null
) a;
quit;
EOF
if [ $? -eq 0 ]; then
  echo $sysdate $0 " 增量抽取完成!"
else
  echo $sysdate $0 " 增量抽取失败!"
fi
[/mw_shl_code]




已有(5)人评论

跳转到指定楼层
恋枫缩影 发表于 2017-4-17 09:54:48
正好搞hive版的数据ETL,参考一下
回复

使用道具 举报

a530491093 发表于 2018-12-5 15:56:35
不错,感谢分享,收益了。
回复

使用道具 举报

renyuliu 发表于 2019-1-10 13:44:51
感谢楼主分享。思路不错。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条