0

I am using Spark Shell (Scala 2.10 and Spark Streaming org.apache.spark:spark-streaming-kafka-0-10_2.10:2.0.1) to test a Spark/Kafka consumer:

import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.streaming.kafka010._
import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent
import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.dstream.DStream

val kafkaParams = Map[String, Object](
  "bootstrap.servers" -> "mykafka01.example.com:9092",
  "key.deserializer" -> classOf[StringDeserializer],
  "value.deserializer" -> classOf[StringDeserializer],
  "group.id" -> "mykafka",
  "auto.offset.reset" -> "latest",
  "enable.auto.commit" -> (false: java.lang.Boolean)
)

val topics = Array("mytopic")

def createKafkaStream(ssc: StreamingContext, topics: Array[String], kafkaParams: Map[String,Object]) : DStream[(String, String)] = {
    KafkaUtils.createDirectStream[String, String](ssc, PreferConsistent, Subscribe[String, String](topics, kafkaParams))
}

def messageConsumer(): StreamingContext = {
    val ssc = new StreamingContext(SparkContext.getOrCreate(), Seconds(10))

    createKafkaStream(ssc, topics, kafkaParams).foreachRDD(rdd => {
        rdd.collect().foreach { msg =>
            try {
                println("Received message: " + msg._2)
            } catch {
                case e @ (_: Exception | _: Error | _: Throwable) => {
                println("Exception: " + e.getMessage)
                e.printStackTrace()
            }
          }
        }
    })

    ssc
}

val ssc = StreamingContext.getActiveOrCreate(messageConsumer)
ssc.start()
ssc.awaitTermination()

When I run this I get the following exception:

<console>:60: error: type mismatch;
 found   : org.apache.spark.streaming.dstream.InputDStream[org.apache.kafka.clients.consumer.ConsumerRecord[String,String]]
 required: org.apache.spark.streaming.dstream.DStream[(String, String)]
                  KafkaUtils.createDirectStream[String, String](ssc, PreferConsistent, Subscribe[String, String](topics, kafkaParams))
                                                               ^

I've checked the Scala/API docs over and over again, and this code looks like it should execute correctly. Any idea where I'm going awry?

Ruslan Ostafiichuk
  • 4,422
  • 6
  • 30
  • 35
smeeb
  • 27,777
  • 57
  • 250
  • 447

1 Answers1

4

Subscribe takes topics argument as Array[String], you are passing a single string as per def createKafkaStream(ssc: StreamingContext, topics: String,. Changing the argument type to Array[String] (and calling it appropriately) will fix the problem.

khachik
  • 28,112
  • 9
  • 59
  • 94
  • 1
    @smeeb I don't have that version of kafka lib to try, but you can look at overloaded `createDirectStream` methods to see what are their return types and what arguments they take. It appears that the method you are calling returns `DStream[ConsumerRecord[K, V]]` instead of `DStream[K, V]` you expect. Or, if it is the only choice, change your code to accept `DStream[ConsumerRecord[K, V]]` and then map to `(K, V`. – khachik Nov 15 '16 at 15:27
  • Thanks again @khachik (+1) - when you say "*It appears that the method you are callig returns `DStream[ConsumerRecord[K,V]]`*"...where do you see evidence of that. I am looking at what I *think* are the [correct javadocs](http://spark.apache.org/docs/2.0.1/api/java/org/apache/spark/streaming/kafka/KafkaUtils.html) and I'm not seeinf any overloaded `createDirectStream` methods that return `DStream[ConsumerRecord[K,V]]`s, thoughts? Thanks again!!! – smeeb Nov 15 '16 at 15:39
  • @smeeb looking at [the integration guide](https://spark.apache.org/docs/latest/streaming-kafka-0-10-integration.html), the method you call returns a stream of `ConsumerRecords`, you should map it to get key/value pairs: `stream.map(record => (record.key, record.value))`. The javadocs you posted seem to be for a different version. – khachik Nov 15 '16 at 16:37