-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccumulators.scala
47 lines (40 loc) · 1.41 KB
/
Accumulators.scala
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
/******************************************************/
/* Spark accumulators in Scala */
/* */
/* */
/******************************************************/
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.storage._
import org.apache.spark.HashPartitioner
object Accumulators
{
def main(args: Array[String])
{
val conf = new SparkConf().setMaster("local").setAppName("Spark accumulators in Scala")
val sc = new SparkContext(conf)
val input = sc.textFile("dataToSortLinesMissing.txt")
// Two accumulators
val blankLines = sc.accumulator(0)
val notBlankLines = sc.accumulator(0)
val goodLines = input.flatMap
{ line =>
{
if (line == "")
{
blankLines += 1
}
{
// First line had to be first. I think because that is the line
// that returns what is needed.
notBlankLines += 1
line.split(" ")
}
}
}
goodLines.count()
println("Blank Lines => " + blankLines.value)
println("Not Blank Lines => " + notBlankLines.value)
}
}