Created
June 19, 2017 08:47
-
-
Save foxish/5eda42b45c6fd4e17b7e87699034ff6a to your computer and use it in GitHub Desktop.
Spark Jupyter Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%pyspark | |
from random import random | |
partitions = 2 | |
n = 100000 * partitions | |
def f(_): | |
x = random() * 2 - 1 | |
y = random() * 2 - 1 | |
return 1 if x ** 2 + y ** 2 <= 1 else 0 | |
count = sc.parallelize(range(1, n + 1), partitions).map(f).reduce(add) | |
print("Pi is roughly %f" % (4.0 * count / n)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%pyspark | |
from math import sqrt; from itertools import count, islice | |
def isprime(n): | |
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1))) | |
nums = sc.parallelize(xrange(10000000)) | |
print nums.filter(isprime).count() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment