Skip to content

Commit 052196f

Browse files
author
James Lee
committed
AvgCount and AverageHousePriceSolution
1 parent 67656f1 commit 052196f

File tree

4 files changed

+32
-68
lines changed

4 files changed

+32
-68
lines changed

src/main/scala/com/sparkTutorial/pairRdd/aggregation/reducebykey/housePrice/AvgCount.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.sparkTutorial.pairRdd.aggregation.reducebykey.housePrice
2+
3+
case class AvgCount(count: Int, total: Double)
4+

src/main/scala/com/sparkTutorial/pairRdd/sort/AverageHousePriceSolution.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.sparkTutorial.pairRdd.sort
2+
3+
import com.sparkTutorial.pairRdd.aggregation.reducebykey.housePrice.AvgCount
4+
import org.apache.log4j.{Level, Logger}
5+
import org.apache.spark.{SparkConf, SparkContext}
6+
7+
object AverageHousePriceSolution {
8+
9+
def main(args: Array[String]) {
10+
Logger.getLogger("org").setLevel(Level.ERROR)
11+
val conf = new SparkConf().setAppName("averageHousePriceSolution").setMaster("local[3]")
12+
val sc = new SparkContext(conf)
13+
14+
val lines = sc.textFile("in/RealEstate.csv")
15+
val cleanedLines = lines.filter(line => !line.contains("Bedrooms"))
16+
val housePricePairRdd = cleanedLines.map(
17+
line => (line.split(",")(3).toInt, AvgCount(1, line.split(",")(2).toDouble)))
18+
19+
val housePriceTotal = housePricePairRdd.reduceByKey((x, y) => AvgCount(x.count + y.count, x.total + y.total))
20+
21+
val housePriceAvg = housePriceTotal.mapValues(avgCount => avgCount.total / avgCount.count)
22+
23+
val sortedHousePriceAvg = housePriceAvg.sortByKey()
24+
25+
for ((bedrooms, avgPrice) <- sortedHousePriceAvg.collect()) println(bedrooms + " : " + avgPrice)
26+
}
27+
28+
}

0 commit comments

Comments
 (0)