あなたはHDFSのソースを読んで、rdd
でデータを持っていたら、次のようなものを試すことができます。
import org.apache.spark.rdd.RDD
import org.apache.spark.graphx.Edge
// Sample data
val rdd = sc.parallelize(Seq("1: 1, 2, 3", "2: 2, 3"))
val edges: RDD[Edge[Int]] = rdd.flatMap {
row =>
// split around ":"
val splitted = row.split(":").map(_.trim)
// the value to the left of ":" is the source vertex:
val srcVertex = splitted(0).toLong
// for the values to the right of ":", we split around "," to get the other vertices
val otherVertices = splitted(1).split(",").map(_.trim)
// for each vertex to the right of ":", we create an Edge object connecting them to the srcVertex:
otherVertices.map(v => Edge(srcVertex, v.toLong, 1))
}
編集
また、お使いの頂点は一定のデフォルトの重みを持っている場合は、エッジからまっすぐにグラフを作成することができますので、頂点RDDを作成する必要はありません。
import org.apache.spark.graphx.Graph
val g = Graph.fromEdges(edges, defaultValue = 1)
thあなたのすべての助けのためのanks!私はあなたが言ったことに続き、それが働いているかどうかを確認する方法を見つけることを試みて、valグラフを作成することができました! –
私はあなたが言ったようにそれをやってみました、うまくいかなかったのはRDDでした[エッジ[Int]だから私はちょうどRDDを使いました。次のエラーが発生し続ける::43:エラー:見つからない:値エッジ otherVertices.map(v => Edge(srcVertex、v.toLong、1)) ^ :43:エラー:タイプの不一致。 found:Array [なし] 必須:TraversableOnce [?] otherVertices.map(v => Edge(srcVertex、v.toLong、1)) –
Edgeクラスをインポートしましたか? 'import org.apache.spark.graphx.Edge'。それはおそらく問題であり、またなぜRDD [Edge [Int]]がうまくいかなかったのですか –