spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zsxwing <...@git.apache.org>
Subject [GitHub] spark pull request #16686: [SPARK-18682][SS] Batch Source for Kafka
Date Wed, 25 Jan 2017 01:35:56 GMT
Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16686#discussion_r97690588
  
    --- Diff: external/kafka-0-10-sql/src/test/scala/org/apache/spark/sql/kafka010/KafkaRelationSuite.scala
---
    @@ -0,0 +1,215 @@
    +/*
    + * 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.sql.kafka010
    +
    +import java.util.concurrent.atomic.AtomicInteger
    +
    +import org.apache.kafka.common.TopicPartition
    +import org.scalatest.BeforeAndAfter
    +
    +import org.apache.spark.sql.QueryTest
    +import org.apache.spark.sql.test.{SharedSQLContext, TestSparkSession}
    +
    +class KafkaRelationSuite extends QueryTest with BeforeAndAfter with SharedSQLContext
{
    +
    +  import testImplicits._
    +
    +  private val topicId = new AtomicInteger(0)
    +
    +  private var testUtils: KafkaTestUtils = _
    +
    +  private def newTopic(): String = s"topic-${topicId.getAndIncrement()}"
    +
    +  private def assignString(topic: String, partitions: Iterable[Int]): String = {
    +    JsonUtils.partitions(partitions.map(p => new TopicPartition(topic, p)))
    +  }
    +
    +  override def beforeAll(): Unit = {
    +    super.beforeAll()
    +    testUtils = new KafkaTestUtils
    +    testUtils.setup()
    +  }
    +
    +  override def afterAll(): Unit = {
    +    if (testUtils != null) {
    +      testUtils.teardown()
    +      testUtils = null
    +      super.afterAll()
    +    }
    +  }
    +
    +  test("explicit earliest to latest offsets") {
    +    val topic = newTopic()
    +    testUtils.createTopic(topic, partitions = 3)
    +    testUtils.sendMessages(topic, (0 to 9).map(_.toString).toArray, Some(0))
    +    testUtils.sendMessages(topic, (10 to 19).map(_.toString).toArray, Some(1))
    +    testUtils.sendMessages(topic, Array("20"), Some(2))
    +
    +    // Specify explicit earliest and latest offset values
    +    val reader = spark
    +      .read
    +      .format("kafka")
    +      .option("kafka.bootstrap.servers", testUtils.brokerAddress)
    +      .option("subscribe", topic)
    +      .option("startingOffsets", "earliest")
    +      .option("endingOffsets", "latest")
    +      .load()
    +    var df = reader.selectExpr("CAST(value AS STRING)")
    +    checkAnswer(df, (0 to 20).map(_.toString).toDF)
    +
    +    // "latest" should late bind to the current (latest) offset in the reader
    +    testUtils.sendMessages(topic, (21 to 29).map(_.toString).toArray, Some(2))
    +    df = reader.selectExpr("CAST(value AS STRING)")
    +    checkAnswer(df, (0 to 29).map(_.toString).toDF)
    +  }
    +
    +  test("default starting and ending offsets") {
    +    val topic = newTopic()
    +    testUtils.createTopic(topic, partitions = 3)
    +    testUtils.sendMessages(topic, (0 to 9).map(_.toString).toArray, Some(0))
    +    testUtils.sendMessages(topic, (10 to 19).map(_.toString).toArray, Some(1))
    +    testUtils.sendMessages(topic, Array("20"), Some(2))
    +
    +    // Implicit offset values, should default to earliest and latest
    +    val df = spark
    +      .read
    +      .format("kafka")
    +      .option("kafka.bootstrap.servers", testUtils.brokerAddress)
    +      .option("subscribe", topic)
    +      .load()
    +      .selectExpr("CAST(value AS STRING)")
    +    // Test that we default to "earliest" and "latest"
    +    checkAnswer(df, (0 to 20).map(_.toString).toDF)
    +  }
    +
    +  test("explicit offsets") {
    +    val topic = newTopic()
    +    testUtils.createTopic(topic, partitions = 3)
    +    testUtils.sendMessages(topic, (0 to 9).map(_.toString).toArray, Some(0))
    +    testUtils.sendMessages(topic, (10 to 19).map(_.toString).toArray, Some(1))
    +    testUtils.sendMessages(topic, Array("20"), Some(2))
    +
    +    // Test explicitly specified offsets
    +    val startPartitionOffsets = Map(
    +      new TopicPartition(topic, 0) -> -2L, // -2 => earliest
    +      new TopicPartition(topic, 1) -> -2L,
    +      new TopicPartition(topic, 2) -> 0L   // explicit earliest
    +    )
    +    val startingOffsets = JsonUtils.partitionOffsets(startPartitionOffsets)
    +
    +    val endPartitionOffsets = Map(
    +      new TopicPartition(topic, 0) -> -1L, // -1 => latest
    +      new TopicPartition(topic, 1) -> -1L,
    +      new TopicPartition(topic, 2) -> 1L  // explicit offset happens to = the latest
    +    )
    +    val endingOffsets = JsonUtils.partitionOffsets(endPartitionOffsets)
    +    val reader = spark
    +      .read
    +      .format("kafka")
    +      .option("kafka.bootstrap.servers", testUtils.brokerAddress)
    +      .option("subscribe", topic)
    +      .option("startingOffsets", startingOffsets)
    +      .option("endingOffsets", endingOffsets)
    +      .load()
    +    var df = reader.selectExpr("CAST(value AS STRING)")
    +    checkAnswer(df, (0 to 20).map(_.toString).toDF)
    +
    +    // static offset partition 2, nothing should change
    +    testUtils.sendMessages(topic, (31 to 39).map(_.toString).toArray, Some(2))
    +    df = reader.selectExpr("CAST(value AS STRING)")
    +    checkAnswer(df, (0 to 20).map(_.toString).toDF)
    +
    +    // latest offset partition 1, should change
    +    testUtils.sendMessages(topic, (21 to 30).map(_.toString).toArray, Some(1))
    +    df = reader.selectExpr("CAST(value AS STRING)")
    +    checkAnswer(df, (0 to 30).map(_.toString).toDF)
    +  }
    +
    +  test("reuse same dataframe in query") {
    +    // This test ensures that we do not cache the Kafka Consumer in KafkaRelation
    +    val topic = newTopic()
    +    testUtils.createTopic(topic, partitions = 1)
    +    testUtils.sendMessages(topic, (0 to 10).map(_.toString).toArray, Some(0))
    +
    +    // Ensure local[2] so that two tasks will execute the query on one partition
    +    val testSession = new TestSparkSession(sparkContext)
    --- End diff --
    
    nit: you don't need to create `TestSparkSession`. I meant this test uses TestSparkSession
and it uses `local[2]`.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message