-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.py
More file actions
80 lines (57 loc) · 2.78 KB
/
Copy pathInterface.py
File metadata and controls
80 lines (57 loc) · 2.78 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python2.7
#
# Assignment2 Interface
#
import psycopg2
import os
import sys
# Donot close the connection inside this file i.e. do not perform openconnection.close()
def RangeQuery(ratingsTableName, ratingMinValue, ratingMaxValue, openconnection):
cursor=openconnection.cursor()
new_list=[]
cursor.execute("select count(*) from RangeRatingsMetaData;")
rangecount = int(cursor.fetchone()[0])
for i in range(rangecount):
new_list.append("SELECT 'rangeratingspart" + str(i) + "' AS tablename, userid, movieid, rating FROM rangeratingspart" + str(i) +
" WHERE rating >= " + str(ratingMinValue) + " AND rating <= " + str(ratingMaxValue))
cursor.execute("SELECT PartitionNum FROM RoundRobinRatingsMetadata")
roundpartitions = int(cursor.fetchone()[0])
for i in range(roundpartitions):
new_list.append("SELECT 'roundrobinratingspart" + str(i) + "' AS tablename, userid, movieid, rating FROM roundrobinratingspart" + str(i) +
" WHERE rating >= " + str(ratingMinValue) + " AND rating <= " + str(ratingMaxValue))
op_query = 'SELECT * FROM ({0}) AS T'.format(' UNION ALL '.join(new_list))
print(op_query)
opfile = open('RangeQueryOut.txt', 'w')
write_file = "COPY (" + op_query + ") TO '" + os.path.abspath(opfile.name) + "' (FORMAT text, DELIMITER ',')"
cursor.execute(write_file)
cursor.close()
opfile.close()
def PointQuery(ratingsTableName, ratingValue, openconnection):
cursor = openconnection.cursor()
new_list = []
# Get range partition
cursor.execute("SELECT COUNT(*) FROM RangeRatingsMetadata")
rangecount = int(cursor.fetchone()[0])
# query for range
for i in range(rangecount):
new_list.append("SELECT 'rangeratingspart" + str(i) + "' AS tablename, userid, movieid, rating FROM rangeratingspart"
+ str(i) + " WHERE rating = " + str(ratingValue))
# Get round robin
cursor.execute("SELECT PartitionNum FROM RoundRobinRatingsMetadata")
roundnpartitions = int(cursor.fetchone()[0])
# query for round
for i in range(roundnpartitions):
new_list.append("SELECT 'roundrobinratingspart" + str(i) + "' AS tablename, userid, movieid, rating FROM roundrobinratingspart"
+ str(i) + " WHERE rating = " + str(ratingValue))
op_query = 'SELECT * FROM ({0}) AS T'.format(' UNION ALL '.join(new_list))
opfile = open('PointQueryOut.txt', 'w')
write_file = "COPY (" + op_query + ") TO '" + os.path.abspath(opfile.name) + "' (FORMAT text, DELIMITER ',')"
cursor.execute(write_file)
cursor.close()
opfile.close()
def writeToFile(filename, rows):
f = open(filename, 'w')
for line in rows:
f.write(','.join(str(s) for s in line))
f.write('\n')
f.close()