Java Code Examples for org.apache.flink.graph.Graph#getVertices()

The following examples show how to use org.apache.flink.graph.Graph#getVertices() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphCreationITCase.java    From flink with Apache License 2.0 7 votes vote down vote up
@Test
public void testCreateWithMapper() throws Exception {
	/*
	 * Test create() with edge dataset and a mapper that assigns the id as value
     */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongEdgeData(env),
		new AssignIdAsValueMapper(), env);

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,1\n" +
		"2,2\n" +
		"3,3\n" +
		"4,4\n" +
		"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 2
Source File: GraphCreationWithMapperITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithConstantValueMapper() throws Exception {
	/*
	 * Test create() with edge dataset with String key type
	 * and a mapper that assigns a double constant as value
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<String, Double, Long> graph = Graph.fromDataSet(TestGraphUtils.getStringLongEdgeData(env),
		new AssignDoubleConstantMapper(), env);

	DataSet<Vertex<String, Double>> data = graph.getVertices();
	List<Vertex<String, Double>> result = data.collect();

	expectedResult = "1,0.1\n" +
		"2,0.1\n" +
		"3,0.1\n" +
		"4,0.1\n" +
		"5,0.1\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 3
Source File: JoinWithVerticesITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testJoinWithVertexSet() throws Exception {
	/*
	 * Test joinWithVertices with the input DataSet parameter identical
	 * to the vertex DataSet
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
		TestGraphUtils.getLongLongEdgeData(env), env);

	Graph<Long, Long, Long> res = graph.joinWithVertices(graph.getVertices()
		.map(new VertexToTuple2Map<>()), new AddValuesMapper());

	DataSet<Vertex<Long, Long>> data = res.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,2\n" +
		"2,4\n" +
		"3,6\n" +
		"4,8\n" +
		"5,10\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 4
Source File: GraphCreationWithMapperITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDoubleValueMapper() throws Exception {
	/*
	 * Test create() with edge dataset and a mapper that assigns a double constant as value
     */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, Double, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongEdgeData(env),
		new AssignDoubleValueMapper(), env);

	DataSet<Vertex<Long, Double>> data = graph.getVertices();
	List<Vertex<Long, Double>> result = data.collect();

	expectedResult = "1,0.1\n" +
		"2,0.1\n" +
		"3,0.1\n" +
		"4,0.1\n" +
		"5,0.1\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 5
