Search in sources :

Example 76 with Edge

use of org.apache.flink.graph.Edge in project flink by apache.

the class GraphMutationsITCase method testRemoveInvalidVertex.

@Test
public void testRemoveInvalidVertex() throws Exception {
    /*
		 * Test removeVertex() -- remove an invalid vertex
		 */
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env), TestGraphUtils.getLongLongEdgeData(env), env);
    graph = graph.removeVertex(new Vertex<>(6L, 6L));
    DataSet<Edge<Long, Long>> data = graph.getEdges();
    List<Edge<Long, Long>> result = data.collect();
    expectedResult = "1,2,12\n" + "1,3,13\n" + "2,3,23\n" + "3,4,34\n" + "3,5,35\n" + "4,5,45\n" + "5,1,51\n";
    compareResultAsTuples(result, expectedResult);
}
Also used : Vertex(org.apache.flink.graph.Vertex) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Edge(org.apache.flink.graph.Edge) Test(org.junit.Test)

Example 77 with Edge

use of org.apache.flink.graph.Edge in project flink by apache.

the class GraphMutationsITCase method testRemoveOneValidOneInvalidEdge.

@Test
public void testRemoveOneValidOneInvalidEdge() throws Exception {
    /*
		 * Test removeEdges() -- one edge is valid, the other is invalid
		 */
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env), TestGraphUtils.getLongLongEdgeData(env), env);
    List<Edge<Long, Long>> edgesToBeRemoved = new ArrayList<>();
    edgesToBeRemoved.add(new Edge<>(1L, 1L, 51L));
    edgesToBeRemoved.add(new Edge<>(6L, 1L, 61L));
    graph = graph.removeEdges(edgesToBeRemoved);
    DataSet<Edge<Long, Long>> data = graph.getEdges();
    List<Edge<Long, Long>> result = data.collect();
    expectedResult = "1,2,12\n" + "1,3,13\n" + "2,3,23\n" + "3,4,34\n" + "3,5,35\n" + "4,5,45\n" + "5,1,51\n";
    compareResultAsTuples(result, expectedResult);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) ArrayList(java.util.ArrayList) Edge(org.apache.flink.graph.Edge) Test(org.junit.Test)

Example 78 with Edge

use of org.apache.flink.graph.Edge in project flink by apache.

the class GraphMutationsITCase method testAddEdges.

@Test
public void testAddEdges() throws Exception {
    /*
		 * Test addEdges() -- simple case
		 */
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env), TestGraphUtils.getLongLongEdgeData(env), env);
    List<Edge<Long, Long>> edgesToBeAdded = new ArrayList<>();
    edgesToBeAdded.add(new Edge<>(2L, 4L, 24L));
    edgesToBeAdded.add(new Edge<>(4L, 1L, 41L));
    graph = graph.addEdges(edgesToBeAdded);
    DataSet<Edge<Long, Long>> data = graph.getEdges();
    List<Edge<Long, Long>> result = data.collect();
    expectedResult = "1,2,12\n" + "1,3,13\n" + "2,3,23\n" + "2,4,24\n" + "3,4,34\n" + "3,5,35\n" + "4,1,41\n" + "4,5,45\n" + "5,1,51\n";
    compareResultAsTuples(result, expectedResult);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) ArrayList(java.util.ArrayList) Edge(org.apache.flink.graph.Edge) Test(org.junit.Test)

Example 79 with Edge

use of org.apache.flink.graph.Edge in project flink by apache.

the class GraphOperationsITCase method testUndirected.

@Test
public void testUndirected() throws Exception {
    /*
		 * Test getUndirected()
		 */
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env), TestGraphUtils.getLongLongEdgeData(env), env);
    DataSet<Edge<Long, Long>> data = graph.getUndirected().getEdges();
    List<Edge<Long, Long>> result = data.collect();
    expectedResult = "1,2,12\n" + "2,1,12\n" + "1,3,13\n" + "3,1,13\n" + "2,3,23\n" + "3,2,23\n" + "3,4,34\n" + "4,3,34\n" + "3,5,35\n" + "5,3,35\n" + "4,5,45\n" + "5,4,45\n" + "5,1,51\n" + "1,5,51\n";
    compareResultAsTuples(result, expectedResult);
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) Edge(org.apache.flink.graph.Edge) Test(org.junit.Test)

Example 80 with Edge

use of org.apache.flink.graph.Edge 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

Edge (org.apache.flink.graph.Edge)82 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)70 Test (org.junit.Test)66 Vertex (org.apache.flink.graph.Vertex)39 NullValue (org.apache.flink.types.NullValue)18 ArrayList (java.util.ArrayList)14 LongValue (org.apache.flink.types.LongValue)13 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)11 MapFunction (org.apache.flink.api.common.functions.MapFunction)6 ChecksumHashCode (org.apache.flink.graph.asm.dataset.ChecksumHashCode)6 Checksum (org.apache.flink.graph.asm.dataset.ChecksumHashCode.Checksum)6 LinkedList (java.util.LinkedList)5 Plan (org.apache.flink.api.common.Plan)5 FieldList (org.apache.flink.api.common.operators.util.FieldList)5 DataSet (org.apache.flink.api.java.DataSet)5 DiscardingOutputFormat (org.apache.flink.api.java.io.DiscardingOutputFormat)5 Graph (org.apache.flink.graph.Graph)5 Tuple2ToVertexMap (org.apache.flink.graph.utils.Tuple2ToVertexMap)5 DualInputPlanNode (org.apache.flink.optimizer.plan.DualInputPlanNode)5 OptimizedPlan (org.apache.flink.optimizer.plan.OptimizedPlan)5