我有一个以这种方式创建的顶点和边的列表:
val v1 = new Vertex(1L, "foo")
val v2 = new Vertex(2L, "bar")
val e1 = new Edge(v1, v2, 0.5)`
并希望使用Graph.fromDataSet方法(或任何其他此类问题)创建Flink图。如何在Flink可读的内容中转换这些边和顶点?
谢谢!
答案 0 :(得分:1)
给定顶点列表val vertices: Seq[Vertex[Long, String]] = ...
和边val edges: Seq[Edge[Long, String]] = ...
,您可以使用Graph
方法创建Graph.fromCollection
:
val env = ExecutionEnvironment.getExecutionEnvironment
val vertices = Seq(new Vertex[Long, String](1L, "foo"), new Vertex[Long, String](2L, "bar"))
val edges = Seq(new Edge[Long, String](1L, 2L, "foobar"))
val graph = Graph.fromCollection(vertices, edges, env)
值得注意的是,您必须导入org.apache.flink.graph.scala.Graph
的Scala版本。
或者,您也可以先使用edgeDataset: DataSet[Edge[Long, String]]
创建vertexDataSet: DataSet[Vertex[Long, String]]
和ExecutionEnvironment
。然后可以使用Graph
方法创建Graph.fromDataSet
:
val vertexDataset = env.fromCollection(vertices)
val edgeDataset = env.fromCollection(edges)
val graph = Graph.fromDataSet(vertexDataset, edgeDataset, env)