标签:
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() cur.execute(‘Drop Table if exists Ratings; CREATE TABLE Ratings(UserID int NOT NULL, movieid int NOT NULL, Rating double precision, CONSTRAINT key PRIMARY KEY (UserID, movieid));‘) with open(ratingsfilepath, "r") as ins: for line in ins: array=line.split("::") cur.execute(‘INSERT INTO Ratings VALUES ({0},{1},{2})‘ .format(array[0],array[1],array[2])) openconnection.commit() cur.close() pass
def rangepartition(ratingstablename, numberofpartitions, openconnection): cur=openconnection.cursor() #part=(5-0.5)/numberofpartitions part=5/numberofpartitions for i in range(0,numberofpartitions): str=‘Drop Table if exists range_part{0};CREATE TABLE range_part{0}(UserID int NOT NULL, movieid int NOT NULL, Rating double precision, PRIMARY KEY (UserID, movieid));‘.format(i) print(str) cur.execute(str) cur.execute(‘select * from {0}‘.format(ratingstablename)) result=cur.fetchall() for row in result: #i=(row[2]-0.01-0.5)/part i=(row[2]-0.01)/part i=int (i) cur.execute(‘INSERT INTO range_part{3} VALUES ({0},{1},{2})‘ .format(row[0],row[1],row[2],i)) cur.execute(‘Drop Table if exists hrange_record;CREATE TABLE hrange_record(n text NOT NULL,part text NOT NULL);‘) cur.execute(‘INSERT INTO hrange_record VALUES ({0},{1})‘ .format(numberofpartitions,part)) openconnection.commit() cur.close() pass
标签:
原文地址:http://www.cnblogs.com/lilyfindjobs/p/4352642.html