需求: hive分区表导入hdfs按天文件夹 按小时文件的数据, 由于历史数据较多, 手动工作量大,采用Linux脚本
工具: notepad++
--------------------------------------------------------------------------以下 脚本------------------------------------------------------------------------------
#!/bin/bash
date_list='20170913 20170914 20170915 ...(列表数据 ,间隔为空格) ... 20180631';
hive -e "create external table if not exists safe.imcs_data(imcs string) partitioned by (static_time string) row format delimited;"
for date in $date_list
do
echo "start to hive partition"
hive -e "alter table safe.imcs_data add partition (static_time = '$date') location '/user/safe/data/imcs_pic_info_hdfs/$date';"
echo "hive partition finished"
done
--------------------------------------------------------------------------以上 脚本------------------------------------------------------------------------------
说明: #为Linux脚本注释 ,#! 为声明Linux脚本采用哪种脚本语言(Linux自带好几种shell,此处采用bash shell)
开始对脚本操作
假定脚本命名为 aa.sh ---> 上传到Linux服务器(假定目录/文件为 /bb/aa.sh) ---> 修改文件权限(chmod 755 /bb/aa.sh 可执行) --->运行脚本 ./bb/aa.sh
排除错误: /bin/bash^M: bad interpreter: No such file or directory
解决方案: window环境编写的脚本格式为dos, 与Linux格式冲突,修改格式 ----> Linux下运行 dos2unix /bb/aa.sh
再执行 ./bb/aa.sh
脚本正常运行,hive建表,添加分区