【Shell】从大数据hdfs路径下载文件或者文件夹到本地Linux目录

#!/bin/bash
#=====================================================
#日期:2019-06-17
#功能:下载hdfs文件到本地脚本
#调用方法: sh getfromfstolocal.sh YYYYMMDD SRC username HDFS_PATH
#调用说明: sh getfromfstolocal.sh 日期  源系统 大数据用户名 hdfs绝对路径
#调用例子: sh getfromfstolocal.sh 20431230 edw edw_user /user/edw_user/EDW/ODM/CBS/KGLB_KMJZLS
# YYYYMMDD   8位批量日期
# SRC        rrs/rrs2/gls/icr/ecr/urp/edw
# username   大数据用户名 parrs/pagls/paicr/paecr/paurp/paedw
# HDFS_PATH  外表绝对路径
#=====================================================

etl_date=$1
source_system=$2
user_name=$3
hdfs_path=$4
#hdfs文件目录
hdfs_dir=${4%/*}
#hdfs文件名称
hdfs_file=${4##*/}

root_path=/data/script/jsj
datapath=${root_path}/data
local_etlpath=${datapath}/${source_system}/${etl_date}/
log_path=${root_path}/log/${source_system}/${etl_date}
keytab_path=/data/script/jsj/keytab/

#认证
kinit -c /data/script/jsj/${user_name}_krb5 -kt ${keytab_path}/${user_name}.keytab ${user_name}
export KRB5CCNAME=/data/script/jsj/${user_name}_krb5 

if [ ! -d ${log_path} ]; then
  mkdir -p ${log_path}
fi

shlog()
{
    local line_no msg 
    line_no=$1
    msg=$2
    echo "[getfromfstolocal.sh][$line_no]["`date "+%Y%m%d %H:%M:%S"`"] $msg ">> ${log_path}/getfromfstolocal${etl_date}.log
    echo "[getfromfstolocal.sh][$line_no]["`date "+%Y%m%d %H:%M:%S"`"] $msg "
}

shlog $LINENO "检查本地下载文件目录${local_etlpath}是否存在,不存在则创建该目录"
if [ ! -d ${local_etlpath} ]; then
  mkdir -p ${local_etlpath}
fi

cd ${local_etlpath}

if [ -f ${local_etlpath}${hdfs_file} ]; then
  rm -r ${local_etlpath}${hdfs_file}
  shlog $LINENO "文件${local_etlpath}${hdfs_file}删除成功"
fi

if [ -d ${local_etlpath}${hdfs_file} ]; then
  rm -r ${local_etlpath}${hdfs_file}
  shlog $LINENO "文件夹${local_etlpath}${hdfs_file}删除成功"
fi

hdfs dfs -get ${hdfs_path}
if [ $? -ne 0 ]; then
  shlog $LINENO "使用大数据用户${username}下载文件或文件夹${hdfs_path}失败"
  exit 1
else
  shlog $LINENO "使用大数据用户${username}下载文件或文件夹${hdfs_path}成功,本地路径:${local_etlpath}${hdfs_file}"
fi

exit 0
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值