Repository navigation
Expand file tree
/
Copy pathFirst spark Program.py
More file actions
111 lines (61 loc) · 2.01 KB
/
Copy pathFirst spark Program.py
File metadata and controls
111 lines (61 loc) · 2.01 KB
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
# Databricks notebook source
print("hello")
# COMMAND ----------
my_list = [11, 12, 17, 14, 10, 13]
odd_numbers = list(filter(lambda x: x%2 != 0, my_list))
print("odd_numbers = {}".format(odd_numbers))
def square(x):
return x**2
squared_numbers = list(map(square, my_list))
print("squared_numbers = {}".format(squared_numbers))
from functools import reduce
max_number = reduce(lambda x, y: x if x>y else y, my_list)
print("max_number = {}".format(max_number))
# COMMAND ----------
my_list = [11, 12, 17, 14, 10, 13]
rdd = sc.parallelize(my_list)
# COMMAND ----------
rdd
# COMMAND ----------
rdd.getNumPartitions()
# COMMAND ----------
print(rdd.collect())
# COMMAND ----------
odd_numbers_rdd = rdd.filter(lambda x: x%2 != 0)
# COMMAND ----------
odd_numbers_rdd
# COMMAND ----------
lsit_of_odd_numbers = odd_numbers_rdd.collect()
print("RDD of odd numbers = {}".format(lsit_of_odd_numbers))
# COMMAND ----------
sorted_rdd = rdd.sortBy(lambda x: x).collect()
print("Sorted RDD = {}".format(sorted_rdd))
# COMMAND ----------
max_val = rdd.reduce(lambda x, y: x if x>y else y)
print("The max value from the RDD is {}".format(max_val))
# COMMAND ----------
# MAGIC %md
# MAGIC #### Aggregations with RDD
# COMMAND ----------
input_rdd = sc.textFile("/FileStore/tables/weather.csv", 2)
# COMMAND ----------
print(input_rdd.getNumPartitions())
# COMMAND ----------
# MAGIC %md
# MAGIC ####MAGIC Select only year and temperature to generate PairRDD
# COMMAND ----------
selected_fields_rdd = input_rdd.map(lambda line: (int(line.split(",")[0].split("-")[0]), int(line.split(",")[2])))
# COMMAND ----------
selected_fields_rdd
# COMMAND ----------
print(selected_fields_rdd.collect())
# COMMAND ----------
# MAGIC %md
# MAGIC ####Get the maximum temperature corresponding to each year
# COMMAND ----------
max_temperature_rdd = selected_fields_rdd.reduceByKey(lambda x, y: x if x>y else y)
# COMMAND ----------
result = max_temperature_rdd.collect()
# COMMAND ----------
print(result)
# COMMAND ----------