Github user tdas commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7185#discussion_r34103210
  
    --- Diff: python/pyspark/streaming/kafka.py ---
    @@ -244,3 +263,87 @@ def __init__(self, host, port):
     
         def _jBroker(self, helper):
             return helper.createBroker(self._host, self._port)
    +
    +
    +class KafkaRDD(RDD):
    +    """
    +    A Python wrapper of KafkaRDD, to provide additional information on 
normal RDD.
    +    """
    +
    +    def __init__(self, jrdd, ctx, jrdd_deserializer):
    +        RDD.__init__(self, jrdd, ctx, jrdd_deserializer)
    +
    +    def offsetRanges(self):
    +        """
    +        Get the OffsetRange of specific KafkaRDD.
    +        :return: A list of OffsetRange
    +        """
    +        try:
    +            helperClass = 
self.ctx._jvm.java.lang.Thread.currentThread().getContextClassLoader() \
    +                
.loadClass("org.apache.spark.streaming.kafka.KafkaUtilsPythonHelper")
    +            helper = helperClass.newInstance()
    +            joffsetRanges = helper.offsetRangesOfKafkaRDD(self._jrdd.rdd())
    +        except Py4JJavaError as e:
    +            if 'ClassNotFoundException' in str(e.java_exception):
    +                KafkaUtils._printErrorMsg(self.ctx)
    +            raise e
    +
    +        ranges = [OffsetRange(o.topic(), o.partition(), o.fromOffset(), 
o.untilOffset())
    +                  for o in joffsetRanges]
    +        return ranges
    +
    +
    +class KafkaDStream(DStream):
    +    """
    +    A Python wrapper of KafkaDStream
    +    """
    +
    +    def __init__(self, jdstream, ssc, jrdd_deserializer):
    +        DStream.__init__(self, jdstream, ssc, jrdd_deserializer)
    +
    +    def foreachRDD(self, func):
    +        """
    +        Apply a function to each RDD in this DStream.
    +        """
    +        if func.__code__.co_argcount == 1:
    +            old_func = func
    +            func = lambda r, rdd: old_func(rdd)
    +        jfunc = TransformFunction(self._sc, func, self._jrdd_deserializer) 
\
    +            .rdd_wrapper(lambda jrdd, ctx, ser: KafkaRDD(jrdd, ctx, ser))
    +        api = self._ssc._jvm.PythonDStream
    +        api.callForeachRDD(self._jdstream, jfunc)
    +
    +    def transform(self, func):
    +        """
    +        Return a new DStream in which each RDD is generated by applying a 
function
    +        on each RDD of this DStream.
    +
    +        `func` can have one argument of `rdd`, or have two arguments of
    +        (`time`, `rdd`)
    +        """
    +        if func.__code__.co_argcount == 1:
    +            oldfunc = func
    +            func = lambda t, rdd: oldfunc(rdd)
    +        assert func.__code__.co_argcount == 2, "func should take one or 
two arguments"
    +
    +        return KafkaTransformedDStream(self, func)
    +
    +
    +class KafkaTransformedDStream(TransformedDStream):
    --- End diff --
    
    LGTM, but I dnt understand the problem well enough to comment with full 
conviction. I will let @davies check it out.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to