#! /bin/bash
#first.sh all/表名 日期
#1、判断参数是否传入
if [ $# -lt 1 ]
then
echo "至少需要传入一个参数..."
exit
fi
#2、判断日志是否传入,如果传入了日志,则用指定的日期,如果没有传入,则用前一天的日期
[ "$2" ] && datestr=$2 || datestr=$(date -d '-1 day' +%Y%m%d)
import_date(){
/opt/module/sqoop/bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/gmall \
--username root \
--password root123 \
--delete-target-dir \
--num-mappers 1 \
--split-by $2 \
--query "$1" \
--target-dir hdfs://hadoop102:8020/gmall/$3/$datestr \
--compress \
--compression-codec lzop \
--null-string '\\N' \
--null-non-string '\\N' \
--fields-terminated-by ,
#给lzo压缩文件创建索引
hadoop jar /opt/module/hadoop/share/hadoop/common/hadoop-lzo-0.4.20.jar com.hadoop.compression.lzo.DistributedLzoIndexer hdfs://hadoop102:8020/gmall/$3/$datestr
}
#3、导入数据
case $1 in
#导入所有表数据
"all")
import_date "select * from sku_info where \$CONDITIONS" "id" "sku_info"
import_date "select * from comment_info where \$CONDITIONS" "id" "comment_info"
import_date "select * from user_info where \$CONDITIONS" "id" "user_info"
import_date "select * from base_province where \$CONDITIONS" "id" "base_province"
;;
#导入指定表数据
"sku_info")
import_date "select * from sku_info where \$CONDITIONS" "id" "sku_info"
;;
"comment_info")
import_date "select * from comment_info where \$CONDITIONS" "id" "comment_info"
;;
"user_info")
import_date "select * from user_info where \$CONDITIONS" "id" "user_info"
;;
"base_province")
import_date "select * from base_province where \$CONDITIONS" "id" "base_province"
;;
*)
echo "第一个参数输入错误,必须是all或者是表名"
;;
esac