自动创建文件
#!/bin/bash
beg_date=`date -d "${1}" +%s`
end_date=`date -d "${2}" +%s`
if((${beg_date} > ${end_date}));then
echo "beg_date < end_date"
exit 0;
fi
currentDate=""
for((i=beg_date;i<=${end_date};i=i+86400))
do
currentDate=`date -d @${i} +%Y%m%d`
echo "--create hive bales /${currentDate}-----"
hive -e "use data;CREATE TABLE order_status_log${currentDate}(id string,order_id string,order_status string,operate_time string) row format deli
mited fields terminated by '|';"
done
自动采集
#!/bin/bash
log_dir=/root/log
log_prefix=order_status_log
function write_log(){
log_format="`date '+%Y-%m-%d' %H:%M%S` ${1} >>"
echo "${log_format} $2" >> ${log_dir}/${log_prefix}.`date '+%Y-%M:%S'`.log
[ $1 == "ERROR" ] && exit 1
}
date=`cat /root/sh/date`
afterday_timestamp=$[`date -d "${date}" +%s` +86400]
afterday=`date -d @${afterday_timestamp} +%Y%m%d`
sed -i "s/order_status_log${date}/order_status_log${afterday}/g" /root/datax/job/logcopy.json
sed -i "s/${date}/${afterday}/g" /root/sh/date
write_log INFO "log.json文件的日期由${date}成功替换成${afterday}!!!"
python /root/datax/bin/datax.py /root/datax/job/logcopy.json
if [ $? -eq 0 ];then
result_num=`hive -e "use test;select count(id) from order_status_log${afterday};"`
write_log INFO "数据从mysql采集成功!!!采集成功的数据有${result_num}条!!!"
else
write_log ERROR "数据从mysql采集到hive失败"
fi
分区
#!/bin/bash
beg_date=`date -d "${1}" +%s`
end_date=`date -d "${2}" +%s`
currentDate=""
for((i = ${beg_date};i<=${end_date};i=i+86400))
do
currentDate=`date -d @${i} +%Y%m%d`
hive -e "
set hive.exec.dynamic.partition.mode=nostrict;
set hive.exec.dynamic.partition=true;
set hive.exec.max.dynamic.partitions=1000;
insert into table data.order_status_log partition(day) select id,order_id,order_status,operate_time,date(operate_time) from data.order_status_
log${currentDate}
"
echo "-------create /${currentDate}---------"
done
hive -S -e "select day,count(id) from data.order_status_log group by day;" >> /root/sh/month_count.txt
user="root"
passwd="123456"
host="123.60.71.57"
mysql_conn="mysql -h"$host" -u "$user" -p"$passwd" -P8888"
cat /root/sh/month_count.txt | while read day count
do
${mysql_conn} -e "insert into test.month_count values('${day}','${count}')"
done
log