Skip to content

Instantly share code, notes, and snippets.

@foxish
Created June 19, 2017 08:47
Show Gist options
  • Save foxish/5eda42b45c6fd4e17b7e87699034ff6a to your computer and use it in GitHub Desktop.
Save foxish/5eda42b45c6fd4e17b7e87699034ff6a to your computer and use it in GitHub Desktop.
Spark Jupyter Examples
%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))
%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