【问题标题】:Spark's RDD.map() will not execute unless the item inside RDD is visited除非访问 RDD 中的项目,否则 Spark 的 RDD.map() 不会执行
【发布时间】:2014-07-25 20:42:18
【问题描述】:

我不太清楚ScalaSpark 是如何工作的,可能是我写错了代码。

我想要实现的功能是,对于给定的Seq[String, Int],将v._2.path中的一个随机项分配给_._2

为此,我实现了一个方法并在下一行调用此方法

def getVerticesWithFeatureSeq(graph: Graph[WikiVertex, WikiEdge.Value]): RDD[(VertexId, WikiVertex)] = {
  graph.vertices.map(v => {
    //For each token in the sequence, assign an article to them based on its path(root to this node)
    println(v._1+" before "+v._2.featureSequence)
    v._2.featureSequence = v._2.featureSequence.map(f => (f._1, v._2.path.apply(new scala.util.Random().nextInt(v._2.path.size))))
    println(v._1+" after "+v._2.featureSequence)
    (v._1, v._2)
  })
}

val dt = getVerticesWithFeatureSeq(wikiGraph)

当我执行它时,我想println 应该打印出一些东西,但它没有。 如果我添加另一行代码

dt.foreach(println)

println 内的map 将正确打印。

spark 的代码执行是否有一些延迟?就像没有人访问变量一样,计算会被推迟甚至取消?

【问题讨论】:

  • 请出示您的 Vertex 类
  • 顶点是 RDD 吗?这可以解释你的问题,因为 Spark 转换是惰性的,直到没有执行任何操作,在这种情况下是 foreach。有关转换和操作的完整列表,请参阅 spark.apache.org/docs/latest/…
  • @jaranda 你会把它变成答案吗?我认为是这样的。
  • @maasg 我刚刚提供了我的答案:)
  • 不要在 Spark 和 Hadoop 等分布式 MR 类型框架中推​​荐 printlns - 它们通常不会返回到您的控制台,而是在节点上某处的某个日志中输出。跨度>

标签: scala apache-spark


【解决方案1】:

graph.vertices 是 RDD 吗?这可以解释你的问题,因为 Spark 转换是惰性的,直到没有执行任何操作,foreach 在你的情况下:

val dt = getVerticesWithFeatureSeq(wikiGraph) //no result is computed yet, map transformation is 'recorded'
dt.foreach(println) //foreach action requires a result, this triggers the computation

RDD 会记住应用的转换,并且仅当操作需要将结果返回给驱动程序时才会计算它们。

您可以查看http://spark.apache.org/docs/latest/programming-guide.html#rdd-operations 了解更多详细信息以及可用转换和操作的列表。

【讨论】:

  • spark.apache.org/docs/latest/… 请注意 RDD 的打印元素。 collect() 然后打印出来更安全,而不是 foreach() 否则在集群上,语句将在多台机器上打印。
猜你喜欢
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2016-10-17
  • 1970-01-01
相关资源
最近更新 更多