forked from luisbelloch/data_processing_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompras_top_ten_countries.py
More file actions
28 lines (21 loc) · 894 Bytes
/
Copy pathcompras_top_ten_countries.py
File metadata and controls
28 lines (21 loc) · 894 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from pyspark import SparkContext
from helpers import *
sc = SparkContext('local', 'compras')
txt = sc.textFile('data/compras_tiny.csv')
no_header = txt.filter(lambda s: not s.startswith(item_fields[0]))
parsed = no_header.map(lambda s: parse_item(s)).cache()
countries_rdd = sc \
.textFile('./data/country_codes.csv') \
.map(lambda c: tuple(reversed(c.split(','))))
join_rdd = parsed \
.filter(lambda i: i.currency_code == 'USD') \
.map(lambda i: (i.country, float(i.item_price))) \
.reduceByKey(lambda a, b: a + b) \
.leftOuterJoin(countries_rdd) \
.sortBy(lambda i: i[1][0], ascending=False)
print(join_rdd.take(10))
# print map(lambda i: (i[0], i[1][1], i[1][0]), join_rdd.take(10))
# join_rdd.saveAsTextFile('./top10countries', 'org.apache.hadoop.io.compress.GzipCodec')