GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — IntroToMapReduceAndSpark/python/task2.py at mainline · krishna555/IntroToMapReduceAndSpark

Expand file tree

/

task2.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
124 lines (101 loc) · 4.91 KB
/

task2.py

Copy path

File metadata and controls

124 lines (101 loc) · 4.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
from pyspark import SparkContext, SparkConf
import sys
from collections import defaultdict
class Task2:
def process_no_spark(self, review_file_path, business_file_path, n):
def readFile(file_path, required_keys):
with open(file_path) as readF:
json_records = []
for line in readF:
line = line.strip()
json_record = json.loads(line)
current_record = {}
for key in required_keys:
current_record[key] = json_record[key]
json_records.append(current_record)
readF.close()
return json_records
review_records = readFile(review_file_path, ['business_id', 'stars'])
business_records = readFile(business_file_path, ['business_id', 'categories'])
bid_to_st = defaultdict()
# Extract Business Id and Stars
for review_obj in review_records:
business_id = review_obj['business_id']
stars = review_obj['stars']
if business_id not in bid_to_st:
bid_to_st[business_id] = [stars]
else:
bid_to_st[business_id].append(stars)
# Extract Business Id and Categories
business_id_to_categories = defaultdict(list)
for business_obj in business_records:
categories_string = business_obj['categories']
business_id = business_obj['business_id']
if categories_string:
categories = categories_string.split(',')
for category in categories:
business_id_to_categories[business_id].append(category.strip())
keys = bid_to_st.keys()
category_to_stars = defaultdict(list)
star_sum = defaultdict(int)
for business_id in bid_to_st.keys():
stars = bid_to_st[business_id]
if business_id in business_id_to_categories:
categories = business_id_to_categories[business_id]
for category in categories:
for star in stars:
category_to_stars[category].append(star)
res = defaultdict()
ans = []
for category in category_to_stars.keys():
res[category] = sum(category_to_stars[category])/len(category_to_stars[category])
ans.append((category, res[category]))
ans = sorted(ans, key=lambda x: (-x[1], x[0]))[:n]
return ans
def process_with_spark(self, sc, review_file_path, business_file_path, n):
review_data_rdd = sc.textFile(review_file_path).map(lambda row: json.loads(row))
business_data_rdd = sc.textFile(business_file_path).map(lambda row: json.loads(row))
review_bid_stars_rdd = review_data_rdd.map(lambda json_record: (json_record['business_id'], json_record['stars']))
business_bid_categories_rdd = business_data_rdd.map(lambda json_record: (json_record['business_id'], json_record['categories']))
stars_and_count_rdd = review_bid_stars_rdd.groupByKey() \
.map(lambda bid_and_stars: (bid_and_stars[0], (sum(bid_and_stars[1]), len(bid_and_stars[1]))))
def all_categories_sanitizer(categories):
res = []
category_list = categories.split(',')
for category in category_list:
res.append(category.strip())
return res
categories_rdd = business_bid_categories_rdd \
.filter(lambda bid_and_categories: bid_and_categories[1] is not None and bid_and_categories[1] != '')
result = stars_and_count_rdd.join(categories_rdd) \
.map(lambda tup: (all_categories_sanitizer(tup[1][1]), tup[1][0])) \
.flatMap(lambda tup: [(category, tup[1]) for category in tup[0]]) \
.reduceByKey(lambda tup1, tup2: (tup1[0] + tup2[0], tup1[1] + tup2[1])) \
.mapValues(lambda sum_and_count_tuple: sum_and_count_tuple[0] / sum_and_count_tuple[1]) \
.takeOrdered(n, key=lambda tup: (-tup[1], tup[0]))
return result
def main(self):
conf = SparkConf().setAppName("Task2").setMaster("local[*]")
sc = SparkContext(conf = conf).getOrCreate()
sc.setLogLevel("OFF")
review_file_path = sys.argv[1]
business_file_path = sys.argv[2]
output_file_path = sys.argv[3]
spark_mode = sys.argv[4]
n = int(sys.argv[5])
if spark_mode == "spark":
output = self.process_with_spark(sc, review_file_path, business_file_path, n)
else:
output = self.process_no_spark(review_file_path, business_file_path, n)
res = []
for tup in output:
res.append([tup[0], round(tup[1], 1) ])
output_json = {}
output_json['result'] = res
with open(output_file_path, "w+") as of:
json.dump(output_json, of)
of.close()
if __name__ == "__main__":
task2 = Task2()
task2.main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.