大数据采集与处理相关脚本

该博客主要涉及自动化创建和管理Hive表,包括根据指定日期范围动态创建分区,并从MySQL采集数据至Hive。此外,还展示了如何使用shell脚本进行日志处理和更新,以及将数据插入MySQL的分区表中。
摘要由CSDN通过智能技术生成
自动创建文件
#!/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 '|';"
 #hive -e "use test;drop table order_status_log${currentDate}"
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值