-
Notifications
You must be signed in to change notification settings - Fork 1
/
datamove.sh
executable file
·214 lines (167 loc) · 7.27 KB
/
datamove.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
##########################################################################################################
#Description:
#Moves data in and out of Amazon S3 storage to Hadoop Cluster
#Primary Functions: export and import
#Uses configuration files to store S3 keys (Owned by root)
#Needs root access to execute
#Example usage
#./datamove.sh export /tmp/tomcatLog dir conf_datamove_devs3.conf
#./datamove.sh import /tmp/tomcatLog dir conf_datamove_devs3.conf
#./datamove.sh export testdb db conf_datamove_devs3.conf
#./datamove.sh import testdb db conf_datamove_devs3.conf
##########################################################################################################
if [ $# -ne 4 ]
then
echo -e "\nError: Exactly three arguments are allowed. Like export dbtest db OR import dbtest dir\n"
exit 1
elif [[ ! "$1" =~ ^(export|import)$ ]]
then
echo -e "\nError: First argument needs to be export or import. Full argument set like export dbtest db OR import dbtest dr\n"
exit 1
elif [[ ! "$3" =~ ^(db|dir)$ ]]
then
echo -e "\nError: Last argument needs to be db or dr. Full argument set like export dbtest db OR import dbtest dr\n"
exit 1
fi
baseDir=/root/scripts/dataCopy
operation=$1
hiveDBName=$2
copyType=$3
confFile=$4
function logsetup {
ts=$(date +%Y_%m_%d_%H_%M_%S)
LOGFILE="$baseDir/$hiveDBName/datamove_$ts.log"
exec > >(tee -a $LOGFILE)
exec 2>&1
}
function log {
echo "[$(date +%Y/%m/%d:%H:%M:%S)]: $*"
}
function exportDB {
log "Step1: Getting list of tables"
hive -e "USE $hiveDBName; SHOW tables;" > $hiveDBName/${hiveDBName}_alltables.txt
log "Step2: Fetching Hive table DDLs"
log "DROP DATABASE IF EXISTS $hiveDBName CASCADE;" > $hiveDBName/${hiveDBName}_all_tables_DDL.txt
log "CREATE DATABASE $hiveDBName;" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
log "USE $hiveDBName;" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
while read line
do
log "Processing table $line"
hive -e "USE $hiveDBName; SHOW CREATE TABLE ${hiveDBName}.$line;" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
echo ";" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
hive -e "USE $hiveDBName; SHOW PARTITIONS $line;" > $hiveDBName/tmp_part.txt
while read tablepart
do
partname=`echo ${tablepart/=/=\"}`
echo "ALTER TABLE ${hiveDBName}.$line ADD PARTITION ($partname\");" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
echo "ANALYZE TABLE ${hiveDBName}.$line PARTITION ($partname\") COMPUTE STATISTICS;" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
done < $hiveDBName/tmp_part.txt
checklib=$(hive -e "USE $hiveDBName; DESCRIBE EXTENDED $line;" |grep serializationLib)
if [[ $checklib == *"OpenCSVSerde"* ]]
then
echo "ALTER TABLE ${hiveDBName}.$line SET SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde';" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
escapechar="\"escapeChar\"=\"\\\\\""
quotechar="\"quoteChar\"=\"\\\"\""
seperatorchar="\"separatorChar\"=\",\""
echo "ALTER TABLE ${hiveDBName}.$line SET SERDEPROPERTIES ($escapechar, $quotechar, $seperatorchar);" >> $hiveDBName/${hiveDBName}_all_tables_DDL.txt
fi
done < $hiveDBName/${hiveDBName}_alltables.txt
sed -i.bak '/LOCATION/,+1 d' $hiveDBName/${hiveDBName}_all_tables_DDL.txt
log " Wrote DDLs to $hiveDBName/${hiveDBName}_all_tables_DDL.txt"
if [ -e /tmp/${hiveDBName}_all_tables_DDL.txt ]
then
rm -f /tmp/${hiveDBName}_all_tables_DDL.txt
sudo -u hdfs hdfs dfs -rm /tmp/${hiveDBName}_all_tables_DDL.txt
fi
cp $hiveDBName/${hiveDBName}_all_tables_DDL.txt /tmp
sudo -u hdfs hdfs dfs -put /tmp/${hiveDBName}_all_tables_DDL.txt /tmp/${hiveDBName}_all_tables_DDL.txt
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update hdfs:///tmp/${hiveDBName}_all_tables_DDL.txt ${s3bucket}/${hiveDBName}/${hiveDBName}_all_tables_DDL.txt
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update hdfs:///apps/hive/warehouse/${hiveDBName}.db ${s3bucket}/apps/hive/warehouse/${hiveDBName}.db
}
function exportDIR {
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update hdfs:///${formattedDirName} ${s3bucket}/${formattedDirName}
}
function importDB {
log "Step1: Running disributed copy."
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update ${s3bucket}/${hiveDBName}/${hiveDBName}_all_tables_DDL.txt hdfs:///tmp/${hiveDBName}_all_tables_DDL.txt
log "Step2: Running database import."
if [ -e /tmp/${hiveDBName}_all_tables_DDL.txt ]
then
rm -f /tmp/${hiveDBName}_all_tables_DDL.txt
fi
sudo -u hdfs hdfs dfs -get /tmp/${hiveDBName}_all_tables_DDL.txt /tmp/${hiveDBName}_all_tables_DDL.txt
if [ ! -e $hiveDBName ]
then
mkdir $hiveDBName
fi
if [ -e $hiveDBName/${hiveDBName}_all_tables_DDL.txt ]
then
rm -f $hiveDBName/${hiveDBName}_all_tables_DDL.txt
fi
chmod 755 /tmp/${hiveDBName}_all_tables_DDL.txt
cp -f /tmp/${hiveDBName}_all_tables_DDL.txt $hiveDBName/${hiveDBName}_all_tables_DDL.txt
sudo -u hive hive -f /tmp/${hiveDBName}_all_tables_DDL.txt
cat /tmp/${hiveDBName}_all_tables_DDL.txt | grep ANALYZE > /tmp/${hiveDBName}_all_tables_ANALYZE.txt
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update ${s3bucket}/apps/hive/warehouse/${hiveDBName}.db hdfs:///apps/hive/warehouse/${hiveDBName}.db
sudo -u hdfs hdfs dfs -chown -R hive:hdfs /apps/hive/warehouse/${hiveDBName}.db
sudo -u hive hive -f /tmp/${hiveDBName}_all_tables_ANALYZE.txt
}
function importDIR {
sudo -u hdfs hadoop distcp -D fs.s3a.server-side-encryption-algorithm=${fs_s3a_server_side_encryption_algorithm} \
-D fs.s3a.secret.key=${fs_s3a_secret_key} \
-D fs.s3a.access.key=${fs_s3a_access_key} \
-p -update ${s3bucket}/${formattedDirName} hdfs:///${formattedDirName}
sudo -u hdfs hdfs dfs -chown -R hive:hdfs /${formattedDirName}
}
##############
#MAIN
##############
cd $baseDir
if [ $copyType = 'dir' ]
then
formattedDirName=$(echo $hiveDBName | sed 's/^\///')
hiveDBName=$(basename $hiveDBName)
fi
if [ ! -e $hiveDBName ]
then
mkdir $hiveDBName
fi
logsetup
log "$copyType $hiveDBName copy initiation..."
. conf/$confFile
if [ $operation = 'export' ]
then
log "$copyType $hiveDBName export initiation..."
if [ $copyType = 'db' ]
then
exportDB
else
exportDIR
fi
log "$hiveDBName Export Processing finished."
else
log "$copyType $hiveDBName import initiation..."
if [ $copyType = 'db' ]
then
importDB
else
importDIR
fi
log "$copyType $hiveDBName Import processing finished. Please verify the objects"
fi