假设我有一个顶点数组,我想以每个顶点连接到下一个 x 顶点的方式从它们创建边。x 可以有任何整数值。有没有办法用 Spark 做到这一点?
到目前为止,这就是我对 Scala 的看法:
//array that holds the edges
var edges = Array.empty[Edge[Double]]
for(j <- 0 to vertices.size - 2) {
for(i <- 1 to x) {
if((j+i) < vertices.size) {
//add edge
edges = edges ++ Array(Edge(vertices(j)._1, vertices(j+i)._1, 1.0))
//add inverse edge, we want both directions
edges = edges ++ Array(Edge(vertices(j+i)._1, vertices(j)._1, 1.0))
}
}
}
其中 vertices 变量是 (Long, String) 的数组。但整个过程当然是连续的。
编辑:
例如,如果我有这样的顶点:Hello, World, and, Planet cosmos. 我需要以下边缘:Hello -> World, World -> Hello, Hello -> and, and -> Hello, Hello-> Planet, Planet -> Hello, World -> and, and -> World, World -> Planet, Planet -> World, World -> cosmos, cosmos -> World, 等等。