Source File: JoinWithVerticesITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDifferentType() throws Exception {
	/*
	 * Test joinWithVertices with the input DataSet passed as a parameter containing
	 * less elements than the vertex DataSet and of a different type(Boolean)
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
		TestGraphUtils.getLongLongEdgeData(env), env);

	Graph<Long, Long, Long> res = graph.joinWithVertices(graph.getVertices().first(3)
		.map(new ProjectIdWithTrue()), new DoubleIfTrueMapper());

	DataSet<Vertex<Long, Long>> data = res.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,2\n" +
		"2,4\n" +
		"3,6\n" +
		"4,4\n" +
		"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 6
Source File: GraphCreationITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithCustomVertexValue() throws Exception {
	/*
	 * Test create() with edge dataset and a mapper that assigns a parametrized custom vertex value
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, DummyCustomParameterizedType<Double>, Long> graph = Graph.fromDataSet(
		TestGraphUtils.getLongLongEdgeData(env), new AssignCustomVertexValueMapper(), env);

	DataSet<Vertex<Long, DummyCustomParameterizedType<Double>>> data = graph.getVertices();
	List<Vertex<Long, DummyCustomParameterizedType<Double>>> result = data.collect();

	expectedResult = "1,(2.0,0)\n" +
		"2,(4.0,1)\n" +
		"3,(6.0,2)\n" +
		"4,(8.0,3)\n" +
		"5,(10.0,4)\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 7
Source File: FromCollectionITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromCollectionEdgesNoInitialValue() throws Exception {
	/*
        * Test fromCollection(edges) with no initial value for the vertices
        */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, NullValue, Long> graph = Graph.fromCollection(TestGraphUtils.getLongLongEdges(),
		env);

	DataSet<Vertex<Long, NullValue>> data = graph.getVertices();
	List<Vertex<Long, NullValue>> result = data.collect();

	expectedResult = "1,(null)\n" +
		"2,(null)\n" +
		"3,(null)\n" +
		"4,(null)\n" +
		"5,(null)\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 8
Source File: GraphCreationITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithoutVertexValues() throws Exception {
	/*
	 * Test create() with edge dataset and no vertex values
     */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, NullValue, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongEdgeData(env), env);

	DataSet<Vertex<Long, NullValue>> data = graph.getVertices();
	List<Vertex<Long, NullValue>> result = data.collect();

	expectedResult = "1,(null)\n" +
		"2,(null)\n" +
		"3,(null)\n" +
		"4,(null)\n" +
		"5,(null)\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 9
Source File: GraphMutationsITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddVertexExisting() throws Exception {
	/*
	 * Test addVertex() -- add an existing vertex
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
			TestGraphUtils.getLongLongEdgeData(env), env);

	graph = graph.addVertex(new Vertex<>(1L, 1L));

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,1\n" +
			"2,2\n" +
			"3,3\n" +
			"4,4\n" +
			"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 10
Source File: JoinWithVerticesITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCustomType() throws Exception {
	/*
	 * Test joinWithVertices with a DataSet containing custom parametrised type input values
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
		TestGraphUtils.getLongLongEdgeData(env), env);

	Graph<Long, Long, Long> res = graph.joinWithVertices(TestGraphUtils.getLongCustomTuple2Data(env),
		new CustomValueMapper());

	DataSet<Vertex<Long, Long>> data = res.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,10\n" +
		"2,20\n" +
		"3,30\n" +
		"4,40\n" +
		"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 11
Source File: FromCollectionITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromCollectionEdgesWithInitialValue() throws Exception {
	/*
        * Test fromCollection(edges) with vertices initialised by a
        * function that takes the id and doubles it
        */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, Long, Long> graph = Graph.fromCollection(TestGraphUtils.getLongLongEdges(),
		new InitVerticesMapper(), env);

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,2\n" +
		"2,4\n" +
		"3,6\n" +
		"4,8\n" +
		"5,10\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 12
Source File: JoinWithVerticesITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDifferentKeys() throws Exception {
	/*
	 * Test joinWithVertices with an input DataSet containing different keys than the vertex DataSet
	 * - the iterator becomes empty.
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
		TestGraphUtils.getLongLongEdgeData(env), env);

	Graph<Long, Long, Long> res = graph.joinWithVertices(TestGraphUtils.getLongLongTuple2Data(env),
		new ProjectSecondMapper());

	DataSet<Vertex<Long, Long>> data = res.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,10\n" +
		"2,20\n" +
		"3,30\n" +
		"4,40\n" +
		"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 13
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddVertexExisting() throws Exception {
	/*
	 * Test addVertex() -- add an existing vertex
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
			TestGraphUtils.getLongLongEdgeData(env), env);

	graph = graph.addVertex(new Vertex<>(1L, 1L));

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,1\n" +
			"2,2\n" +
			"3,3\n" +
			"4,4\n" +
			"5,5\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 14
Source File: GraphCreationITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithoutVertexValues() throws Exception {
	/*
	 * Test create() with edge dataset and no vertex values
     */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, NullValue, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongEdgeData(env), env);

	DataSet<Vertex<Long, NullValue>> data = graph.getVertices();
	List<Vertex<Long, NullValue>> result = data.collect();

	expectedResult = "1,(null)\n" +
		"2,(null)\n" +
		"3,(null)\n" +
		"4,(null)\n" +
		"5,(null)\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 15
Source File: SingleSourceShortestPaths.java    From flink with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		if (!parseParameters(args)) {
			return;
		}

		ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

		DataSet<Edge<Long, Double>> edges = getEdgesDataSet(env);

		Graph<Long, Double, Double> graph = Graph.fromDataSet(edges, new InitVertices(srcVertexId), env);

		// Execute the scatter-gather iteration
		Graph<Long, Double, Double> result = graph.runScatterGatherIteration(
				new MinDistanceMessenger(), new VertexDistanceUpdater(), maxIterations);

		// Extract the vertices as the result
		DataSet<Vertex<Long, Double>> singleSourceShortestPaths = result.getVertices();

		// emit result
		if (fileOutput) {
			singleSourceShortestPaths.writeAsCsv(outputPath, "\n", ",");

			// since file sinks are lazy, we trigger the execution explicitly
			env.execute("Single Source Shortest Paths Example");
		} else {
			singleSourceShortestPaths.print();
		}

	}
 
Example 16
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddVerticesOneExisting() throws Exception {
	/*
	 * Test addVertices() -- add an existing vertex
	 */

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
			TestGraphUtils.getLongLongEdgeData(env), env);

	List<Vertex<Long, Long>> vertices = new ArrayList<>();
	vertices.add(new Vertex<>(1L, 1L));
	vertices.add(new Vertex<>(6L, 6L));

	graph = graph.addVertices(vertices);

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,1\n" +
			"2,2\n" +
			"3,3\n" +
			"4,4\n" +
			"5,5\n" +
			"6,6\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 17
Source File: GatherSumApplyConfigurationITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunWithConfiguration() throws Exception {
	/*
	 * Test Graph's runGatherSumApplyIteration when configuration parameters are provided
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromCollection(TestGraphUtils.getLongLongVertices(),
		TestGraphUtils.getLongLongEdges(), env).mapVertices(new AssignOneMapper());

	// create the configuration object
	GSAConfiguration parameters = new GSAConfiguration();

	parameters.addBroadcastSetForGatherFunction("gatherBcastSet", env.fromElements(1, 2, 3));
	parameters.addBroadcastSetForSumFunction("sumBcastSet", env.fromElements(4, 5, 6));
	parameters.addBroadcastSetForApplyFunction("applyBcastSet", env.fromElements(7, 8, 9));
	parameters.registerAggregator("superstepAggregator", new LongSumAggregator());
	parameters.setOptNumVertices(true);

	Graph<Long, Long, Long> res = graph.runGatherSumApplyIteration(new Gather(), new Sum(),
		new Apply(), 10, parameters);

	DataSet<Vertex<Long, Long>> data = res.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,11\n" +
		"2,11\n" +
		"3,11\n" +
		"4,11\n" +
		"5,11";

	compareResultAsTuples(result, expectedResult);
}
 
Example 18
Source File: IncrementalSSSPITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncrementalSSSPNonSPEdge() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	DataSet<Vertex<Long, Double>> vertices = IncrementalSSSPData.getDefaultVertexDataSet(env);
	DataSet<Edge<Long, Double>> edges = IncrementalSSSPData.getDefaultEdgeDataSet(env);
	DataSet<Edge<Long, Double>> edgesInSSSP = IncrementalSSSPData.getDefaultEdgesInSSSP(env);
	// the edge to be removed is a non-SP edge
	Edge<Long, Double> edgeToBeRemoved = new Edge<>(3L, 5L, 5.0);

	Graph<Long, Double, Double> graph = Graph.fromDataSet(vertices, edges, env);
	// Assumption: all minimum weight paths are kept
	Graph<Long, Double, Double> ssspGraph = Graph.fromDataSet(vertices, edgesInSSSP, env);
	// remove the edge
	graph.removeEdge(edgeToBeRemoved);

	// configure the iteration
	ScatterGatherConfiguration parameters = new ScatterGatherConfiguration();

	if (IncrementalSSSP.isInSSSP(edgeToBeRemoved, edgesInSSSP)) {

		parameters.setDirection(EdgeDirection.IN);
		parameters.setOptDegrees(true);

		// run the scatter gather iteration to propagate info
		Graph<Long, Double, Double> result = ssspGraph.runScatterGatherIteration(
				new IncrementalSSSP.InvalidateMessenger(edgeToBeRemoved),
				new IncrementalSSSP.VertexDistanceUpdater(),
				IncrementalSSSPData.NUM_VERTICES, parameters);

		DataSet<Vertex<Long, Double>> resultedVertices = result.getVertices();

		resultedVertices.writeAsCsv(resultPath, "\n", ",");
		env.execute();
	} else {
		vertices.writeAsCsv(resultPath, "\n", ",");
		env.execute();
	}

	expected = IncrementalSSSPData.VERTICES;
}
 
Example 19
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddVerticesOneExisting() throws Exception {
	/*
	 * Test addVertices() -- add an existing vertex
	 */

	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	Graph<Long, Long, Long> graph = Graph.fromDataSet(TestGraphUtils.getLongLongVertexData(env),
			TestGraphUtils.getLongLongEdgeData(env), env);

	List<Vertex<Long, Long>> vertices = new ArrayList<>();
	vertices.add(new Vertex<>(1L, 1L));
	vertices.add(new Vertex<>(6L, 6L));

	graph = graph.addVertices(vertices);

	DataSet<Vertex<Long, Long>> data = graph.getVertices();
	List<Vertex<Long, Long>> result = data.collect();

	expectedResult = "1,1\n" +
			"2,2\n" +
			"3,3\n" +
			"4,4\n" +
			"5,5\n" +
			"6,6\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 20
Source File: IncrementalSSSP.java    From flink with Apache License 2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {

		if (!parseParameters(args)) {
			return;
		}

		ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

		Edge<Long, Double> edgeToBeRemoved = getEdgeToBeRemoved();

		Graph<Long, Double, Double> graph = IncrementalSSSP.getGraph(env);

		// Assumption: all minimum weight paths are kept
		Graph<Long, Double, Double> ssspGraph = IncrementalSSSP.getSSSPGraph(env);

		// remove the edge
		graph.removeEdge(edgeToBeRemoved);

		// configure the iteration
		ScatterGatherConfiguration parameters = new ScatterGatherConfiguration();

		if (isInSSSP(edgeToBeRemoved, ssspGraph.getEdges())) {

			parameters.setDirection(EdgeDirection.IN);
			parameters.setOptDegrees(true);

			// run the scatter-gather iteration to propagate info
			Graph<Long, Double, Double> result = ssspGraph.runScatterGatherIteration(new InvalidateMessenger(edgeToBeRemoved),
					new VertexDistanceUpdater(), maxIterations, parameters);

			DataSet<Vertex<Long, Double>> resultedVertices = result.getVertices();

			// Emit results
			if (fileOutput) {
				resultedVertices.writeAsCsv(outputPath, "\n", ",");
				env.execute("Incremental SSSP Example");
			} else {
				resultedVertices.print();
			}
		} else {
			// print the vertices
			if (fileOutput) {
				graph.getVertices().writeAsCsv(outputPath, "\n", ",");
				env.execute("Incremental SSSP Example");
			} else {
				graph.getVertices().print();
			}
		}
	}