Advertisement
kieni17

Untitled

Apr 18th, 2020
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. from mrjob.job import MRJob
  2. from mrjob.step import MRStep
  3. from collections import Counter
  4.  
  5. class MR_Ex3(MRJob):
  6.  
  7.     def mapper_get_words(self, _, line):
  8.         # split line into array
  9.         (event_time,event_type,product_id,category_id,category_code,brand,price,user_id,user_session) = line.split(',')
  10.  
  11.         # check if brand has a value and if so yield user_id and brand
  12.         # also check if event_type is purchase
  13.         if brand and event_type == "purchase":
  14.             yield user_id, brand
  15.    
  16.    
  17.     def reducer_count_words(self, user, brands):
  18.         yield  user,Counter(brands).most_common(1)[0][0]
  19.  
  20.  
  21.     def steps(self):
  22.         return [
  23.             MRStep(mapper  = self.mapper_get_words,
  24.                    reducer = self.reducer_count)
  25.         ]
  26.  
  27. if __name__ == '__main__':
  28.     MR_Ex3.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement