-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001-load.sh
182 lines (160 loc) · 4.75 KB
/
001-load.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
#!/usr/bin/env bash
BASE=/docker-entrypoint-initdb.d
MSQL="mysql --defaults-extra-file=$BASE/my.cnf ${MYSQL_DATABASE}"
# schema
echo "creating schema..."
(
time $MSQL<<EOF
create table alt_name (
id bigint not null,
gid bigint not null,
code varchar(7),
name varchar(400) not null,
is_preferred tinyint(1) not null default 0,
is_short tinyint(1) not null default 0,
is_colloquial tinyint(1) not null default 0,
is_historic tinyint(1) not null default 0,
primary key (id),
key an_code (code),
key an_historic (is_historic)
) engine=myisam default charset=utf8mb4 ;
create table country_info (
country_code char(2) not null,
name varchar(200),
capital varchar(200),
area double,
population bigint,
continent_code char(2),
tld char(3),
currency_code char(3),
currencyname char(20),
phone varchar(20),
postalcodeformat varchar(100),
postalcoderegex varchar(255),
gid bigint,
primary key (country_code),
key ci_continent_code_key (continent_code),
unique key ci_gid_ukey (gid)
) engine=myisam default charset=utf8mb4 ;
create table feature (
gid bigint not null,
name varchar(200),
latitude decimal(10,7),
longitude decimal(10,7),
fclass char(1),
fcode varchar(10),
continent_code char(2) not null default '',
country_code char(2) not null default '',
admin1_code varchar(20) not null default '',
admin2_code varchar(80) not null default '',
admin3_code varchar(20) not null default '',
admin4_code varchar(20) not null default '',
parent_continent bigint,
parent_country bigint,
parent_admin1 bigint,
parent_admin2 bigint,
parent_admin3 bigint,
parent_admin4 bigint,
population bigint,
elevation int,
dem int,
timezone varchar(40),
mod_date date,
primary key (gid),
key fcode_key (fcode),
key f_continent_code_key (continent_code),
key f_country_code_key (country_code),
key f_admin1_code_key (admin1_code),
key f_admin2_code_key (admin2_code),
key f_admin3_code_key (admin3_code),
key f_admin4_code_key (admin4_code)
) engine=myisam default charset=utf8mb4 ;
EOF
) || exit $?
echo "loading country_info..."
time (echo "load data local infile '$BASE/country_info.txt' into table country_info character set 'utf8mb4'" | $MSQL) || exit $?
echo "loading alternateNamesV2"
(
time $MSQL<<EOF
load data local infile '$BASE/alternateNamesV2_nulls.txt'
into table alt_name character set 'utf8mb4'
(id,gid,code,name,is_preferred,is_short,is_colloquial,is_historic)
EOF
) || exit $?
echo "loading allCountries"
(
time $MSQL <<EOF
load data local infile '$BASE/allCountries_nulls.txt'
into table feature character set 'utf8mb4'
(gid, name, @dummy, @dummy, latitude, longitude, fclass, fcode,
country_code, @dummy, admin1_code, admin2_code, admin3_code,
admin4_code, population, elevation, dem, timezone, mod_date)
EOF
) || exit $?
echo 'update continent codes for continent features'
(
time $MSQL <<EOF
update feature set continent_code = 'AF' where gid = 6255146;
update feature set continent_code = 'AS' where gid = 6255147;
update feature set continent_code = 'EU' where gid = 6255148;
update feature set continent_code = 'NA' where gid = 6255149;
update feature set continent_code = 'OC' where gid = 6255150;
update feature set continent_code = 'SA' where gid = 6255151;
update feature set continent_code = 'AN' where gid = 6255152;
EOF
) || exit $?
echo 'set continent codes, parent continents and countries'
(
time $MSQL <<EOF
set max_heap_table_size=128*1024*1024;
CREATE TABLE fp (
country_code char(2) primary key,
continent_code char(2),
country_gid bigint,
continent_gid bigint
) engine=memory
AS
SELECT country_code, continent_code, gid as country_gid,
CASE continent_code
WHEN 'AF' then 6255146
WHEN 'AS' then 6255147
WHEN 'EU' then 6255148
WHEN 'NA' then 6255149
WHEN 'OC' then 6255150
WHEN 'SA' then 6255151
WHEN 'AN' then 6255152
ELSE null
END as continent_gid
FROM country_info;
UPDATE feature fc JOIN fp USING (country_code)
SET fc.continent_code = fp.continent_code,
fc.parent_continent = fp.continent_gid,
fc.parent_country = fp.country_gid
;
DROP TABLE fp;
EOF
) || exit $?
function join_by { local IFS="$1"; shift; echo "$*"; }
keys=(country_code)
keyDefs=("country_code:char(2)")
for a in 1 2 3 4
do
echo "set admin$a parents..."
clen=20
[[ $a = 2 ]] && clen=80
keys+=("admin${a}_code")
keyDefs+=("admin${a}_code:varchar($clen)")
cols=$(join_by , ${keys[@]})
defs=$(join_by , ${keyDefs[@]} | sed -e 's/:/ /g')
(
time $MSQL <<EOF
set max_heap_table_size=128*1024*1024;
create table fp ($defs, gid bigint, primary key fp_pk ($cols)) engine=memory
as select $cols, gid from feature where fcode='ADM$a';
update feature fc join fp using ($cols)
set fc.parent_admin$a = fp.gid;
drop table fp;
EOF
) || exit $?
done
echo DONE