-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchasedBrands.py
51 lines (34 loc) · 1.12 KB
/
purchasedBrands.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
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 28 12:19:10 2014
@author: redaal-bahrani
"""
from mrjob.job import MRJob
class CommaValueProtocol(object):
def write(self, key, values):
return ','.join(str(v) for v in values)
class PurchasedBrands(MRJob):
OUTPUT_PROTOCOL = CommaValueProtocol
def mapper(self, key, line):
"""
Emit the id and brands purchased
86246 15343
86246 55172
86246 3830
"""
user_id,chain,dept,category,company,brand,sdate,productsize,productmeasure,purchasequantity,purchaseamount = line.split(',')
yield user_id, brand
def reducer(self, user_id, values):
"""
For each user_id, emit a row containing their purchased brands
user_id, brands
86246,15343,55172,3830
"""
purchased_brands = []
for brand in values:
purchased_brands.append(brand)
purchased_brands = list(sorted(set(purchased_brands)))
purchased_brands.insert(0,user_id)
yield None, (purchased_brands)
if __name__ == '__main__':
PurchasedBrands.run()