#!/bin/bash
currentDate=`date -d today +"%Y%m%d"`
if [ x"$1" = x ]; then
echo "====没有导入数据的日期,输入日期===="
exit
else
echo "====使用导入数据的日期 ===="
currentDate=$1
fi
echo "日期为 : $currentDate"

#查询hive ODS层表 TO_YCAK_USR_LOGIN_D 中目前存在的最大的ID
maxid=`hive -e "select max(id) from TO_YCAK_USR_LOGIN_D"`
echo "Hive ODS层表 TO_YCAK_USR_LOGIN_D 最大的ID是$maxid"
if [ x"$maxid" = xNULL ]; then
echo "maxid is NULL 重置为0"
maxid=0
fi

#sqoop 导入数据:
sqoop import \
--connect jdbc:mysql://node1:3306/ycak?dontTrackOpenResources=true\&defaultFetchSize=10000\&useCursorFetch=true\&useUnicode=yes\&characterEncoding=utf8 \
--username root \
--password 123456 \
--table user_login_info \
--target-dir /user/hive_ha/warehouse/data/user/TO_YCAK_USR_LOGIN_D/data_dt=${currentDate} \
--num-mappers 1 \
--fields-terminated-by '\t' \
--incremental append \
--check-column id \
--last-value $maxid

#更新Hive 分区
hive -e "alter table TO_YCAK_USR_LOGIN_D add partition(data_dt=${currentDate});"