zoukankan      html  css  js  c++  java
  • foreachRDD,foreach,foreachPartition区别联系

    foreachRDD(SparkStreaming):

      SparkStreaming是流式实时处理数据,就是将数据流按照定义的时间进行分割(就是"批处理").每一个时间段内处理到的都是一个RDD.而SparkStreaming中的foreachRDD方法就是在处理每一个时间段内的RDD数据.

      DStream中即使有foreachRDD算子也不会立即进行处理,只有foreach(func)函数func中存在action算子才会执行foreachRDD算子运算,所有foreachRDD的函数中可以使用foreach和foreachPartition算子来触发action操作.

      foreachRDD算子源码中的注释是:Apply a function to each RDD in this DStream. This is an output operator,so 'this' DStream will be registered as an output stream and therefore materialized.

      (将函数应用于此DStream中的每个RDD.这是一个输出操作符,所以'this'' DStream将被注册为输出流,因此具体化)

    foreachPartition(Spark-Core):

      foreachPartition是action算子,该算子源码中的注释是:Applies a function func to each parition of this RDD.(将函数func应用于此RDD的每个分区)

      foreachPartition是对每个partition中的iterator实行迭代的处理,通过用户传入的function(即函数func)iterator进行内容的处理,源码中函数func传入的参数是一个迭代器,也就是说在functionPartition中函数处理的是分区迭代器,而非具体的数据.

    foreach(Spark-Core):

      与foreachPartition类似的是,foreach也是对每个partition中的iterator实行迭代处理,通过用户传入的function(即函数func)对iterator进行内容的处理,而不同的是,函数func中的参数传入的不再是一个迭代器,而是每次foreach得到的一个rdd的kv实例,也就是具体的数据.

      foreach也是action算子,源码中的注释是:Applies  a function fun to all elements of this RDD.(将函数func用于此RDD的所有元素)

  • 相关阅读:
    Codeforces Round #590 D. Distinct Characters Queries
    线段树模板加模板题POJ3468
    hihoCoder挑战赛5 C 与链
    HDU 5044 Tree 树链剖分
    HYSBZ 1901 Dynamic Rankings 树状数组套主席树
    POJ 2761 Feed the dogs 主席树
    POJ 2104 K-th Number 主席树 区间第K大
    HDU 4547 CD操作 LCA
    POJ 1470 Closest Common Ancestors 离线LCA
    HYSBZ 1036 树的统计Count 树链剖分 线段树
  • 原文地址:https://www.cnblogs.com/gentle-awen/p/10011262.html
Copyright © 2011-2022 走看看