在Spark GraphX中查找最大边缘权重

时间:2015-08-28 09:01:09

标签: scala apache-spark spark-graphx

假设我有一个带有边值属性和I的双值的图表 想要找到我的图表的最大边缘权重。如果我这样做:

val max = sc.accumulator(0.0) //max holds the maximum edge weight
g.edges.distinct.collect.foreach{ e => if (e.attr > max.value) max.value
= e.attr }

我想问一下在主人身上做了多少工作以及在主人身上做了多少工作 执行者,因为我知道collect()方法带来了整个RDD 大师?是否存在并行性?有没有更好的方法来找到 最大边缘重量?

注意:

g.edges.distinct.foreach{ e => if (e.attr > max.value) max.value =
e.attr } // does not work without the collect() method.
//I use an accumulator because I want to use the max edge weight later

如果我想对两个图之间具有相同srcId和dstId的边的属性应用一些平均函数,那么最好的方法是什么?

1 个答案:

答案 0 :(得分:4)

您可以聚合:

graph.edges.aggregate(Double.NegativeInfinity)(
  (m, e) => e.attr.max(m),
  (m1, m2) => m1.max(m2)
)

或map并取max:

 graph.edges.map(_.attr).max

关于你的尝试:

  1. 如果收集所有数据,则会在驱动程序上按顺序处理,因此没有理由使用accumulator
  2. 它不起作用,因为累加器是从工作者的角度来写的。
相关问题