spark采坑集锦之用kafka作为DStream数据源,并行度问题

在SparkStreaming中作为数据源的Kafka怎样接收多主题发送的数据呢?

使用StreamingContext.union方法将多个streaming流合并处理

def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    val conf = new SparkConf().setMaster("local[4]").setAppName("KafkaWordCount")
    val sc = new SparkContext(conf)
    val ssc = new StreamingContext(sc,Seconds(10))
    val topics = Map("tony" -> 1)
    val topics1 = Map("hunrry" -> 1)

    val num = 2

    val kafkastream = (1 to num).map(i => {
        if (i==1){
          System.out.println("i==1")
          KafkaUtils.createStream(ssc,"192.168.252.153:2181","mygroup",topics)
        }else{
          System.out.println("i==2")
          KafkaUtils.createStream(ssc,"192.168.252.153:2181","mygroup",topics1)
        }
    })

    //同时获取多个主题发送的数据
    val unonKafka  = ssc.union(kafkastream)

   // val lineStream = unonKafka.map(e => new String(e.toString))

    unonKafka.print()

    ssc.start()
    ssc.awaitTermination()


  }

经过个人实验发现个问题
如果指定的setMaster(“local[?]”)过低,可能会导致程序获取一两次后就不动了,不在获取了,只有local[]中的核数足够才能确保运行正常,能够获取多个主题发送过来的数据。

你可能感兴趣的:(spark采坑集锦)