这里用到了shell脚本中的条件语句,因为时间比较紧,所以先不做详细的展开学习,先给自己留一下课后作业:
1、判断文件目录是否存在的参数 -d
2、[ ] 和 [[ ]] 的区别和注意事项
3、if 语句的表达注意事项
放一下写的代码
#!/bin/bash
#第一步:对/data/columns目录下的前一日目录进行判断,如果目录存在,则上传到hdfs,不存在目录则输出文件目录不存在
#每日定时把/data/columns目录下的内容进行文件分类上传到hdfs指定目录下
the_year=$1
the_month=$2
the_day=$3
#文件存放的目录
linux_dir=/data/columns/"$the_year$the_month$the_day"/
#判断目录是否存在
if [ ! -d "$linux_dir" ];then
echo " "$linux_dir"目录不存在! "
fi
#目录文件存在则上传到hdfs
if [ -d "$linux_dir" ]; then
#上传目录
hdfs_category_dir=/test/lee/gdfs/"$the_year$the_month$the_day"/Columns/
hdfs_asset_dir=/test/lee/gdfs/"$the_year$the_month$the_day"/ColumnsResAsset/
hdfs_series_dir=/test/lee/gdfs/"$the_year$the_month$the_day"/SeriesResChild/
#创建目录
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -mkdir /test/lee/gdfs/"$the_year$the_month$the_day"
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -mkdir /test/lee/gdfs/"$the_year$the_month$the_day"/Columns
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -mkdir /test/lee/gdfs/"$the_year$the_month$the_day"/ColumnsResAsset
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -mkdir /test/lee/gdfs/"$the_year$the_month$the_day"/SeriesResChild
#读取目录
ls $linux_dir | while read fileName
do
#上传栏目架构表
if [[ "$fileName" == Columns_* ]];then
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -put $linux_dir$fileName $hdfs_category_dir$fileName
fi
#上传媒资信息表
if [[ "$fileName" == ColumnsResAsset_* ]];then
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -put $linux_dir$fileName $hdfs_asset_dir$fileName
fi
#上传子集集合关联表
if [[ "$fileName" == SeriesResChild_* ]];then
/usr/local/hadoop/hadoop-2.8.1/bin/hadoop fs -put $linux_dir$fileName $hdfs_series_dir$fileName
fi
done
fi
网友评论