Search in sources :

Example 6 with Triplet

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);
}
Also used : FileInputSplit(org.apache.flink.core.fs.FileInputSplit) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) NullValue(org.apache.flink.types.NullValue) Triplet(org.apache.flink.graph.Triplet) Test(org.junit.Test)

Example 7 with Triplet

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();
    }
}
Also used : Vertex(org.apache.flink.graph.Vertex) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Triplet(org.apache.flink.graph.Triplet) Tuple3(org.apache.flink.api.java.tuple.Tuple3) Edge(org.apache.flink.graph.Edge)

Aggregations

ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)7 Triplet (org.apache.flink.graph.Triplet)7 Test (org.junit.Test)6 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)5 NullValue (org.apache.flink.types.NullValue)2 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)1 Edge (org.apache.flink.graph.Edge)1 Vertex (org.apache.flink.graph.Vertex)1