use of org.apache.flink.graph.Triplet in project flink by apache.
the class GraphCreationWithCsvITCase method testCsvWithNullEdge.
@Test
public void testCsvWithNullEdge() throws Exception {
/*
Test fromCsvReader with edge and vertex path and nullvalue for edge
*/
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
final String vertexFileContent = "1,one\n" + "2,two\n" + "3,three\n";
final String edgeFileContent = "1,2\n" + "3,2\n" + "3,1\n";
final FileInputSplit split = createTempFile(vertexFileContent);
final FileInputSplit edgeSplit = createTempFile(edgeFileContent);
Graph<Long, String, NullValue> graph = Graph.fromCsvReader(split.getPath().toString(), edgeSplit.getPath().toString(), env).vertexTypes(Long.class, String.class);
List<Triplet<Long, String, NullValue>> result = graph.getTriplets().collect();
expectedResult = "1,2,one,two,(null)\n" + "3,2,three,two,(null)\n" + "3,1,three,one,(null)\n";
compareResultAsTuples(result, expectedResult);
}
use of org.apache.flink.graph.Triplet in project flink by apache.
the class EuclideanGraphWeighing method main.
public static void main(String[] args) throws Exception {
if (!parseParameters(args)) {
return;
}
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<Vertex<Long, Point>> vertices = getVerticesDataSet(env);
DataSet<Edge<Long, Double>> edges = getEdgesDataSet(env);
Graph<Long, Point, Double> graph = Graph.fromDataSet(vertices, edges, env);
// the edge value will be the Euclidean distance between its src and trg vertex
DataSet<Tuple3<Long, Long, Double>> edgesWithEuclideanWeight = graph.getTriplets().map(new MapFunction<Triplet<Long, Point, Double>, Tuple3<Long, Long, Double>>() {
@Override
public Tuple3<Long, Long, Double> map(Triplet<Long, Point, Double> triplet) throws Exception {
Vertex<Long, Point> srcVertex = triplet.getSrcVertex();
Vertex<Long, Point> trgVertex = triplet.getTrgVertex();
return new Tuple3<Long, Long, Double>(srcVertex.getId(), trgVertex.getId(), srcVertex.getValue().euclideanDistance(trgVertex.getValue()));
}
});
Graph<Long, Point, Double> resultedGraph = graph.joinWithEdges(edgesWithEuclideanWeight, new EdgeJoinFunction<Double, Double>() {
public Double edgeJoin(Double edgeValue, Double inputValue) {
return inputValue;
}
});
// retrieve the edges from the final result
DataSet<Edge<Long, Double>> result = resultedGraph.getEdges();
// emit result
if (fileOutput) {
result.writeAsCsv(outputPath, "\n", ",");
// since file sinks are lazy, we trigger the execution explicitly
env.execute("Euclidean Graph Weighing Example");
} else {
result.print();
}
}
Aggregations