Created
August 16, 2012 20:04
-
-
Save marcelcaraciolo/3373168 to your computer and use it in GitHub Desktop.
mapreduce_job1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def group_by_user_rating(self, key, line): | |
""" | |
Emit the user_id and group by their ratings (item and rating) | |
17 70,3 | |
35 21,1 | |
49 19,2 | |
49 21,1 | |
49 70,4 | |
87 19,1 | |
87 21,2 | |
98 19,2 | |
""" | |
user_id, item_id, rating = line.split('|') | |
#yield (item_id, int(rating)), user_id | |
#yield item_id, (user_id, int(rating)) | |
yield user_id, (item_id, float(rating)) | |
#yield (user_id, item_id), int(rating) | |
def count_ratings_users_freq(self, user_id, values): | |
""" | |
For each user, emit a row containing their "postings" | |
(item,rating pairs) | |
Also emit user rating sum and count for use later steps. | |
17 1,3,(70,3) | |
35 1,1,(21,1) | |
49 3,7,(19,2 21,1 70,4) | |
87 2,3,(19,1 21,2) | |
98 1,2,(19,2) | |
""" | |
item_count = 0 | |
item_sum = 0 | |
final = [] | |
for item_id, rating in values: | |
item_count += 1 | |
item_sum += rating | |
final.append((item_id, rating)) | |
yield user_id, (item_count, item_sum, final) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment