Exercises - Custom Map Reduce Functions

Here are the same exercises which you have solved before. Try to solve these using mapReduce APIs.

  • We will provide you a python script which will have all the above map reduce APIs. Use it as package and solve the below mentioned problems.

  • Create a file with name mymapreduce.py

  • Import and use it from mymapreduce import *.

def myFilter(c, f):
    c_f = []
    for e in c:
        if(f(e)):
            c_f.append(e)
    return c_f

def myMap(c, f):
    c_f = []
    for e in c:
        c_f.append(f(e))
    return c_f

def myReduce(c, f):
    t = c[0]
    for e in c[1:]:
        t = f(t, e)
    return t

def myReduceByKey(p, f):
    p_f = {}
    for e in p:
        if(e[0] in p_f):
            p_f[e[0]] = f(p_f[e[0]], e[1])
        else:
            p_f[e[0]] = e[1]
    return list(p_f.items())

def myJoin(c1, c2):
    c1_dict = dict(c1) # dict with first element as key and second element as value
    results = [] # Initializing empty list
    for c2_item in c2: 
        if c2_item[0] in c1_dict:
            results.append((c2_item[0], (c1_dict[c2_item[0]], c2_item[1])))
    return results
  • Get number of COMPLETE orders placed by each customer

  • Get total number of PENDING or PENDING_PAYMENT orders for the month of 2014 January.

  • Get outstanding amount for each month considering orders with status PAYMENT_REVIEW, PENDING, PENDING_PAYMENT and PROCESSING.