废话少说,直接上代码
#!/bin/bash
##################
#作者:
#描述:导出hive表
#
##################
usage(){
echo -e "脚本功能:从hive中导出某张表的数据到本地"
echo -e "命令参数:"
echo -e "-h --help 帮助文档"
echo -e "-D --database 数据库名称,必填"
echo -e "-T --table 表名,必填"
echo -e "-F --fields 字段列表,英文逗号隔开,可添加默认值,必填,记住用引号包起来,否则接收的不完整,例如\"id,name,'10' as age,createdate\""
echo -e "-t --terminated-by 字段数据分隔符,可选,默认'|'"
echo -e "-W --where 过滤条件,可选,记住用引号包起来,否则接收的不完整,例如\"dt='2017-04-21' and createdata='2017-04-21'\""
exit 1
}
#-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项,没有冒号表示不接参数,单冒号表示必须接参数,双冒号表示参数可选
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o hD:T:F:t:W: --long help,database:,table:,fields:,terminated-by:,where: -n 'export_hive_table.sh' -- "$@"`
if [ $? != 0 ]; then
exit 1
fi
#echo $ARGS
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
database=""
table=""
fields=""
terminatedby="|"
where=""
while true
do
case "$1" in
-h|--help)
usage;
shift 1
;;
-D|--database)
database=$2;
shift 2
;;
-T|--table)
table=$2;
shift 2
;;
-F|--fields)
fields=$2;
shift 2
;;
-t|--terminated-by)
terminatedby=$2;
shift 2
;;
-W|--where)
where=$2;
shift 2
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
echo "database:$database"
if [ -z "$database" ];then
echo "-D|--database 数据库名必传!"
exit 1
fi
echo "table:$table"
if [ -z "$table" ];then
echo "-T|--table 表名必传!"
exit 1
fi
echo "fields:$fields"
if [ -z "$fields" ];then
echo "-F|--fields 字段列表必传!"
exit 1
fi
echo "terminated-by:$terminatedby"
echo "where:$where"
if [ -n "$where" ];then
where="where $where"
fi
##此脚本所在目录路径
bin_abs_path=$(readlink -f $(dirname $0))
base_dir=$bin_abs_path/..
##导出的数据,存储为本地文件的目录路径
localfile_dir=$base_dir/data/$database/$table
hive_sql="set mapreduce.job.queuename=root.bigdata.friend;insert overwrite local directory '$localfile_dir' row format delimited fields terminated by '$terminatedby' select $fields from $database.$table $where;"
echo "`date +'%F %H:%M:%S'` $hive_sql"
echo "`date +'%F %H:%M:%S'` export hive data ......"
hive -e "$hive_sql"
success=$?
echo "`date +'%F %H:%M:%S'` hive command return $success"
if [ $success != 0 ];then
echo "`date +'%F %H:%M:%S'` export hive data failed!"
exit $success
fi
echo "`date +'%F %H:%M:%S'` export hive data success! data directory:$localfile_dir "