-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA5T2.py
134 lines (99 loc) · 3.29 KB
/
A5T2.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
import sqlite3
from pymongo import MongoClient
'''
A5T2.py
Created by Brad Conklin, Mashiad Hasan and Klyde Pausang
For CMPUT 291 - Assignment 5
University of Alberta
Winter 2021
'''
connection = None
cursor = None
def connect_sqlite(path):
'''
Connect to the sqlite3 database located at path
'''
global connection, cursor
connection = sqlite3.connect(path)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute(' PRAGMA foreign_keys=ON; ')
connection.commit()
return
def setup_mongoDB():
global client, db, listings
# Connect to a specific port
client = MongoClient('mongodb://localhost:27012')
# Create the A5db database on server.
db = client["A5db"]
collist = db.list_collection_names()
if "listings" in collist:
print("The collection listings exists.")
# Create or open the collection in the db
listings = db["listings"]
# delete all previous entries in the listings
# specify no condition.
listings.delete_many({})
def create_collection():
'''
use a MongoDB database called A5db and create within the database
a single collection where all the reviews associated to one given
listing are to be embedded within that one listing
'''
'''
get all listing ids
'''
cursor.execute('''Select id From Listings''')
rows = cursor.fetchall()
listing_ids = []
for each in rows:
listing_ids.append(int(each["id"]))
'''
all the reviews associated to a given listing are to be embedded
within that one listing
'''
for i in range (0, len(listing_ids)):
cursor.execute('''Select *
From Listings L
Where L.id = :id''',{"id":listing_ids[i]})
listing_info = [dict(row) for row in cursor.fetchall()]
for dict1 in listing_info:
listing_info = dict1
cursor.execute('''Select *
From Reviews R
Where R.listing_id = :id''',{"id":listing_ids[i]})
reviews_info = [dict(row) for row in cursor.fetchall()]
listing_info['reviews'] = reviews_info
'''
insert the entry for the given listing
'''
listings.insert_one(listing_info)
return
def test():
'''
testing to ensure that the ETL process works as expected
'''
c2 = db.listings.aggregate([{"$group": {"_id": "null", 'min': {'$min': "$host_id"},
'max': {'$max': "$host_id"},
'avg': {'$avg': "$host_id"}, 'count': {'$sum': 1}}} ])
result = list(c2)
print(result)
c3 = db.listings.aggregate([{'$unwind': "$reviews"},
{'$group': {'_id': 'null', 'min': {'$min': "$reviews.id"},
'max': {'$max': "$reviews.id"}, 'avg': {'$avg': "$reviews.id"},
'count': {'$sum': 1}}}])
result2 = list(c3)
print(result2)
return
def main():
global connection, cursor
sqlite_path = "./A5.db"
connect_sqlite(sqlite_path)
setup_mongoDB()
create_collection()
test()
connection.commit()
connection.close()
return
if __name__ == "__main__":
main()