-
Notifications
You must be signed in to change notification settings - Fork 3
/
datapartitioning.py
319 lines (249 loc) · 12 KB
/
datapartitioning.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/python2.7
#
# This project done as part of CSE 512 Fall 2017
#
__author__ = "Prashant Gonarkar"
__version__ = "v0.1"
__email__ = "[email protected]"
import psycopg2
import csv
import os
DATABASE_NAME = 'dds_assgn2'
USER_ID_COLNAME = 'userId'
MOVIE_ID_COLNAME = 'movieId'
RATING_COLNAME = 'rating'
RANGE_TABLE_PREFIX = 'range_part'
RROBIN_TABLE_PREFIX = 'rrobin_part'
RATINGS_TABLE = 'ratings'
def getopenconnection(user='postgres', password='1234', dbname='dds_assgn1'):
return psycopg2.connect("dbname='" + dbname + "' user='" + user + "' host='localhost' password='" + password + "'")
def loadratings(ratingstablename, ratingsfilepath, openconnection):
cur = openconnection.cursor()
createtable(ratingstablename,cur)
# For inserting data COPY method of postgres is used being the fastest among other approaches.
# But input data has multi-byte characters as delimiters (::) which postgres doesn't support
# hence input data is preprocessed and converted into tab seprated data
# Preprocessing the data before importing into postgres
try:
with open(ratingsfilepath) as finput, open('/tmp/_output_zxyvk.csv','w') as foutput:
csv_output = csv.writer(foutput,delimiter='\t')
for lines in finput:
line = lines.rstrip('\n')
splits = line.split("::")
#print("value of splits: ",splits)
csv_output.writerow(splits[0:3])
except Exception as ex:
print("File processing error: ",ex)
with open('/tmp/_output_zxyvk.csv') as datafile:
try:
cur.copy_from(datafile,ratingstablename )
openconnection.commit()
cur.close()
except Exception as ex:
print("Failed to copy file in database: ",ex)
# removing temporary intermediate file created while data preprocessing
os.remove('/tmp/_output_zxyvk.csv')
return
def rangepartition(ratingstablename, numberofpartitions, openconnection):
# find max and min to get the range but here range is being given.
# As given range is given as [0-5]
ratinglowerbound = 0.0
ratingupperbound = 5.0
partitioninterval = abs(ratingupperbound-ratinglowerbound) / numberofpartitions
cur = openconnection.cursor()
for i in range( 0, numberofpartitions ):
partitiontablename = RANGE_TABLE_PREFIX + repr(i)
createtable(partitiontablename,cur)
# upper and lower bounds for created partition table
lowerbound = i * partitioninterval
upperbound = lowerbound + partitioninterval
# inserting values according to range in table
if lowerbound == ratinglowerbound:
query = " INSERT INTO {0} SELECT * FROM {1} WHERE {2} >= {3} and {2} <= {4}".format( partitiontablename,
ratingstablename,
RATING_COLNAME,
lowerbound,
upperbound )
else:
query = " INSERT INTO {0} SELECT * FROM {1} WHERE {2} > {3} and {2} <= {4}".format( partitiontablename,
ratingstablename,
RATING_COLNAME,
lowerbound,
upperbound )
cur.execute(query)
openconnection.commit()
print("Created partition table: ",partitiontablename)
pass
def roundrobinpartition(ratingstablename, numberofpartitions, openconnection):
# create round robin partitions
modvalue = 0
cur = openconnection.cursor()
for i in range( 0, numberofpartitions ):
partitiontablename = RROBIN_TABLE_PREFIX + repr(i)
createtable(partitiontablename,cur)
print("partitiontablename: ",partitiontablename)
# modvalue acts as partition selector in query
# e.g for modvalue 1 with no of partitions 4, the query will select rows number 1, 5, 9, ..
if (i != (numberofpartitions - 1)):
modvalue = i + 1;
else:
modvalue = 0;
print("mod value ",modvalue)
try:
query = "INSERT INTO {0} " \
"SELECT {1},{2},{3} " \
"FROM (SELECT ROW_NUMBER() OVER() as row_number,* FROM {4}) as foo " \
"WHERE MOD(row_number,{5}) = cast ('{6}' as bigint) ".format(partitiontablename,USER_ID_COLNAME, MOVIE_ID_COLNAME,
RATING_COLNAME, ratingstablename, numberofpartitions, modvalue)
cur.execute(query)
openconnection.commit()
except Exception as ex:
print(ex)
pass
def roundrobininsert(ratingstablename, userid, itemid, rating, openconnection):
# Round Robin insert approach: start comparing count of adjacent two tables
# if all tables have same count then insert into first table, and if next table
# has less count than previous table then insert into next table
#
cur = openconnection.cursor()
# calculate number of partitions
partitioncount = tablecount(cur, RROBIN_TABLE_PREFIX)
print ('partition count ', partitioncount)
partitiontoinsert = 0
previouscount = countrowsintable(cur, RROBIN_TABLE_PREFIX + repr(0) )
for i in range(1,partitioncount):
nextcount = countrowsintable(cur, RROBIN_TABLE_PREFIX + repr(i) )
if ( nextcount < previouscount ):
partitiontoinsert = i
break
# inserting in ratings table
query = " INSERT INTO {0} VALUES ({1}, {2}, {3})".format( ratingstablename,
userid, itemid, rating )
cur.execute(query)
# inserting in appropriate round robin partition
query = " INSERT INTO {0} VALUES ({1}, {2}, {3})".format( RROBIN_TABLE_PREFIX+repr(partitiontoinsert),
userid, itemid, rating )
cur.execute(query)
openconnection.commit()
print("Inserted value in partition: ",RROBIN_TABLE_PREFIX+repr(partitiontoinsert))
pass
def rangeinsert(ratingstablename, userid, itemid, rating, openconnection):
cur = openconnection.cursor()
# calculate number of partitions
partitioncount = tablecount(cur, RANGE_TABLE_PREFIX)
print ('partition count ', partitioncount )
# As given range is given as [0-5]
ratinglowerbound = 0.0
ratingupperbound = 5.0
partitioninterval = abs(ratingupperbound-ratinglowerbound) / partitioncount
for i in range( 0, partitioncount):
# upper and lower bounds for created partition table
lowerbound = i * partitioninterval
upperbound = lowerbound + partitioninterval
if lowerbound == ratinglowerbound:
if (rating >= lowerbound) and (rating <= upperbound):
break
elif (rating > lowerbound) and (rating <= upperbound):
break
partitiontoinsert = i
# inserting in ratings table
query = " INSERT INTO {0} VALUES ({1}, {2}, {3})".format( ratingstablename,
userid, itemid, rating )
cur.execute(query)
# inserting in appropriate range partition table
query = " INSERT INTO {0} VALUES ({1}, {2}, {3})".format( RANGE_TABLE_PREFIX + repr(partitiontoinsert),
userid, itemid, rating )
cur.execute(query)
openconnection.commit()
print("Inserted value in partition: ", RANGE_TABLE_PREFIX + repr(partitiontoinsert))
pass
def deletepartitionsandexit(openconnection):
cur = openconnection.cursor()
# delete range paritions
partitioncount = tablecount(cur, RANGE_TABLE_PREFIX)
print ('partition count %s', partitioncount)
for i in range(0, partitioncount):
partitionname = RANGE_TABLE_PREFIX + repr(i)
cur.execute('DROP TABLE IF EXISTS {0} CASCADE'.format(partitionname))
openconnection.commit()
# delete round robin paritions
partitioncount = tablecount(cur, RROBIN_TABLE_PREFIX)
print ('partition count %s', partitioncount)
for i in range(0, partitioncount):
partitionname = RROBIN_TABLE_PREFIX + repr(i)
cur.execute('DROP TABLE IF EXISTS {0} CASCADE'.format(partitionname))
openconnection.commit()
# delete ratings parition
cur.execute('DROP TABLE IF EXISTS {0} CASCADE'.format(RATINGS_TABLE))
openconnection.commit()
def tablecount(cur, tableprefix):
query = "SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema = 'public' AND table_name LIKE '{0}%';".format(tableprefix)
cur.execute(query)
partitioncount = int(cur.fetchone()[0])
return partitioncount
def countrowsintable( cur, tablename):
query = "SELECT count(*) FROM {0}".format(tablename)
cur.execute(query)
count = int(cur.fetchone()[0])
return count
def createtable(tablename,cursor):
try:
query = "CREATE TABLE {0} ( {1} integer, {2} integer,{3} real);".format(tablename,
USER_ID_COLNAME,MOVIE_ID_COLNAME,RATING_COLNAME)
cursor.execute(query)
except Exception as ex:
print("Failed to create table: ",ex)
print("Created table: ",tablename)
def create_db(dbname):
"""
We create a DB by connecting to the default user and database of Postgres
The function first checks if an existing database exists for a given name, else creates it.
:return:None
"""
# Connect to the default database
con = getopenconnection(dbname='postgres')
con.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
# Check if an existing database with the same name exists
cur.execute('SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname=\'%s\'' % (dbname,))
count = cur.fetchone()[0]
if count == 0:
cur.execute('CREATE DATABASE %s' % (dbname,)) # Create the database
else:
print 'A database named {0} already exists'.format(dbname)
# Clean up
cur.close()
con.close()
# Middleware
def before_db_creation_middleware():
# Use it if you want to
pass
def after_db_creation_middleware(databasename):
# Use it if you want to
pass
def before_test_script_starts_middleware(openconnection, databasename):
# Use it if you want to
pass
def after_test_script_ends_middleware(openconnection, databasename):
# Use it if you want to
pass
if __name__ == '__main__':
try:
# Use this function to do any set up before creating the DB, if any
before_db_creation_middleware()
create_db(DATABASE_NAME)
# Use this function to do any set up after creating the DB, if any
after_db_creation_middleware(DATABASE_NAME)
with getopenconnection() as con:
# Use this function to do any set up before I starting calling your functions to test, if you want to
before_test_script_starts_middleware(con, DATABASE_NAME)
# Here is where I will start calling your functions to test them. For example,
loadratings('ratings.dat', con)
# ###################################################################################
# Anything in this area will not be executed as I will call your functions directly
# so please add whatever code you want to add in main, in the middleware functions provided "only"
# ###################################################################################
# Use this function to do any set up after I finish testing, if you want to
after_test_script_ends_middleware(con, DATABASE_NAME)
except Exception as detail:
print "OOPS! This is the error ==> ", detail