-
Notifications
You must be signed in to change notification settings - Fork 34
/
03_import_dec_2020_pl94.sh
executable file
·86 lines (72 loc) · 2.77 KB
/
03_import_dec_2020_pl94.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
#!/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 csvkit
git clone git://github.com/censusreporter/census-postgres.git
DATA_DIR=/home/ubuntu/data/dec2020_pl94
# Create the schema
cd /home/ubuntu/census-postgres/dec2020_pl94
psql -v ON_ERROR_STOP=1 -q -c "DROP SCHEMA IF EXISTS dec2020_pl94 CASCADE; CREATE SCHEMA dec2020_pl94;"
# Create import tables
echo "Creating import tables"
psql -v ON_ERROR_STOP=1 -q -f 01_create_import_tables.sql
if [[ $? != 0 ]]; then
echo "Failed creating import tables."
exit 1
fi
for i in `ls ${DATA_DIR}/*zip`; do
unzip -d ${DATA_DIR} $i
done
# Slurp in the actual data
# We're doing the COPY FROM STDIN so we don't have to be a psql superuser
# Only load blocks (SUMLEV 750)
echo "Importing geoheader"
for i in `ls ${DATA_DIR}/*geo2020.pl`; do
echo `basename $i`
# expecting pipe delimited, no header row - but after csvgrep, delim is comma and header has been added
cat $i | csvgrep -H -d \| -c 3 -m 750 | psql -v ON_ERROR_STOP=1 -q -c "COPY dec2020_pl94.geoheader FROM STDIN WITH CSV HEADER ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing geoheader $i."
exit 1
fi
done
# 2020 geoheader files include geoids so don't create a column.
psql -v ON_ERROR_STOP=1 -q -c "CREATE UNIQUE INDEX geoheader_geoid_idx ON dec2020_pl94.geoheader(GEOID);"
psql -v ON_ERROR_STOP=1 -q -c "CREATE INDEX dec2020_geoheader_state_place_idx ON dec2020_pl94.geoheader(state,place);"
echo "Importing sequence 0001"
for i in $(ls ${DATA_DIR}/*12020.pl); do
echo `basename $i`
cat $i | psql -v ON_ERROR_STOP=1 -q -c "COPY dec2020_pl94.seq0001 FROM STDIN WITH CSV DELIMITER '|' ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing sequence 0001 $i."
exit 1
fi
done;
echo "Importing sequence 0002"
for i in $(ls ${DATA_DIR}/*22020.pl); do
echo `basename $i`
cat $i | psql -v ON_ERROR_STOP=1 -q -c "COPY dec2020_pl94.seq0002 FROM STDIN WITH CSV DELIMITER '|' ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing sequence 0002 $i."
exit 1
fi
done;
echo "Importing sequence 0003"
for i in $(ls ${DATA_DIR}/*32020.pl); do
echo `basename $i`
cat $i | psql -v ON_ERROR_STOP=1 -q -c "COPY dec2020_pl94.seq0003 FROM STDIN WITH CSV DELIMITER '|' ENCODING 'latin1';"
if [[ $? != 0 ]]; then
echo "Failed importing sequence 0003 $i."
exit 1
fi
done;
echo "Creating real tables from import tables"
psql -v ON_ERROR_STOP=1 -q -f 02_select_seq_into_tables.sql
if [[ $? != 0 ]]; then
echo "Failed creating real tables from import tables."
# exit 1
fi