• spark2.1消费kafka0.8的数据 Recevier && Direct


    官网案例:

    http://spark.apache.org/docs/2.1.1/streaming-kafka-0-8-integration.html

    pom.xml依赖

        <dependency>
          <groupId>org.apache.spark</groupId>
          <artifactId>spark-streaming_2.11</artifactId>
          <version>2.1.1</version>
          <!--      <scope>provided</scope>   -->
        </dependency>
        <dependency>
          <groupId>org.apache.spark</groupId>
          <artifactId>spark-streaming-kafka-0-8_2.11</artifactId>
          <version>2.1.1</version>
        </dependency>

    Receiver  方式代码:

    package SpartStreamingaiqiyi
    
    import org.apache.spark.SparkConf
    import org.apache.spark.streaming.{Seconds, StreamingContext}
    import org.apache.spark.streaming.kafka.KafkaUtils
    
    object kafkaReading {
      def main(args: Array[String]): Unit = {
        if (args.length != 4){
          println("usage: SpartStreamingaiqiyi.test <zkQuorm> <group> <topics> <numthreads>")
        }
        val Array(zkQuorm,group,topics,numthreads) = args
        val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
        val ssc = new StreamingContext(conf, Seconds(5))
        val topicMap=topics.split(",").map((_,numthreads.toInt)).toMap
        val messages = KafkaUtils.createStream(ssc,
          zkQuorm, group,topicMap)
        messages.map(_._2).print()
        ssc.start()
        ssc.awaitTermination()
      }
    }

    Direct模式代码:

    package SpartStreamingaiqiyi
    
    import kafka.serializer.StringDecoder
    import org.apache.spark.SparkConf
    import org.apache.spark.streaming.{Seconds, StreamingContext}
    import org.apache.spark.streaming.kafka.KafkaUtils
    import org.apache.kafka.common.serialization.StringDeserializer
    object kafkaReading {
      def main(args: Array[String]): Unit = {
        if (args.length != 2){
          System.err.print("usage: SpartStreamingaiqiyi.test <brockers>  <topics> ")
          System.exit(1)
        }
        val Array(brokers,topics) = args
    
        val conf = new SparkConf().setMaster("local[2]").setAppName("NetworkWordCount")
        val ssc = new StreamingContext(conf, Seconds(5))
        val kafkaParams=Map[String, String]("bootstrap.servers" -> brokers)
        val topicSet=topics.split(",").toSet
        val messages = KafkaUtils.createDirectStream[String,String,StringDecoder,StringDecoder](ssc,kafkaParams,topicSet)
        messages.map(_._2).print()
        ssc.start()
        ssc.awaitTermination()
      }
    }
  • 相关阅读:
    R语言 主成分分析
    主成分分析(PCA)及其在R里的实现
    UML类图几种关系的总结
    微信Android客户端架构演进之路
    Android单元测试实践
    Android studio 快捷键(Mac)
    Android 启动模式及常用的Intent的Flag
    linux常用命令 (mac ),积少成多
    Android Studio IDE 简单学习和介绍
    轻量级分布式 RPC 框架
  • 原文地址:https://www.cnblogs.com/students/p/12035376.html
Copyright © 2020-2023  润新知