forked from Networks-Learning/stackexchange-dump-to-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_into_pg.py
executable file
·285 lines (249 loc) · 8.61 KB
/
load_into_pg.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
import sys
import time
import argparse
import psycopg2 as pg
import row_processor as Processor
import six
# Special rules needed for certain tables (esp. for old database dumps)
specialRules = {
('Posts', 'ViewCount'): "NULLIF(%(ViewCount)s, '')::int"
}
def _makeDefValues(keys):
"""Returns a dictionary containing None for all keys."""
return dict(( (k, None) for k in keys ))
def _createMogrificationTemplate(table, keys):
"""Return the template string for mogrification for the given keys."""
return ( '(' +
', '.join( [ '%(' + k + ')s' if (table, k) not in specialRules else specialRules[table, k]
for k in keys
]
) +
')'
)
def _createCmdTuple(cursor, keys, templ, attribs):
"""Use the cursor to mogrify a tuple of data.
The passed data in `attribs` is augmented with default data (NULLs) and the
order of data in the tuple is the same as in the list of `keys`. The
`cursor` is used toe mogrify the data and the `templ` is the template used
for the mogrification.
"""
defs = _makeDefValues(keys)
defs.update(attribs)
return cursor.mogrify(templ, defs)
def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPassword):
"""Handle the table including the post/pre processing."""
dbFile = mbDbFile if mbDbFile is not None else table + '.xml'
tmpl = _createMogrificationTemplate(table, keys)
start_time = time.time()
try:
pre = open('./sql/' + table + '_pre.sql').read()
post = open('./sql/' + table + '_post.sql').read()
except IOError as e:
six.print_("Could not load pre/post sql. Are you running from the correct path?", file=sys.stderr)
sys.exit(-1)
dbConnectionParam = "dbname={}".format(dbname)
if mbPort is not None:
dbConnectionParam += ' port={}'.format(mbPort)
if mbHost is not None:
dbConnectionParam += ' host={}'.format(mbHost)
# TODO Is the escaping done here correct?
if mbUsername is not None:
dbConnectionParam += ' user={}'.format(mbUsername)
# TODO Is the escaping done here correct?
if mbPassword is not None:
dbConnectionParam += ' password={}'.format(mbPassword)
try:
with pg.connect(dbConnectionParam) as conn:
with conn.cursor() as cur:
try:
with open(dbFile, 'rb') as xml:
# Pre-processing (dropping/creation of tables)
six.print_('Pre-processing ...')
if pre != '':
cur.execute(pre)
conn.commit()
six.print_('Pre-processing took {:.1f} seconds'.format(time.time() - start_time))
# Handle content of the table
start_time = time.time()
six.print_('Processing data ...')
for rows in Processor.batch(Processor.parse(xml), 500):
valuesStr = ',\n'.join(
[ _createCmdTuple(cur, keys, tmpl, row_attribs).decode('utf-8')
for row_attribs in rows
]
)
if len(valuesStr) > 0:
cmd = 'INSERT INTO ' + table + \
' VALUES\n' + valuesStr + ';'
cur.execute(cmd)
conn.commit()
six.print_('Table processing took {:.1f} seconds'.format(time.time() - start_time))
# Post-processing (creation of indexes)
start_time = time.time()
six.print_('Post processing ...')
if post != '':
cur.execute(post)
conn.commit()
six.print_('Post processing took {} seconds'.format(time.time() - start_time))
except IOError as e:
six.print_("Could not read from file {}.".format(dbFile), file=sys.stderr)
six.print_("IOError: {0}".format(e.strerror), file=sys.stderr)
except pg.Error as e:
six.print_("Error in dealing with the database.", file=sys.stderr)
six.print_("pg.Error ({0}): {1}".format(e.pgcode, e.pgerror), file=sys.stderr)
six.print_(str(e), file=sys.stderr)
except pg.Warning as w:
six.print_("Warning from the database.", file=sys.stderr)
six.print_("pg.Warning: {0}".format(str(w)), file=sys.stderr)
#############################################################
parser = argparse.ArgumentParser()
parser.add_argument( 'table'
, help = 'The table to work on.'
, choices = ['Users', 'Badges', 'Posts', 'Tags', 'Votes', 'PostLinks', 'PostHistory', 'Comments']
)
parser.add_argument( '-d', '--dbname'
, help = 'Name of database to create the table in. The database must exist.'
, default = 'stackoverflow'
)
parser.add_argument( '-f', '--file'
, help = 'Name of the file to extract data from.'
, default = None
)
parser.add_argument( '-u', '--username'
, help = 'Username for the database.'
, default = None
)
parser.add_argument( '-p', '--password'
, help = 'Password for the database.'
, default = None
)
parser.add_argument( '-P', '--port'
, help = 'Port to connect with the database on.'
, default = None
)
parser.add_argument( '-H', '--host'
, help = 'Hostname for the database.'
, default = None
)
parser.add_argument( '--with-post-body'
, help = 'Import the posts with the post body. Only used if importing Posts.xml'
, action = 'store_true'
, default = False
)
args = parser.parse_args()
table = args.table
keys = None
if table == 'Users':
keys = [
'Id'
, 'Reputation'
, 'CreationDate'
, 'DisplayName'
, 'LastAccessDate'
, 'WebsiteUrl'
, 'Location'
, 'AboutMe'
, 'Views'
, 'UpVotes'
, 'DownVotes'
, 'ProfileImageUrl'
, 'Age'
, 'AccountId'
]
elif table == 'Badges':
keys = [
'Id'
, 'UserId'
, 'Name'
, 'Date'
]
elif table == 'PostLinks':
keys = [
'Id'
, 'CreationDate'
, 'PostId'
, 'RelatedPostId'
, 'LinkTypeId'
]
elif table == 'Comments':
keys = [
'Id'
, 'PostId'
, 'Score'
, 'Text'
, 'CreationDate'
, 'UserId'
]
elif table == 'Votes':
keys = [
'Id'
, 'PostId'
, 'VoteTypeId'
, 'UserId'
, 'CreationDate'
, 'BountyAmount'
]
elif table == 'Posts':
keys = [
'Id'
, 'PostTypeId'
, 'AcceptedAnswerId'
, 'ParentId'
, 'CreationDate'
, 'Score'
, 'ViewCount'
, 'Body'
, 'OwnerUserId'
, 'LastEditorUserId'
, 'LastEditorDisplayName'
, 'LastEditDate'
, 'LastActivityDate'
, 'Title'
, 'Tags'
, 'AnswerCount'
, 'CommentCount'
, 'FavoriteCount'
, 'ClosedDate'
, 'CommunityOwnedDate'
]
# If the user has not explicitly asked for loading the body, we replace it with NULL
if not args.with_post_body:
specialRules[('Posts', 'Body')] = 'NULL'
elif table == 'Tags':
keys = [
'Id'
, 'TagName'
, 'Count'
, 'ExcerptPostId'
, 'WikiPostId'
]
elif table == 'PostHistory':
keys = [
'Id',
'PostHistoryTypeId',
'PostId',
'RevisionGUID',
'CreationDate',
'UserId',
'Text'
]
elif table == 'Comments':
keys = [
'Id',
'PostId',
'Score',
'Text',
'CreationDate',
'UserId',
]
try:
# Python 2/3 compatibility
input = raw_input
except NameError:
pass
choice = input('This will drop the {} table. Are you sure [y/n]?'.format(table))
if len(choice) > 0 and choice[0].lower() == 'y':
handleTable(table, keys, args.dbname, args.file, args.host, args.port, args.username, args.password)
else:
six.print_("Cancelled.")