-
Notifications
You must be signed in to change notification settings - Fork 34
/
03_import_acs_2015_1yr.sh
executable file
·104 lines (90 loc) · 3.15 KB
/
03_import_acs_2015_1yr.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
#!/bin/bash
if [ -z $PGHOST ]; then
echo "You must set PGHOST environment variable to the hostname of the PostgreSQL server to operate on."
exit 1
fi
# Clone the import scripts from git
cd /home/ubuntu
sudo apt-get -y install git
git clone git://github.com/censusreporter/census-postgres.git
# Create the schema
cd /home/ubuntu/census-postgres/acs2015_1yr
psql -d census -h $PGHOST -U census -c "DROP SCHEMA IF EXISTS acs2015_1yr CASCADE; CREATE SCHEMA acs2015_1yr;"
# Create import tables
echo "Creating geoheader"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f create_geoheader.sql
if [[ $? != 0 ]]; then
echo "Failed creating geoheader."
exit 1
fi
echo "Creating geoheader comments"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f geoheader_comments.sql
if [[ $? != 0 ]]; then
echo "Failed creating geoheader comments."
exit 1
fi
echo "Creating temp geoheader"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f create_tmp_geoheader.sql
if [[ $? != 0 ]]; then
echo "Failed creating temp geoheader."
exit 1
fi
echo "Creating temp import tables"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f create_import_tables.sql
if [[ $? != 0 ]]; then
echo "Failed creating temp import tables."
exit 1
fi
# Slurp in the actual data
echo "Importing geoheader"
cat /mnt/tmp/acs2015_1yr/g20151*txt | psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -c "COPY acs2015_1yr.tmp_geoheader FROM STDIN WITH ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing geoheader."
exit 1
fi
echo "Parsing geoheader"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f parse_tmp_geoheader.sql
if [[ $? != 0 ]]; then
echo "Failed parsing geoheader."
exit 1
fi
echo "Importing sequences"
for s in $(seq -f "%04g" 1 165)
do
echo "Importing sequence $s"
cat /mnt/tmp/acs2015_1yr/e20151[a-z][a-z]${s}*txt | psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -c "COPY acs2015_1yr.tmp_seq${s} FROM STDIN WITH CSV ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing sequences."
exit 1
fi
cat /mnt/tmp/acs2015_1yr/m20151[a-z][a-z]${s}*txt | psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -c "COPY acs2015_1yr.tmp_seq${s}_moe FROM STDIN WITH CSV ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing sequences."
exit 1
fi
done
echo "Storing tables"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f store_by_tables.sql
if [[ $? != 0 ]]; then
echo "Failed storing tables."
exit 1
fi
echo "Inserting into tables"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f insert_into_tables.sql # This takes ~5 minutes
if [[ $? != 0 ]]; then
echo "Failed inserting into tables."
exit 1
fi
echo "Creating views"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f view_stored_by_tables.sql
if [[ $? != 0 ]]; then
echo "Failed creating views."
exit 1
fi
# Drop temp tables
echo "Dropping import tables"
psql -d census -h $PGHOST -U census -v ON_ERROR_STOP=1 -q -f drop_import_tables.sql
if [[ $? != 0 ]]; then
echo "Failed dropping import tables."
exit 1
fi