码迷,mamicode.com
首页 > 其他好文 > 详细

Streaming 与kafka updateStateBykey()

时间:2017-03-10 22:30:49      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:sparksteaming kafka updatestatebykey

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 与kafka updateStateBykey()

标签:sparksteaming kafka updatestatebykey

原文地址:http://11194009.blog.51cto.com/11184009/1905314

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!