Skip to content

Commit 5f53ad1

Browse files
committed
Added de-duplication of the input data, so the database does not get corrupted with multiple identical values
1 parent 20bc205 commit 5f53ad1

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

mlab_mysql_import.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
# Version history
88
# 2012xxxx SF first version
99
# 20120628 AX removed testing for every line, added timing code,
10-
# 20120629 AX added loop over all arguments, exception handling, restructured code
10+
# 20120629 AX added loop over all arguments, exception handling, restructured code, moved processed files to archive or error folder
1111
#
1212
# test:
1313
# cd /DATA
1414
# python scripts/mlab/mlab_mysql_import2.py mlab/clean/glasnost/20090128T000000Z-batch-batch-glasnost-0002.tgz.csv
1515
#
16-
# ToDO: loop over all arguments in sys.argv[0]
17-
# deduplication toevoegen (put in hash, test on hash, clear hash for each file, but keep last entry
18-
# move files naar done directory
19-
# move error files naar error directory
20-
#
16+
# ToDO: v loop over all arguments in sys.argv[0]
17+
# v deduplication toevoegen (put in hash, test on hash, clear hash for each file, but keep last entry
18+
# v move files naar archive directory
19+
# v move error files naar error directory
20+
# v log process and errors
2121

2222
import sys
2323
import re
@@ -62,9 +62,7 @@
6262
#################################################################
6363

6464
def usage():
65-
print "Usage: mlab_mysql_import.py mlab_file.csv"
66-
print "Recursive import can be realised by running:"
67-
print "find . -iname '*.tgz.csv' -exec ./mlab_mysql_import.py {} \;"
65+
print "Usage: mlab_mysql_import3.py mlab_file1.csv [mlab_files.csv ...]"
6866
sys.exit(1)
6967

7068
def extract_destination(filename):
@@ -145,6 +143,14 @@ def get_file_id(cur, filename):
145143
return get_file_id(cur, filename)
146144
return id[0]
147145

146+
def dedup(file_id, table, test_datetime, destination, source_ip):
147+
key = str(file_id) + table + str(test_datetime) + destination + source_ip
148+
if key in deduplookup:
149+
return False
150+
else:
151+
deduplookup[key] = True
152+
return True
153+
148154
# returns True on error, False on correct processing
149155
def process_file(f, filename):
150156
start_time = datetime.now()
@@ -185,7 +191,9 @@ def process_file(f, filename):
185191
# this file has already been read: ABORT WITH ERROR
186192
raise Exception('File entry already exist in db; the file has already been read: ' + filename)
187193
filetest=False
188-
blunt_insert_dbentry(cur, file_id, db_tables[test], test_datetime, destination, source_ip)
194+
# test if we have already done it in this or last filetest
195+
if (dedup(file_id, db_tables[test], test_datetime, destination, source_ip)):
196+
blunt_insert_dbentry(cur, file_id, db_tables[test], test_datetime, destination, source_ip)
189197
end_time = datetime.now()
190198
print 'File done in ' + str(end_time - start_time)
191199
failure = False
@@ -231,8 +239,6 @@ def move_archive(pathname):
231239
f.write(pathname + '\n')
232240

233241

234-
235-
236242
#################################################################
237243
# #
238244
# start of initialisation #
@@ -251,6 +257,11 @@ def move_archive(pathname):
251257
f.write("\nNew batchjob on " + str(datetime.now()))
252258
f.close
253259

260+
# deduplookup is a hash we use for de-duplication of input lines
261+
# maybe it is necessary to purge parts of it during the duration of the import
262+
# but then we have to carefully monitor tests that appear in multiple files
263+
# OR store the last test in a separate global (dirty? yeah, I know)
264+
deduplookup = {}
254265

255266
#################################################################
256267
# #

0 commit comments

Comments
 (0)