|
| 1 | +####Movie Lens 1m### |
| 2 | +#Tests for fixed bugs (Explain, Explain Analyze, ..)# |
| 3 | + |
| 4 | + |
| 5 | +import psycopg2 |
| 6 | +import sys |
| 7 | +import time |
| 8 | +import os |
| 9 | +def main(): |
| 10 | + host = raw_input("Enter the host address of postgresql: ") |
| 11 | + |
| 12 | + dbname = raw_input("Enter the database name: ") |
| 13 | + |
| 14 | + user = raw_input("Enter the username of postgresql: ") |
| 15 | + |
| 16 | + password = raw_input("Enter the password of postgresql: ") |
| 17 | + |
| 18 | + #First data set |
| 19 | + UserDataPath = raw_input("Enter the abs path for the first set of user data(.dat file): ") |
| 20 | + |
| 21 | + ItemDataPath = raw_input("Enter the abs path for the first set of item data(.dat file): ") |
| 22 | + |
| 23 | + RatingDataPath = raw_input("Enter the abs path for the first set of ratings data(.dat file): ") |
| 24 | + |
| 25 | + #Second recommender data set path |
| 26 | + |
| 27 | + RatingDataPath2 = raw_input("Enter the abs path for the MoiveTweets ratings data(.csv file): ") |
| 28 | + |
| 29 | + |
| 30 | + #Define our connection string |
| 31 | + conn_string = "host='"+host+"' dbname='"+dbname+"' user='"+user+"' password='"+password+"'" |
| 32 | + |
| 33 | + # print the connection string we will use to connect |
| 34 | + print "Connecting to database\n ->%s" % (conn_string) |
| 35 | + |
| 36 | + # get a connection, if a connect cannot be made an exception will be raised here |
| 37 | + conn = psycopg2.connect(conn_string) |
| 38 | + cursor = conn.cursor() |
| 39 | + |
| 40 | + print "Data copying from .csv file to postgresql database" |
| 41 | + |
| 42 | + import os |
| 43 | + path = os.getcwd() |
| 44 | + |
| 45 | + executionStart = time.time() |
| 46 | + |
| 47 | + cursor.execute(" set client_encoding = LATIN1;"); |
| 48 | + conn.commit() |
| 49 | + |
| 50 | + cursor.execute("create table if not exists users( userid int, age varchar, gender varchar, job varchar, zipcode varchar);"); |
| 51 | + conn.commit() |
| 52 | + executionTime = time.time() - executionStart |
| 53 | + print "\n Execution time is :-" |
| 54 | + print executionTime |
| 55 | + |
| 56 | + executionStart = time.time() |
| 57 | + query = "COPY users(userid,gender,age,job,zipcode) from "+UserDataPath+"DELIMITERS ';';" |
| 58 | + cursor.execute(query); |
| 59 | + conn.commit() |
| 60 | + executionTime = time.time() - executionStart |
| 61 | + print "\n Execution time is :-" |
| 62 | + print executionTime |
| 63 | + |
| 64 | + cursor.execute("create table if not exists moive( itemid int, name varchar, genre varchar);"); |
| 65 | + conn.commit() |
| 66 | + |
| 67 | + query = "COPY moive(itemid,name,genre) from "+ItemDataPath+" DELIMITERS ';';" |
| 68 | + cursor.execute(query); |
| 69 | + conn.commit() |
| 70 | + |
| 71 | + |
| 72 | + cursor.execute("create table if not exists ratings ( userid int, itemid int, rating real ,garbage varchar);"); |
| 73 | + conn.commit() |
| 74 | + |
| 75 | + query = "COPY ratings(userid,itemid,rating,garbage) from "+RatingDataPath+" DELIMITERS ';';" |
| 76 | + cursor.execute(query); |
| 77 | + conn.commit() |
| 78 | + |
| 79 | + #Second recommender informations |
| 80 | + cursor.execute("create table if not exists gsratings (userid int, itemid int, rating int);"); |
| 81 | + conn.commit() |
| 82 | + executionTime = time.time() - executionStart |
| 83 | + |
| 84 | + query = "copy gsratings(userid, itemid, rating) from "+RatingDataPath2+" DELIMITER ':';" |
| 85 | + cursor.execute(query); |
| 86 | + conn.commit() |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + print "Connected!\n" |
| 92 | + |
| 93 | + ############### |
| 94 | + print "\n \n Creating Recommender for join of two recommendation queries.." |
| 95 | + executionStart = time.time() |
| 96 | + cursor.execute("CREATE RECOMMENDER mtitemcos on gsratings Users FROM userid Items FROM itemid Events FROM rating Using ItemCosCF;"); |
| 97 | + conn.commit() |
| 98 | + executionTime = time.time() - executionStart |
| 99 | + print "\n" |
| 100 | + print (" Execution time is :-", executionTime) |
| 101 | + |
| 102 | + |
| 103 | + ############### |
| 104 | + |
| 105 | + print "Recommendation query being shooted with ItemCosCF technique" |
| 106 | + |
| 107 | + ############### |
| 108 | + |
| 109 | + print "\n \n Creating Recommender.." |
| 110 | + executionStart = time.time() |
| 111 | + cursor.execute("CREATE RECOMMENDER mlRecItemCos on ratings Users FROM userid Items FROM itemid Events FROM rating Using ItemCosCF;"); |
| 112 | + conn.commit() |
| 113 | + executionTime = time.time() - executionStart |
| 114 | + print " Execution time is :-" |
| 115 | + print executionTime |
| 116 | + |
| 117 | + ############### |
| 118 | + |
| 119 | + print "\n \n Explain the selection of movie for single user.." |
| 120 | + executionStart = time.time() |
| 121 | + cursor.execute("explain select itemid from ratings RECOMMEND itemid to userid ON rating Using ItemCosCF where userid =21;"); |
| 122 | + conn.commit() |
| 123 | + executionTime = time.time() - executionStart |
| 124 | + print " Execution time is :-" |
| 125 | + print executionTime |
| 126 | + |
| 127 | + ############### |
| 128 | + |
| 129 | + print "\n \n Explain Analyze for the selection of movie for single user.." |
| 130 | + executionStart = time.time() |
| 131 | + cursor.execute("explain analyze select itemid from ratings RECOMMEND itemid to userid ON rating Using ItemCosCF where userid =21;"); |
| 132 | + conn.commit() |
| 133 | + executionTime = time.time() - executionStart |
| 134 | + print " Execution time is :-" |
| 135 | + print executionTime |
| 136 | + |
| 137 | + ################ |
| 138 | + |
| 139 | + print "\n \n Explain the Join Query.." |
| 140 | + executionStart = time.time() |
| 141 | + cursor.execute("explain select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' ;"); |
| 142 | + conn.commit() |
| 143 | + executionTime = time.time() - executionStart |
| 144 | + print " Execution time is :-" |
| 145 | + print executionTime |
| 146 | + |
| 147 | + ################ |
| 148 | + |
| 149 | + print "\n \n Explain Analyze the Join Query.." |
| 150 | + executionStart = time.time() |
| 151 | + cursor.execute("explain analyze select r.itemid, i.name, i,genre, r.rating , r.userid, b.age from ratings r, moive i, users b Recommend r.itemid to r.userid On r.rating Using ItemCosCF where r.userid = 1 and r.userid = b.userid and r.itemid = i.itemid AND i.genre ILIKE '%action%' ;"); |
| 152 | + conn.commit() |
| 153 | + executionTime = time.time() - executionStart |
| 154 | + print " Execution time is :-" |
| 155 | + print executionTime |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + ################ |
| 160 | + |
| 161 | + print "\n \n Join of Two Recommendations.." |
| 162 | + executionStart = time.time() |
| 163 | + cursor.execute("select t1.itemid, t1.userid, t2.itemid, t2.userid FROM (select * from ratings r Recommend r.itemid TO r.userid ON r.rating USING ItemCosCF where r.userid =15 limit 10) t1 join (select * from gsratings g RECOMMEND g.itemid TO g.userid ON g.rating USING ItemCosCF where g.userid between 10 and 20 AND g.itemid between 90000 and 100000) t2 ON t1.userid =t2.userid limit 10;"); |
| 164 | + conn.commit() |
| 165 | + executionTime = time.time() - executionStart |
| 166 | + print " Execution time is :-" |
| 167 | + print executionTime |
| 168 | + |
| 169 | + ############### |
| 170 | + |
| 171 | + print "\n \n Union of Two Recommendations.." |
| 172 | + executionStart = time.time() |
| 173 | + cursor.execute("(select r.userid from ratings r RECOMMEND r.itemid TO r.userid ON r.rating USING ItemCosCF where r.userid =1 ) union (select g.userid from gsratings g RECOMMEND g.itemid TO g.userid ON g.rating USING ItemCosCF where g.userid =21);"); |
| 174 | + conn.commit() |
| 175 | + executionTime = time.time() - executionStart |
| 176 | + print "Execution time is :-" |
| 177 | + print executionTime |
| 178 | + |
| 179 | + ############### |
| 180 | + |
| 181 | + print "\n \n Recommender in subselect" |
| 182 | + executionStart = time.time() |
| 183 | + cursor.execute("select u.age, sub.rating from users u, (select * FROM ratings r RECOMMEND r.itemid TO r.userid ON r.rating USING ItemCosCF WHERE r.itemid in (1,2,3) limit 10) sub WHERE u.userid = sub.userid;"); |
| 184 | + conn.commit() |
| 185 | + executionTime = time.time() - executionStart |
| 186 | + print " Execution time is :-" |
| 187 | + print executionTime |
| 188 | + |
| 189 | + ############### |
| 190 | + |
| 191 | + print "\n \n Recommender and IN" |
| 192 | + executionStart = time.time() |
| 193 | + cursor.execute("select * from moive m where m.itemid IN (SELECT r.itemid FROM ratings r RECOMMEND r.itemid TO r.userid ON r.rating USING ItemCosCF WHERE r.userid=1 limit 10);"); |
| 194 | + conn.commit() |
| 195 | + executionTime = time.time() - executionStart |
| 196 | + print " Execution time is :-" |
| 197 | + print executionTime |
| 198 | + |
| 199 | + ################ |
| 200 | + |
| 201 | + cursor.execute("drop table ratings;"); |
| 202 | + conn.commit() |
| 203 | + |
| 204 | + cursor.execute("drop table users;"); |
| 205 | + conn.commit() |
| 206 | + |
| 207 | + cursor.execute("drop table moive;"); |
| 208 | + conn.commit() |
| 209 | + |
| 210 | + cursor.execute("drop table gsratings;"); |
| 211 | + conn.commit() |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + main() |
0 commit comments