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
| from dataclasses import field from pyspark import SparkConf, SparkContext
conf = SparkConf().setMaster('local').setAppName('MinTemperatures') sc = SparkContext(conf = conf)
print("Start")
def parseLine(line): fields = line.split(",") stationID = fields[0] entryType = fields[2] temperature = float(fields[3]) * 0.1 * (9.0/5.0) + 32.0 return (stationID, entryType, temperature)
lines = sc.textFile('data/1800.csv')
parseLines = lines.map(parseLine)
minTemps = parseLines.filter(lambda x: "TMIN" in x[1]) stationTemps = minTemps.map(lambda x: (x[0],x[2])) minTemps = stationTemps.map(lambda x, y: min(x, y)) results = minTemps.collect()
for result in results: print(result[0]+ "\t{:.2f}F".format(result[1]))
|