3

This question is about Spark GraphX. I want to compute a subgraph by removing nodes that are neighbors of certain other nodes.

Example

[Task] Retain A nodes and B nodes that are not neighbors of C2 nodes.

Input graph:

                    ┌────┐
              ┌─────│ A  │──────┐
              │     └────┘      │
              v                 v
┌────┐     ┌────┐            ┌────┐     ┌────┐
│ C1 │────>│ B  │            │ B  │<────│ C2 │
└────┘     └────┘            └────┘     └────┘
              ^                 ^
              │     ┌────┐      │
              └─────│ A  │──────┘
                    └────┘

Output graph:

         ┌────┐
   ┌─────│ A  │
   │     └────┘
   v           
┌────┐         
│ B  │         
└────┘         
   ^           
   │     ┌────┐
   └─────│ A  │
         └────┘

How to elegantly write a GraphX query that returns the output graph?

4

3 回答 3

3

一种不同的查找val nodesAB方式GraphOps.collectNeighbors

val nodesAB = graph.collectNeighbors(EdgeDirection.Either)
  .filter{case (vid,ns) => ! ns.map(_._2).contains("C2")}.map(_._1)
  .intersection(
    graph.vertices
      .filter{case (vid,attr) => ! attr.toString.startsWith("C") }.map(_._1)
  )

其余的工作方式与您相同:

val solution1 = Graph(nodesAB, graph.edges) .
subgraph(vpred = {case(id, label) => label != null})

如果您想使用可能(?)更具可扩展性的 DataFrame,那么首先我们需要将 nodesAB 转换为 DataFrame:

val newNodes = sqlContext.createDataFrame(
  nodesAB,
  StructType(Array(StructField("newNode", LongType, false)))
)

你用这个创建和边缘 DataFrame:

val edgeDf = sqlContext.createDataFrame(
  graph.edges.map{edge => Row(edge.srcId, edge.dstId, edge.attr)}, 
  StructType(Array(
    StructField("srcId", LongType, false),
    StructField("dstId", LongType, false),
    StructField("attr", LongType, false)
  ))
)

然后,您可以这样做以创建没有子图的图:

val solution1 = Graph(
  nodesAB, 
  edgeDf
  .join(newNodes, $"srcId" === $"newNode").select($"srcId", $"dstId", $"attr")
  .join(newNodes, $"dstId" === $"newNode")
  .rdd.map(row => Edge(row.getLong(0), row.getLong(1), row.getLong(2)))
)
于 2015-05-22T21:14:15.623 回答
3

这是另一个解决方案。此解决方案使用 aggregateMessages 将整数 (1) 发送到应从图中删除的那些 B。生成的顶点集与图连接,随后的子图调用从输出图中删除不需要的 B。

// Step 1: send the message (1) to vertices that should be removed   
val deleteMe = graph.aggregateMessages[Int](
    ctx => {
      if (ctx.dstAttr.equals("B") && ctx.srcAttr.equals("C")) {
        ctx.sendToDst(1) // 1 means delete, but number is not actually used
      }
    },
    (a,b) => a  // choose either message, they are all (1)
  )

  // Step 2: join vertex sets, original and deleteMe
  val joined = graph.outerJoinVertices(deleteMe) {
    (id, origValue, msgValue ) => msgValue match {
      case Some(number) => "deleteme"  // vertex received msg
      case None => origValue
    }
  }

  // Step 3: Remove nodes with domain = deleteme
  joined.subgraph(vpred = (id, data) => data.equals("deleteme"))

我正在考虑一种只使用一个中间删除标志的方法,例如“deleteme”,而不是 1 和“deleteme”。但这是一件好事,因为我能做到这一点。

于 2015-05-29T08:25:12.193 回答
0

一种解决方案是使用三元组视图来识别与 C1 节点相邻的 B 节点子集。接下来,将它们与 A 节点联合。接下来,创建一个新图:

// Step 1
// Compute the subset of B's that are neighbors with C1
val nodesBC1 = graph.triplets .
    filter {trip => trip.srcAttr == "C1"} .
    map {trip => (trip.dstId, trip.dstAttr)}

// Step 2    
// Union the subset B's with all the A's
val nodesAB = nodesBC1 .
    union(graph.vertices filter {case (id, label) => label == "A"})

// Step 3
// Create a graph using the subset nodes and all the original edges
// Remove nodes that have null values
val solution1 = Graph(nodesAB, graph.edges) .
    subgraph(vpred = {case(id, label) => label != null})

在第 1 步中,我通过将三元组视图的 dstID 和 dstAttr 映射在一起来重新创建节点 RDD(包含 B 节点)。不确定这对于大图的效率如何?

于 2015-05-19T13:47:05.133 回答