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

    https://github.com/apache/spark/pull/10294#discussion_r48715601
  
    --- Diff: 
external/kafka-v09/src/test/scala/org/apache/spark/streaming/kafka/v09/DirectKafkaStreamSuite.scala
 ---
    @@ -0,0 +1,503 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.streaming.kafka.v09
    +
    +import java.io.File
    +import java.util.concurrent.atomic.AtomicLong
    +
    +import kafka.common.TopicAndPartition
    +import org.apache.kafka.clients.consumer.{ ConsumerRecord, ConsumerConfig }
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.streaming.dstream.DStream
    +import org.apache.spark.streaming.scheduler.rate.RateEstimator
    +import org.apache.spark.streaming.scheduler.{ 
StreamingListenerBatchCompleted, StreamingListenerBatchStarted, 
StreamingListenerBatchSubmitted, StreamingListener }
    +import org.apache.spark.streaming.{ Time, Milliseconds, StreamingContext }
    +import org.apache.spark.util.Utils
    +import org.apache.spark.{ SparkContext, SparkConf, Logging, SparkFunSuite }
    +import org.scalatest.concurrent.Eventually
    +import org.scalatest.{ BeforeAndAfter, BeforeAndAfterAll }
    +
    +import scala.collection.mutable
    +import scala.collection.mutable.ArrayBuffer
    +import scala.concurrent.duration._
    +import scala.language.postfixOps
    +
    +class DirectKafkaStreamSuite
    +  extends SparkFunSuite
    +  with BeforeAndAfter
    +  with BeforeAndAfterAll
    +  with Eventually
    +  with Logging {
    +  val sparkConf = new SparkConf()
    +    .setMaster("local[4]")
    +    .setAppName(this.getClass.getSimpleName)
    +
    +  private var sc: SparkContext = _
    +  private var ssc: StreamingContext = _
    +  private var testDir: File = _
    +
    +  private var kafkaTestUtils: KafkaTestUtils = _
    +
    +  override def beforeAll {
    +    kafkaTestUtils = new KafkaTestUtils
    +    kafkaTestUtils.setup()
    +  }
    +
    +  override def afterAll {
    +    if (kafkaTestUtils != null) {
    +      kafkaTestUtils.teardown()
    +      kafkaTestUtils = null
    +    }
    +  }
    +
    +  after {
    +    if (ssc != null) {
    +      ssc.stop()
    +      sc = null
    +    }
    +    if (sc != null) {
    +      sc.stop()
    +    }
    +    if (testDir != null) {
    +      Utils.deleteRecursively(testDir)
    +    }
    +  }
    +
    +  test("basic stream receiving with multiple topics and earliest starting 
offset") {
    +    val topics = Set("new_basic1", "new_basic2", "new_basic3")
    +    val data = Map("a" -> 7, "b" -> 9)
    +    topics.foreach { t =>
    +      kafkaTestUtils.createTopic(t)
    +      kafkaTestUtils.sendMessages(t, data)
    +    }
    +    val totalSent = data.values.sum * topics.size
    +    val kafkaParams = Map(
    +      ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG -> 
kafkaTestUtils.brokerAddress,
    +      ConsumerConfig.AUTO_OFFSET_RESET_CONFIG -> "earliest",
    +      ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG ->
    +        "org.apache.kafka.common.serialization.StringDeserializer",
    +      ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG ->
    +        "org.apache.kafka.common.serialization.StringDeserializer",
    +      "spark.kafka.poll.time" -> "1000")
    +
    +    ssc = new StreamingContext(sparkConf, Milliseconds(200))
    +    val stream = withClue("Error creating direct stream") {
    +      KafkaUtils.createDirectStream[String, String](
    +        ssc, kafkaParams, topics)
    +    }
    +
    +    val allReceived =
    +      new ArrayBuffer[(String, String)] with 
mutable.SynchronizedBuffer[(String, String)]
    +
    +    // hold a reference to the current offset ranges, so it can be used 
downstream
    +    var offsetRanges = Array[OffsetRange]()
    +
    +    stream.transform { rdd =>
    +      // Get the offset ranges in the RDD
    +      offsetRanges = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
    +      rdd
    +    }.foreachRDD { rdd =>
    +      for (o <- offsetRanges) {
    +        log.info(s"${rdd.id} | ${o.topic} ${o.partition} ${o.fromOffset} 
${o.untilOffset}")
    +      }
    +      val collected = rdd.mapPartitionsWithIndex { (i, iter) =>
    +        // For each partition, get size of the range in the partition,
    +        // and the number of items in the partition
    +        val off = offsetRanges(i)
    +        val all = iter.toSeq
    +        val partSize = all.size
    +        val rangeSize = off.untilOffset - off.fromOffset
    +        Iterator((partSize, rangeSize))
    +      }.collect
    +
    +      // Verify whether number of elements in each partition
    +      // matches with the corresponding offset range
    +      collected.foreach {
    +        case (partSize, rangeSize) =>
    +          assert(partSize === rangeSize, "offset ranges are wrong")
    +      }
    +    }
    +    stream.foreachRDD { rdd => allReceived ++= rdd.collect() }
    +    ssc.start()
    +    eventually(timeout(20000.milliseconds), interval(200.milliseconds)) {
    +      assert(allReceived.size === totalSent,
    +        "didn't get expected number of messages, messages:\n" + 
allReceived.mkString("\n"))
    +    }
    +    ssc.stop()
    +  }
    +
    +  test("receiving from latest starting offset") {
    +    val topic = "new_latest"
    +    val topicPartition = TopicAndPartition(topic, 0)
    +    val data = Map("a" -> 10)
    +    kafkaTestUtils.createTopic(topic)
    +    val kafkaParams = Map(
    +      ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG -> 
kafkaTestUtils.brokerAddress,
    +      ConsumerConfig.AUTO_OFFSET_RESET_CONFIG -> "latest",
    +      ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG ->
    +        "org.apache.kafka.common.serialization.StringDeserializer",
    +      ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG ->
    +        "org.apache.kafka.common.serialization.StringDeserializer",
    +      "spark.kafka.poll.time" -> "100")
    +    val kc = new KafkaCluster(kafkaParams)
    +    def getLatestOffset(): Long = {
    +      kc.getLatestOffsets(Set(topicPartition)).get(topicPartition).get
    --- End diff --
    
    given that getLatestOffsets method returns a Map now and a get on that 
would return Option....we will need to change this line to 
    
    kc.getLatestOffsets(Set(topicPartition)).get(topicPartition).getOrElse(0)


---
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