大数据学习之路97-kafka直连方式(spark streaming 整合kafka 0.10版本)

我们之前SparkStreaming整合Kafka的时候用的是傻瓜式的方式-----createStream,但是这种方式的效率很低。而且在kafka 0.10版本之后就不再提供了。

接下来我们使用Kafka直连的方式,这种方式其实是调用Kafka底层的消费数据的API,我们知道,越底层的东西效率也就越高。

使用之前的方式是要连接到zookeeper的,而现在的方式则不需要。

代码如下:

package com.test.sparkStreaming



import org.apache.kafka.clients.consumer.ConsumerRecord
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.{HashPartitioner, SparkConf}
import org.apache.spark.rdd.RDD
import org.apache.spark.streaming.dstream.{DStream, InputDStream}
import org.apache.spark.streaming.kafka010._
import org.apache.spark.streaming.{Seconds, StreamingContext}


object KafkaDirectStream {
  val updateFunc = (it : Iterator[(String, Seq[Int], Option[Int])]) => {
    it.map{case (w,s,o) => (w,s.sum + o.getOrElse(0))}
  }
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("KafkaDirectStream").setMaster("local[*]")
    val 

你可能感兴趣的:(大数据生态圈从入门到精通)