Skip to content

Instantly share code, notes, and snippets.

@AtlasPilotPuppy
Created October 24, 2014 17:43
Show Gist options
  • Save AtlasPilotPuppy/6fdbc6a3d9a40d453397 to your computer and use it in GitHub Desktop.
Save AtlasPilotPuppy/6fdbc6a3d9a40d453397 to your computer and use it in GitHub Desktop.
accumulator example
from pyspark import SparkContext
sc = SparkContext('spark://master:7077', 'accumulator example')
# accumulators are initialized with a initial value
# they have and add method to add values to the accumulator
# and a value property that is visibile only to the master
accum = sc.accumulator(0)
data = sc.parallelize(range(1,1000))
# we are going to iterate over our data and add each value to the
# accumulator
data.foreach(lambda value: accum.add(value))
print accum.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment