重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Streaming与kafkaupdateStateBykey()

object H extends App{
        val  conf=new  SparkConf().setMaster("local[2]").setAppName("hello")
        val ss=new StreamingContext(conf,Seconds(5))
        val kafkaParams=Map[String,String]("metadata.broker.list"->"myhadoop1:9092")
        ss.checkpoint("hdfs://myhadoop1:8020/data")
        val topic=Set[String]("wordcount1")
        //kafka
        val lines=KafkaUtils.createDirectStream[String,String,StringDecoder,StringDecoder](ss,kafkaParams,topic)
        lines.flatMap(_._2.split(" ")).map((_,1)).updateStateByKey((seqs:Seq[Int],option:Option[Int])=>{
                var oldValue=option.getOrElse(0)
                for(seq<-seqs){
                        oldValue+=seq
                }
                Option[Int](oldValue)
        }).print()
        ss.start()
        ss.awaitTermination()
}

文章题目:Streaming与kafkaupdateStateBykey()
链接分享:http://cqcxhl.com/article/gcojeh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP