Skip to content

Commit 9eaec29

Browse files
author
James Lee
committed
improve AverageHousePriceSolution and AvgCount
1 parent d36bffa commit 9eaec29

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/main/java/com/sparkTutorial/pairRdd/aggregation/reducebykey/WordCount.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static void main(String[] args) throws Exception {
2929

3030
JavaPairRDD<String, Integer> wordCounts = wordPairRdd.reduceByKey((Function2<Integer, Integer, Integer>) (x, y) -> x + y);
3131

32-
for (Map.Entry<String, Integer> wordCountPair : wordCounts.collectAsMap().entrySet()) {
32+
Map<String, Integer> worldCountsMap = wordCounts.collectAsMap();
33+
34+
for (Map.Entry<String, Integer> wordCountPair : worldCountsMap.entrySet()) {
3335
System.out.println(wordCountPair.getKey() + " : " + wordCountPair.getValue());
3436

3537
}

src/main/java/com/sparkTutorial/pairRdd/aggregation/reducebykey/housePrice/AverageHousePriceSolution.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ public static void main(String[] args) throws Exception {
3434
(Function2<AvgCount, AvgCount, AvgCount>) (x, y) ->
3535
new AvgCount(x.getCount() + y.getCount(), x.getTotal() + y.getTotal()));
3636

37+
System.out.println("housePriceTotal: ");
38+
for (Map.Entry<String, AvgCount> housePriceTotalPair : housePriceTotal.collectAsMap().entrySet()) {
39+
System.out.println(housePriceTotalPair.getKey() + " : " + housePriceTotalPair.getValue());
40+
41+
}
3742

3843
JavaPairRDD<String, Double> housePriceAvg = housePriceTotal.mapToPair(
3944
(PairFunction<Tuple2<String, AvgCount>, String, Double>) total ->
4045
new Tuple2<>(total._1(), total._2().getTotal()/total._2().getCount()));
4146

47+
System.out.println("housePriceAvg: ");
4248
for (Map.Entry<String, Double> housePriceAvgPair : housePriceAvg.collectAsMap().entrySet()) {
4349
System.out.println(housePriceAvgPair.getKey() + " : " + housePriceAvgPair.getValue());
4450

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ public int getCount() {
1818
public double getTotal() {
1919
return total;
2020
}
21+
22+
@Override
23+
public String toString() {
24+
return "AvgCount{" +
25+
"count=" + count +
26+
", total=" + total +
27+
'}';
28+
}
2129
}

0 commit comments

Comments
 (0)