Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
ssc = StreamingContext(sc, 1)
ssc.checkpoint("checkpoint")

# RDD with initial state (key, value) pairs
initialStateRDD = sc.parallelize([(u'hello', 1), (u'world', 1)])

def updateFunc(new_values, last_sum):
return sum(new_values) + (last_sum or 0)

lines = ssc.socketTextStream(sys.argv[1], int(sys.argv[2]))
running_counts = lines.flatMap(lambda line: line.split(" "))\
.map(lambda word: (word, 1))\
.updateStateByKey(updateFunc)
.updateStateByKey(updateFunc, initialRDD=initialStateRDD)

running_counts.pprint()

Expand Down
13 changes: 11 additions & 2 deletions python/pyspark/streaming/dstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def invReduceFunc(t, a, b):
self._ssc._jduration(slideDuration))
return DStream(dstream.asJavaDStream(), self._ssc, self._sc.serializer)

def updateStateByKey(self, updateFunc, numPartitions=None):
def updateStateByKey(self, updateFunc, numPartitions=None, initialRDD=None):
"""
Return a new "state" DStream where the state for each key is updated by applying
the given function on the previous state of the key and the new values of the key.
Expand All @@ -579,6 +579,9 @@ def updateStateByKey(self, updateFunc, numPartitions=None):
if numPartitions is None:
numPartitions = self._sc.defaultParallelism

if initialRDD and not isinstance(initialRDD, RDD):
initialRDD = self._sc.parallelize(initialRDD)

def reduceFunc(t, a, b):
if a is None:
g = b.groupByKey(numPartitions).mapValues(lambda vs: (list(vs), None))
Expand All @@ -590,7 +593,13 @@ def reduceFunc(t, a, b):

jreduceFunc = TransformFunction(self._sc, reduceFunc,
self._sc.serializer, self._jrdd_deserializer)
dstream = self._sc._jvm.PythonStateDStream(self._jdstream.dstream(), jreduceFunc)
if initialRDD:
initialRDD = initialRDD._reserialize(self._jrdd_deserializer)
dstream = self._sc._jvm.PythonStateDStream(self._jdstream.dstream(), jreduceFunc,
initialRDD._jrdd)
else:
dstream = self._sc._jvm.PythonStateDStream(self._jdstream.dstream(), jreduceFunc)

return DStream(dstream.asJavaDStream(), self._ssc, self._sc.serializer)


Expand Down
20 changes: 20 additions & 0 deletions python/pyspark/streaming/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,26 @@ def func(dstream):
expected = [[('k', v)] for v in expected]
self._test_func(input, func, expected)

def test_update_state_by_key_initial_rdd(self):

def updater(vs, s):
if not s:
s = []
s.extend(vs)
return s

initial = [('k', [0, 1])]
initial = self.sc.parallelize(initial, 1)

input = [[('k', i)] for i in range(2, 5)]

def func(dstream):
return dstream.updateStateByKey(updater, initialRDD=initial)

expected = [[0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
expected = [[('k', v)] for v in expected]
self._test_func(input, func, expected)

def test_failed_func(self):
# Test failure in
# TransformFunction.apply(rdd: Option[RDD[_]], time: Time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,27 @@ private[python] class PythonTransformed2DStream(
*/
private[python] class PythonStateDStream(
parent: DStream[Array[Byte]],
reduceFunc: PythonTransformFunction)
reduceFunc: PythonTransformFunction,
initialRDD: Option[RDD[Array[Byte]]])
extends PythonDStream(parent, reduceFunc) {

def this(
parent: DStream[Array[Byte]],
reduceFunc: PythonTransformFunction) = this(parent, reduceFunc, None)

def this(
parent: DStream[Array[Byte]],
reduceFunc: PythonTransformFunction,
initialRDD: JavaRDD[Array[Byte]]) = this(parent, reduceFunc, Some(initialRDD.rdd))

super.persist(StorageLevel.MEMORY_ONLY)
override val mustCheckpoint = true

override def compute(validTime: Time): Option[RDD[Array[Byte]]] = {
val lastState = getOrCompute(validTime - slideDuration)
val rdd = parent.getOrCompute(validTime)
if (rdd.isDefined) {
func(lastState, rdd, validTime)
func(lastState.orElse(initialRDD), rdd, validTime)
} else {
lastState
}
Expand Down