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/task3.py at mainline · krishna555/IntroToMapReduceAndSpark

Expand file tree

/

task3.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
38 lines (32 loc) · 1.23 KB
/

task3.py

Copy path

File metadata and controls

38 lines (32 loc) · 1.23 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
from pyspark import SparkContext, SparkConf
import sys
import json
class Task3:
def main(self):
def bid_partitioner(bid):
key = bid[0] + bid[-1]
return hash(key)
review_file_path = sys.argv[1]
output_file_path = sys.argv[2]
partition_type = sys.argv[3]
num_partitions = int(sys.argv[4])
n = int(sys.argv[5])
sc = SparkContext.getOrCreate()
sc.setLogLevel("OFF")
review_data_rdd = sc.textFile(review_file_path).map(lambda row: json.loads(row))
bid_cnt_rdd = review_data_rdd.map(lambda json_record: (json_record["business_id"], 1))
if partition_type != 'default':
bid_cnt_rdd = bid_cnt_rdd.partitionBy(num_partitions, bid_partitioner)
result = {}
result['n_partitions'] = bid_cnt_rdd.getNumPartitions()
result['n_items'] = bid_cnt_rdd.glom().map(len).collect()
result['result'] = bid_cnt_rdd\
.reduceByKey(lambda val1, val2: val1 + val2)\
.filter(lambda tup: tup[1] > n)\
.collect()
with open(output_file_path, "w+") as of:
json.dump(result, of)
of.close()
if __name__ == "__main__":
t3 = Task3()
t3.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.