Spark Streaming Transformations

map(func):对DStream中的所有的元素进行func转换生成新的DStream

flatMap(func):和map方法类似,先对DStream中的元素进行func运算,然后压平,就是说,如果有一个元素是集合或者数组,那么会被拆成一个一个的元素

filter(func):对DStream中的元素进行func运算,把返回true的元素组成一个新的DStream

repartition(numPartitions): DStream重分区

union(otherStream):合并两个DStream

count(): 返回DStream中RDD中的元素的个数

reduce(func):聚合DStream中RDD的元素

countByValue():统计值出现的次数

reduceByKey(func, [numTasks]):对相同key的value进行func操作

join(otherStream, [numTasks]):相同key进行连接,(K, V) join (K, W) -> (K, (V, W))

cogroup(otherStream, [numTasks]):相同key进行右边的转换 (K, V) cogroup (K, W) (K, Seq[V], Seq[W])

transform(func): 对DStream中的RDD做func操作返回另外一个RDD

wordCounts.transform(rdd =>{
    rdd.filter(_._1 == "hello")
    rdd
})

updateStateByKey(func):根据key更新以前操作的结果,这个方法可以做累计操作,使用该方法要设置检查点目录,updateStateByKey方法参数需要指定类型

sc.setCheckpointDir("D://checkpoints/")
// 设置日志级别 sc.setLogLevel("ERROR") val ds1 = wordCounts.updateStateByKey[Int]((x:Seq[Int], y:Option[Int]) => { val newValue = x.sum + y.getOrElse(0) Some(newValue) })

  

posted @ 2017-04-28 11:28  天之涯0204  阅读(148)  评论(0编辑  收藏  举报