· 8 years ago · Aug 22, 2018, 11:12 AM
1#!/bin/bash
2cur_date=`date +%Y-%m-%d`
3table=pay
4datebase=ods
5hive_table=pay
6dir=public/ods/upp/
7if hdfs dfs -test -e /$dir/$table
8then
9 dates=$(hdfs dfs -ls /$dir/$table)
10 lastest=$(echo $dates | grep -o -E -e "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}" |uniq -d )
11 yestday=$(echo ${lastest:0-10:10})
12else
13 yestday=1900-01-01
14fi
15if test $yestday = $cur_date
16then
17 hdfs dfs -rm -r /$dir/$table/$cur_date
18 hive -e "alter table $datebase.$hive_table drop partition (pdate='$cur_date');"
19 dates=$(hdfs dfs -ls /$dir/$table)
20 lastest=$(echo $dates | grep -o -E -e "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}" |uniq -d )
21 yestday=$(echo ${lastest:0-10:10})
22fi
23if test $yestday==''
24then
25 yestday=1900-01-01
26fi
27
28output=$( \
29sqoop import \
30--connect jdbc:mysql://10.92.192.79:3306/database \
31--username Up \
32--password Up\
33--target-dir /${dir}/${table}/${cur_date} \
34--query "select * from ${table} where \$CONDITIONS and (CAST(create_date as DATE)<'${cur_date}' and CAST(create_date as DATE)>='${yestday}') or (CAST(last_edit_date as DATE)<'${cur_date}' and CAST(last_edit_date as DATE)>='${yestday}')" \
35--fields-terminated-by '\001' \
36--split-by fmid \
37--m 1 \
382>&1)
39
40if [ $? -ne 0 ];then
41 echo "wrong"
42 echo "---------------------------------------------------------------------------------------------"
43 echo $output
44fi
45hive -e "
46use $datebase;
47create external table if not exists $hive_table (
48fmid string,
49pay_card_issue_name string,
50) partitioned by (pdate string) location 'hdfs://ns1/${dir}/${table}';
51alter table $hive_table add partition (pdate='${cur_date}') location 'hdfs://ns1/${dir}/${table}/${cur_date}';"