diff options
Diffstat (limited to 'examples/src/main/scala/SparkPi.scala')
-rw-r--r-- | examples/src/main/scala/SparkPi.scala | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/examples/src/main/scala/SparkPi.scala b/examples/src/main/scala/SparkPi.scala new file mode 100644 index 0000000000..f055614125 --- /dev/null +++ b/examples/src/main/scala/SparkPi.scala @@ -0,0 +1,21 @@ +import scala.math.random +import spark._ +import SparkContext._ + +object SparkPi { + def main(args: Array[String]) { + if (args.length == 0) { + System.err.println("Usage: SparkPi <host> [<slices>]") + System.exit(1) + } + val spark = new SparkContext(args(0), "SparkPi") + val slices = if (args.length > 1) args(1).toInt else 2 + var count = spark.accumulator(0) + for (i <- spark.parallelize(1 to 100000, slices)) { + val x = random * 2 - 1 + val y = random * 2 - 1 + if (x*x + y*y < 1) count += 1 + } + println("Pi is roughly " + 4 * count.value / 100000.0) + } +} |