장연우

assignment2

1 +(u'allston', 207)
2 +(u'area', 6)
3 +(u'back', 283)
4 +(u'bay', 302)
5 +(u'beacon', 205)
6 +(u'boston', 348)
7 +(u'brighton', 180)
8 +(u'charlestown', 65)
9 +(u'chinatown', 55)
10 +(u'district', 8)
11 +(u'dorchester', 240)
12 +(u'downtown', 152)
13 +(u'east', 126)
14 +(u'end', 518)
15 +(u'fenway', 296)
16 +(u'hill', 306)
17 +(u'hyde', 26)
1 +(u'jamaica', 315)
2 +(u'leather', 8)
3 +(u'longwood', 6)
4 +(u'mattapan', 20)
5 +(u'medical', 6)
6 +(u'mission', 101)
7 +(u'neighborhood', 1)
8 +(u'north', 132)
9 +(u'park', 26)
10 +(u'plain', 315)
11 +(u'roslindale', 56)
12 +(u'roxbury', 177)
13 +(u'south', 550)
14 +(u'village', 19)
15 +(u'waterfront', 69)
16 +(u'west', 88)
1 +import sys
2 +import re
3 +from operator import add
4 +
5 +from pyspark import SparkContext
6 +
7 +def map_phase(x):
8 + x = re.sub('--', ' ', x)
9 + x = re.sub("'", '', x)
10 + return re.sub('[?!@#$\'",.;:()]', '', x).lower()
11 +
12 +def countWord(line):
13 + global count_number
14 + if (line == "Tokyo"):
15 + count_number += 1
16 + return line.split(' ')
17 +
18 +if __name__ == "__main__":
19 + if len(sys.argv) < 4:
20 + print >> sys.stderr, "Usage: wordcount <master> <inputfile> <outputfile>"
21 + exit(-1)
22 + sc = SparkContext(sys.argv[1], "python_wordcount_sorted in bigdataprogrammiing")
23 + lines = sc.textFile(sys.argv[2],2)
24 + count_number = sc.accumulator(0)
25 +
26 + counts = lines\
27 + .flatMap(countWord)\
28 + .filter(lambda x: x!="Tokyo")\
29 + .map(lambda x: (x.lower(), 1))\
30 + .reduceByKey(lambda x,y:x+y)\
31 + .sortByKey(ascending=True)
32 +#.sortBy(lambda x: x[0])
33 +
34 + counts.saveAsTextFile("hdfs://localhost:9000/output5")
35 + print('Number of Tokyo : ', count_number.value)
36 + sc.stop()