forked from IBM/fiben-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db2loaddata.sh
executable file
·211 lines (177 loc) · 5.33 KB
/
db2loaddata.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
#!/bin/sh
# If a userid is not provided, then a local connection is assumed.
# If a userid is provided, but a password is not, the password is prompted for.
# If a schema is not provided, the current user is assumed.
# If the database is remote, provide a hostname and port, otherwise a local database is assumed
# -l is to use load instead of import. Default is import.
# Load will perform much faster, but requires LOAD authority.
# Typical uses would be as follows:
# 1. Local database
# db2loaddata.sh -d <dbname> -s <schema>
# 2. Remote database
# db2loaddata.sh -d <dbname> -h <hostname> -o <port> -s <schema> -u <userid>
# 3. Remote database already cataloged
# db2loaddata.sh -d <dbname> -s <schema> -u <userid>
USAGE="Usage: db2loaddata.sh -d <dbname> [-h <hostname> -o <port>] [-s <schema>] [-u <userid> [-p <password]] [-l]"
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
TABLELIST=tablelist.txt
DATADIR=data
DSDRIVERCFG=$SCRIPTPATH/db2dsdriver.cfg
INTTABFILE=$SCRIPTPATH/inttabs2.txt
ACTION=IMPORT
SCHEMA=$USER
function atexit
{
echo "Exiting ..."
if [[ -e $DSDRIVERCFG ]]
then
echo "Cleaning up generated $DSDRIVERCFG"
rm $DSDRIVERCFG
fi
if [[ -e $INTTABFILE ]]
then
echo "Cleaning up generated $INTTABFILE"
rm $INTTABFILE
fi
}
trap atexit EXIT
while getopts 'ld:s:u:p:h:o:' c
do
case $c in
d) DBNAME=$OPTARG ;;
s) SCHEMA=$OPTARG ;;
u) DBUSER=$OPTARG ;;
p) PASSWORD=$OPTARG ;;
h) DBHOST=$OPTARG ;;
o) DBPORT=$OPTARG ;;
l) ACTION=LOAD ;;
esac
done
SCHEMA=$(echo "$SCHEMA" | tr '[:lower:]' '[:upper:]')
if [[ -z $DBNAME ]]
then
echo $USAGE
exit 2
fi
# If we were given a userid but no password, prompt for it.
if [[ ! -z $DBUSER ]]
then
if [[ -z $PASSWORD ]]
then
stty -echo
printf "Password: "
read PASSWORD
stty echo
printf "\n"
fi
fi
echo "Connecting using the following values:"
echo "HOSTNAME = $DBHOST"
echo "PORT = $DBPORT"
echo "DBNAME = $DBNAME"
echo "SCHEMA = $SCHEMA"
echo "USER = $DBUSER"
# If the user has given us a hostname, then lets catalog it.
if [[ ! -z $DBHOST ]]
then
export DB2DSDRIVER_CFG_PATH=$DSDRIVERCFG
echo "Creating db2dsdriver.cfg alias for database ..."
db2cli writecfg add -dsn FIBENSCR -host $DBHOST -port $DBPORT -database $DBNAME
fi
# Make sure the data directory exists.
echo "Checking existence of directory $SCRIPTPATH/$DATADIR ..."
if [ ! -d $SCRIPTPATH/$DATADIR ]
then
echo "Data directory does not exists: $SCRIPTPATH/$DATADIR"
exit 1
fi
#Make sure all the .csv files exist
echo "Checking existence of .csv files ..."
for tablename in $(cat $TABLELIST)
do
if [ ! -f $SCRIPTPATH/$DATADIR/$tablename.csv ]
then
echo "CSV file does not exist: $SCRIPTPATH/$DATADIR/$tablename.csv"
exit 1
fi
done
if [[ -z $DBHOST ]]
then
DBCONNNAME=$DBNAME
else
DBCONNNAME=FIBENSCR
fi
echo "Connecting to database $DBNAME ..."
if [[ -z $DBUSER ]]
then
db2 connect to $DBCONNNAME
else
db2 connect to $DBCONNNAME user $DBUSER using $PASSWORD
fi
if [ $? -ne 0 ]
then
echo "Error: Could not connect to database, quitting."
exit 1
fi
echo "Creating schema $SCHEMA. SQL0601N is OK for schemas that already exist"
db2 -v "create schema $SCHEMA"
db2 -v "set current schema $SCHEMA"
echo "Creating tables ..."
db2 -tvf $SCRIPTPATH/FIBEN.sql
if [ $? -ne 0 ]
then
echo "Error: Could not create tables, quitting."
exit 1
fi
echo "Beginning table loads"
for tablename in $(cat $TABLELIST)
do
fulltab="$SCHEMA.$tablename"
echo "Loading from $DATADIR/$tablename.csv info $fulltab"
case $ACTION in
IMPORT)
db2 -v "import from $SCRIPTPATH/$DATADIR/$tablename.csv of del commitcount 100000 insert into $fulltab"
;;
LOAD)
db2 -v "load client from $SCRIPTPATH/$DATADIR/$tablename.csv of del replace into $fulltab nonrecoverable"
;;
esac
if [ $? -ne 0 ]
then
echo "Error: Failed to load from $DATADIR/$tablename.csv info $fulltab, quitting."
exit 1
fi
done
case $ACTION in
LOAD)
# The db2 load command will place tables into SET INTEGRITY PENDING mode.
# We need to call set integrity so they are usable.
echo "Checking for tables in SET INTEGRITY PENDING mode ..."
echo "db2 -x select rtrim( rtrim(tabschema) || '.' || rtrim(tabname) ) as qual_tab from syscat.tables where TABSCHEMA = '$SCHEMA' and ( CONST_CHECKED like '%N%' or status != 'N' or access_mode != 'F' ) with ur"
db2 -x "select rtrim( rtrim(tabschema) || '.' || rtrim(tabname) ) as qual_tab from syscat.tables where TABSCHEMA = '$SCHEMA' and ( CONST_CHECKED like '%N%' or status != 'N' or access_mode != 'F' ) with ur" > $INTTABFILE
if [ $? -ne 0 ]
then
echo "Error: Could not obtain list of tables in set integrity pending mode. Loaded tables may not be available, quitting."
exit 1
fi
# Get the list of tables output to the $INTTABFILE file
sed -i 's/[[:space:]]*$//' $INTTABFILE
INTPENDTABLES=""
while read -ru 3 LINE; do
INTPENDTABLES="$LINE, $INTPENDTABLES"
done 3< $INTTABFILE
#Strip last comma
INTPENDTABLES=${INTPENDTABLES%,*}
if [[ ! -z $INTPENDTABLES ]]
then
db2 -v "set integrity for $INTPENDTABLES immediate checked"
if [ $? -ne 0 ]
then
echo "Error: Error setting integrity for loaded files, table may not be available."
exit 1
fi
fi
;;
esac
db2 terminate