1.场景分析
生产环境均为腾讯云服务器,日志数据计划存储于hdfs中,由于日志数据较大(压缩后1t/天),不断扩充云盘成本消耗大。鉴于对象存储的存储成本较为低廉,但是日常频繁使用会产生流量费用。
鉴于此,规划将日常常用热数据采用hdfs存储(存储时间2~3个月),超过该时间段数据采用对象存储。改方案均采用同一套hadoop架构,使用hive均可以读取到,在降低成本的同时提高数据的利用率。
2.准备条件
3.详细脚本
create external table ods.test_dh (
`timestamp` bigint comment '时间',
`offset` bigint comment '偏移量',
`request_uri` string comment '请求uri'
)
comment '日志表'
partitioned by (
`part_day` string,
`part_hour` string)
row format serde
'org.apache.hadoop.hive.serde2.jsonserde'
stored as inputformat
'org.apache.hadoop.mapred.textinputformat'
outputformat
'org.apache.hadoop.hive.ql.io.hiveignorekeytextoutputformat'
location
'hdfs://hadoopcluster/hadoop/dm_dw/on/ods/test_dh';
create external table ods.test_bu (
`timestamp` bigint comment '时间',
`offset` bigint comment '偏移量',
`request_uri` string comment '请求uri'
)
comment '日志备份表'
partitioned by (
`part_day` string,
`part_hour` string)
row format serde
'org.apache.hadoop.hive.serde2.jsonserde'
stored as inputformat
'org.apache.hadoop.mapred.textinputformat'
outputformat
'org.apache.hadoop.hive.ql.io.hiveignorekeytextoutputformat'
location
'cosn://xxxx/hadoop/dm_dw/on/ods/test_bu';
#!/bin/bash
# 定义变量方便修改
app=ods
#hdfs表
hdfs_table=test_dh
#对象存储表
cos_table=test_bu
hdfs_path1=hdfs://hadoopcluster/hadoop/dm_dw/on/$app/$hdfs_table
hdfs_path2=/hadoop/dm_dw/on/$app/$hdfs_table
cos_path=cosn://xxxx/hadoop/dm_dw/on/$app/$cos_table
# hdfs数据冷备份,将hdfs3个月前的数据按照月份移动至cos中,移动完成后删除hdfs中的数据,数据保留时间最大不超过3个月
# 执行时间为每个月1号12点
do_date=$1
hr=${do_date: 8: 2}
date1=${do_date: 0: 8}
#date1=`date -d "$date1 +1 day" +%y%m%d`
date1_month=`date -d "$date1" +%y%m`
date3=`date -d "$[ $date1_month + 0 ]01" +%y%m%d`
if [ "$date1" -eq "$date3" ] && [ "$hr" -eq 11 ] ; then
start_date=`date -d "$date3 -3 month" +%y%m%d`
end_date=`date -d "$date3 -2 month" +%y%m%d` # 日期自增
echo ================== $cos_table 导入月份为 $start_date ==================
while [[ $start_date != $end_date ]]
do
hadoop fs -test -e $hdfs_path2/part_day=$start_date
if [[ $? -eq 0 ]]; then
#数据备份开始
echo "$start_date 数据开始移动..."
hadoop distcp -dmapreduce.job.queuename=dw $hdfs_path1/part_day=$start_date $cos_path
hadoop fs -test -e $cos_path/part_day=$start_date/part_hour=23
if [[ $? -eq 0 ]]; then
#数据备份完成,删除hdfs中的数据
echo "路径 $cos_path/part_day=$start_date 数据已移动至cos,hdfs数据删除......"
hadoop fs -rm -rf $hdfs_path2/part_day=$start_date
else
echo " $cos_path/part_day=$start_date hdfs数据没有移动至cos"
fi
else
echo " $hdfs_path2/part_day=$start_date 文件夹中没有数据"
fi
start_date=$(date -d "$start_date +1 day" +%y%m%d)
done
echo ================== $cos_table 导入月份为 $do_date ==================
sql="msck repair table ${app}.${cos_table};"
hive -e "$sql"
else
echo "$do_date 不是月初第一天的12点"
fi
发表评论