Spark-Streaming中累加
来源:互联网 发布:手机淘宝付款方式 编辑:程序博客网 时间:2024/06/06 01:37
import org.apache.spark.streaming.{Seconds, StreamingContext}import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}/** * Created by root on 2016/5/21. */object StateFulWordCount { //Seq这个批次某个单词的次数 //Option[Int]:以前的结果 //分好组的数据 val updateFunc = (iter: Iterator[(String, Seq[Int], Option[Int])]) => { //iter.flatMap(it=>Some(it._2.sum + it._3.getOrElse(0)).map(x=>(it._1,x))) //iter.map{case(x,y,z)=>Some(y.sum + z.getOrElse(0)).map(m=>(x, m))} //iter.map(t => (t._1, t._2.sum + t._3.getOrElse(0))) iter.map{ case(word, current_count, history_count) => (word, current_count.sum + history_count.getOrElse(0)) } } def main(args: Array[String]) { LoggerLevels.setStreamingLogLevels() //StreamingContext val conf = new SparkConf().setAppName("StateFulWordCount").setMaster("local[2]") val sc = new SparkContext(conf) //updateStateByKey必须设置setCheckpointDir sc.setCheckpointDir("c://ck") val ssc = new StreamingContext(sc, Seconds(5)) val ds = ssc.socketTextStream("172.16.0.11", 8888) //DStream是一个特殊的RDD //hello tom hello jerry val result = ds.flatMap(_.split(" ")).map((_, 1)).updateStateByKey(updateFunc, new HashPartitioner(sc.defaultParallelism), true) result.print() ssc.start() ssc.awaitTermination() }}
阅读全文
0 0
- Spark-Streaming中累加
- Spark Streaming基础学习【二】数值累加
- apach zeppelin中使用spark streaming:基本功能
- Spark Streaming中并行运行任务
- Spark Streaming中如何实现Exactly-Once
- Spark Streaming
- spark streaming
- Spark/Streaming
- Spark Streaming
- spark streaming
- Spark Streaming
- Spark Streaming
- Spark Streaming
- Spark Streaming
- spark streaming
- Spark Streaming
- Spark Streaming
- Spark Streaming
- Nested Segments CodeForces
- html标签
- 基于TI的TCP/IP协议栈---NDK
- Libevent源码分析-----超时event的处理
- 解决EventBus中接收方法中无法更新UI的问题
- Spark-Streaming中累加
- Libevent源码分析-----Libevent时间管理
- hdoj1080 Human Gene Functions (DP,map)
- 冒泡排序
- dom4j读取xml文件,用extjs以树的形式显示
- 倍增法求LCA
- Libevent源码分析-----管理超时event
- Spring中自定义JSON注释
- Python高级编程--闭包与装饰器