diff options
author | Josh Rosen <joshrosen@eecs.berkeley.edu> | 2012-12-28 22:51:28 -0800 |
---|---|---|
committer | Josh Rosen <joshrosen@eecs.berkeley.edu> | 2012-12-28 22:51:28 -0800 |
commit | c2b105af34f7241ac0597d9c35fbf66633a3eaf6 (patch) | |
tree | e96946d2b714365937019f60741bf3ae62d565c6 /pyspark/examples/pi.py | |
parent | 7ec3595de28d53839cb3a45e940ec16f81ffdf45 (diff) | |
download | spark-c2b105af34f7241ac0597d9c35fbf66633a3eaf6.tar.gz spark-c2b105af34f7241ac0597d9c35fbf66633a3eaf6.tar.bz2 spark-c2b105af34f7241ac0597d9c35fbf66633a3eaf6.zip |
Add documentation for Python API.
Diffstat (limited to 'pyspark/examples/pi.py')
-rw-r--r-- | pyspark/examples/pi.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/pyspark/examples/pi.py b/pyspark/examples/pi.py new file mode 100644 index 0000000000..348bbc5dce --- /dev/null +++ b/pyspark/examples/pi.py @@ -0,0 +1,20 @@ +import sys +from random import random +from operator import add +from pyspark.context import SparkContext + + +if __name__ == "__main__": + if len(sys.argv) == 1: + print >> sys.stderr, \ + "Usage: PythonPi <host> [<slices>]" + exit(-1) + sc = SparkContext(sys.argv[1], "PythonPi") + slices = int(sys.argv[2]) if len(sys.argv) > 2 else 2 + n = 100000 * slices + def f(_): + x = random() * 2 - 1 + y = random() * 2 - 1 + return 1 if x ** 2 + y ** 2 < 1 else 0 + count = sc.parallelize(xrange(1, n+1), slices).map(f).reduce(add) + print "Pi is roughly %f" % (4.0 * count / n) |