-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmongo2postgres.py
More file actions
executable file
·58 lines (45 loc) · 1.57 KB
/
mongo2postgres.py
File metadata and controls
executable file
·58 lines (45 loc) · 1.57 KB
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
#!/usr/bin/env python3
from pprint import pprint
import time
import sys
import os
import postgres_config
import common.utils as utils
import common.mongo_db as mongo_db
import common.postgres_db as postgres_db
import common.scan as scan
INSERT_NUM = 100
def main():
mdb = mongo_db.Database("SensorDB", "Scan")
pdb = postgres_db.Database(postgres_config.database, \
postgres_config.username, \
postgres_config.password, \
postgres_config.hostname, \
postgres_config.port)
# Purging tables
# Uncomment this if you want to delete all old data
# in the tables (the tables must already be defined)
#utils.log("Purging tables...")
#pdb.purge_tables()
# Go ahead and initialize the tables. No harm if they are
# already initialized.
utils.log("Initializing tables...")
pdb.init_tables()
# Grab the uuids that exist
uuids = pdb.get_uuids()
# Read in each of the scan objects
scan_uuid_lst = []
i = 0
for (full_scan, uuid, version) in mdb.get_scans(uuids=uuids):
i = i + 1
if i % 100 == 0:
pprint("Scan number: {:d}".format(i))
# Actually append the scan to the full scan list
scan_uuid_lst.append((full_scan, uuid, version,))
if len(scan_uuid_lst) >= INSERT_NUM:
pdb.insert_scans(scan_uuid_lst)
scan_uuid_lst = []
if len(scan_uuid_lst) > 0:
pdb.insert_scans(scan_uuid_lst)
if __name__ == '__main__':
main()