Skip to content

Instantly share code, notes, and snippets.

@koushikmln
Last active July 8, 2018 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koushikmln/800dc29318b4373c4396d460b05a1136 to your computer and use it in GitHub Desktop.
Save koushikmln/800dc29318b4373c4396d460b05a1136 to your computer and use it in GitHub Desktop.
Process Order Items CSV to get Order Id, Sub-Total Tuples, Total Amount by Order Id and Revenue Per Order Collection
#Problem Statement 1
#Get (order_id, sub_total) tuple from order items csv using map function.
def getOrderItemTuples(order_items):
return list(map(lambda x: (int(x.split(",")[1]),float(x.split(",")[4])),order_items))
order_items = open("/data/retail_db/order_items/part-00000","r").read().splitlines()
getOrderItemTuples(order_items[:20])
#Problem Statement 2
#Get the total amount for a particular order using map, reduce, filter.
from functools import reduce
def getTotalById(order_items,id):
filtered_list = filter(lambda x: int(x.split(",")[1]) == id,order_items)
subtotal = map(lambda x: float(x.split(",")[4]), filtered_list)
return reduce(lambda x,y: x + y,subtotal)
order_items = open("/data/retail_db/order_items/part-00000","r").read().splitlines()
getTotalById(order_items[:20],5)
#Problem Statement 3
#Get the total amount for all orders using map, reduce, filter, group by.
import itertools as it, sys
from functools import reduce
order_items = open("/data/retail_db/order_items/part-00000","r").read().splitlines()[:50]
order_subtotal = list(map(lambda x: (int(x.split(",")[1]),float(x.split(",")[4])),order_items))
#sorted values should be passed in groupby
iterator = it.groupby(sorted(order_subtotal),lambda x: x[0])
#list(map(lambda x: (x[0], list(x[1])), iterator)) to view the groupby iterator
revenuePerOrder = map(lambda x: (x[0], reduce(lambda a,b: a + b, map(lambda v: v[1], x[1]))), iterator)
print(list(revenuePerOrder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment