使用sqoop將hive分區表的數據導入到mysql的解決方案:shell腳本循環


方案1:使用shell腳本進行do while循環,挨個分區輸出。從開始的分區一直循環到結束分區即可。本例子為月,若需日,改為%Y-%m-%d即可,-1month改為-1day即可

partition_month=`date -d "$start_date" +"%Y-%m"`
partition_month_end=`date -d "$end_date" +"%Y-%m"`
while [[ ${partition_month} < $partition_month_end ]]
do
sqoop eval --connect "jdbc:mysql://。。。。。。。。。。。
--password "。。。。。。
--input-null-string '\\N' --input-null-non-string '\\N' \
--table 表的名字\
--export-dir hdfs://。。。/hive/warehouse/路徑/表名字/分區字段=$partition_month \
--input-fields-terminated-by '\001';
partition_month=`date -d "$partition_month -1 month" +"%Y-%m"`
echo "partition_month:"${partition_month}
done;

方案2:把要同步的數據拿出來,建立臨時表,從臨時表同步數據過去

 

具體例子: 

if [ ! -n "$1" ] ;then
start_date=`date --date='30 days ago' +%Y-%m-%d`
echo "start_date: "${start_date}
elif [ `date -d "$1" +%s` -lt `date -d "2018-01-01" +%s` ];
then
start_date='2018-01-01'
else
start_date=`date -d "$1" +%Y-%m-%d`
fi
if [ ! -n "${start_date}" ];
then
start_date=`date --date='30 days ago' +%Y-%m-%d`
fi

echo "start_date: "${start_date}

 

if [ ! -n "$2" ] ;then
end_date=`date --date='1 days ago' +%Y-%m-%d`
echo "end_date: "${end_date}
elif [ `date -d "$2" +%s` -lt `date -d "2019-01-01" +%s` ];
then
end_date=`date --date='1 days ago' +%Y-%m-%d`
else
end_date=`date -d "$2" +%Y-%m-%d`
fi

if [ ! -n "${end_date}" ];
then
end_date=`date --date='1 days ago' +%Y-%m-%d`
fi
echo "end_date: "${end_date} 

partition_month=`date -d "$start_date" +"%Y-%m"`
partition_month_end=`date -d "$end_date" +"%Y-%m"`

echo "partition_month:"${partition_month}
echo "partition_month_end:"${partition_month_end}


while [[ ${partition_month} < $partition_month_end ]]
do
sqoop eval --connect "jdbc:mysql://。。。。。。。。。。。
--password "。。。。。。
--input-null-string '\\N' --input-null-non-string '\\N' \
--table 表的名字\
--export-dir hdfs://。。。/hive/warehouse/app/。。。。/分區字段=$partition_month \
--input-fields-terminated-by '\001';
partition_month=`date -d "$partition_month -1 month" +"%Y-%m"`
echo "partition_month:"${partition_month}
done;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM