forked from P7h/IntroToHadoopAndMR__Udacity_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1Q1_Reducer.py
More file actions
31 lines (21 loc) · 720 Bytes
/
P1Q1_Reducer.py
File metadata and controls
31 lines (21 loc) · 720 Bytes
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
#!/usr/bin/python
# Instead of breaking the sales down by store, instead give us a sales breakdown by product category across all of our stores.
import sys
salesTotal = 0
oldKey = None
# It will be in the format key\tval
# Where key is the store name, val is the sale amount
for line in sys.stdin:
data_mapped = line.strip().split("\t")
if len(data_mapped) != 2:
# Something has gone wrong. Skip this line.
continue
thisKey, thisSale = data_mapped
if oldKey and oldKey != thisKey:
print oldKey, "\t", salesTotal
oldKey = thisKey;
salesTotal = 0
oldKey = thisKey
salesTotal += float(thisSale)
if oldKey != None:
print oldKey, "\t", salesTotal