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

The following examples show how to use org.apache.flink.graph.Graph#getEdges() . 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: FromCollectionITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromCollectionVerticesEdges() throws Exception {
	/*
	 * Test fromCollection(vertices, edges):
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	Graph<Long, Long, Long> graph = Graph.fromCollection(TestGraphUtils.getLongLongVertices(),
		TestGraphUtils.getLongLongEdges(), env);

	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);
}
 
Example 2
Source File: TriangleEnumerator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public DataSet<Tuple3<K, K, K>> run(Graph<K, VV, EV> input) throws Exception {

	DataSet<Edge<K, EV>> edges = input.getEdges();

	// annotate edges with degrees
	DataSet<EdgeWithDegrees<K>> edgesWithDegrees = edges.flatMap(new EdgeDuplicator<>())
			.groupBy(0).sortGroup(1, Order.ASCENDING).reduceGroup(new DegreeCounter<>())
			.groupBy(EdgeWithDegrees.V1, EdgeWithDegrees.V2).reduce(new DegreeJoiner<>());

	// project edges by degrees
	DataSet<Edge<K, NullValue>> edgesByDegree = edgesWithDegrees.map(new EdgeByDegreeProjector<>());
	// project edges by vertex id
	DataSet<Edge<K, NullValue>> edgesById = edgesByDegree.map(new EdgeByIdProjector<>());

	DataSet<Tuple3<K, K, K>> triangles = edgesByDegree
			// build triads
			.groupBy(EdgeWithDegrees.V1).sortGroup(EdgeWithDegrees.V2, Order.ASCENDING)
			.reduceGroup(new TriadBuilder<>())
			// filter triads
			.join(edgesById, JoinHint.REPARTITION_HASH_SECOND).where(Triad.V2, Triad.V3).equalTo(0, 1).with(new TriadFilter<>());

	return triangles;
}
 
Example 3
Source File: JoinWithEdgesITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCustomType() throws Exception {
	/*
     * Test joinWithEdges 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.joinWithEdges(TestGraphUtils.getLongLongCustomTuple3Data(env),
		new CustomValueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

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

	compareResultAsTuples(result, expectedResult);
}
 
Example 4
Source File: GraphMutationsITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCustomType() throws Exception {
	/*
     * Test joinWithEdges 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.joinWithEdges(TestGraphUtils.getLongLongCustomTuple3Data(env),
		new CustomValueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

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

	compareResultAsTuples(result, expectedResult);
}
 
Example 6
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: GraphOperationsITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnion() throws Exception {
	/*
	 * Test union()
	 */
	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<>();
	List<Edge<Long, Long>> edges = new ArrayList<>();

	vertices.add(new Vertex<>(6L, 6L));
	edges.add(new Edge<>(6L, 1L, 61L));

	graph = graph.union(Graph.fromCollection(vertices, edges, env));

	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" +
		"6,1,61\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 8
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnTargetWithDifferentType() throws Exception {
	/*
	 * Test joinWithEdgesOnTarget with the input DataSet passed as a parameter containing
	 * less elements than the edge 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.joinWithEdgesOnTarget(graph.getEdges().first(3)
		.map(new ProjectTargetWithTrueMapper()), new DoubleIfTrueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,24\n" +
		"1,3,26\n" +
		"2,3,46\n" +
		"3,4,34\n" +
		"3,5,35\n" +
		"4,5,45\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 9
Source File: GraphOperationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnion() throws Exception {
	/*
	 * Test union()
	 */
	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<>();
	List<Edge<Long, Long>> edges = new ArrayList<>();

	vertices.add(new Vertex<>(6L, 6L));
	edges.add(new Edge<>(6L, 1L, 61L));

	graph = graph.union(Graph.fromCollection(vertices, edges, env));

	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" +
		"6,1,61\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 10
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveSameEdgeTwice() throws Exception {
	/*
	 * Test removeEdges() -- try to remove the same edge twice
	 */

	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<>(5L, 1L, 51L));
	edgesToBeRemoved.add(new Edge<>(5L, 1L, 51L));

	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";

	compareResultAsTuples(result, expectedResult);
}
 
Example 11
Source File: GraphMutationsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveEdges() throws Exception {
	/*
	 * Test removeEdges() -- 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>> edgesToBeRemoved = new ArrayList<>();
	edgesToBeRemoved.add(new Edge<>(5L, 1L, 51L));
	edgesToBeRemoved.add(new Edge<>(2L, 3L, 23L));

	// duplicate edge should be preserved in output
	graph = graph.addEdge(new Vertex<>(1L, 1L), new Vertex<>(2L, 2L), 12L);

	graph = graph.removeEdges(edgesToBeRemoved);

	DataSet<Edge<Long, Long>> data = graph.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,12\n" +
			"1,2,12\n" +
			"1,3,13\n" +
			"3,4,34\n" +
			"3,5,35\n" +
			"4,5,45\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 12
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnSourceWithNoCommonKeys() throws Exception {
	/*
	 * Test joinWithEdgesOnSource with the input DataSet containing different keys than the edge 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.joinWithEdgesOnSource(TestGraphUtils.getLongLongTuple2SourceData(env),
		new DoubleValueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,20\n" +
		"1,3,20\n" +
		"2,3,60\n" +
		"3,4,80\n" +
		"3,5,80\n" +
		"4,5,120\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 13
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnSourceWithDifferentType() throws Exception {
	/*
	 * Test joinWithEdgesOnSource with the input DataSet passed as a parameter containing
	 * less elements than the edge 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.joinWithEdgesOnSource(graph.getEdges().first(3)
		.map(new ProjectSourceWithTrueMapper()), new DoubleIfTrueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,24\n" +
		"1,3,26\n" +
		"2,3,46\n" +
		"3,4,34\n" +
		"3,5,35\n" +
		"4,5,45\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 14
Source File: GraphMutationsITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveEdges() throws Exception {
	/*
	 * Test removeEdges() -- 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>> edgesToBeRemoved = new ArrayList<>();
	edgesToBeRemoved.add(new Edge<>(5L, 1L, 51L));
	edgesToBeRemoved.add(new Edge<>(2L, 3L, 23L));

	// duplicate edge should be preserved in output
	graph = graph.addEdge(new Vertex<>(1L, 1L), new Vertex<>(2L, 2L), 12L);

	graph = graph.removeEdges(edgesToBeRemoved);

	DataSet<Edge<Long, Long>> data = graph.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,12\n" +
			"1,2,12\n" +
			"1,3,13\n" +
			"3,4,34\n" +
			"3,5,35\n" +
			"4,5,45\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 15
Source File: JoinWithEdgesITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithEdgesOnSource() throws Exception {
	/*
	 * Test joinWithEdgesOnSource with the input DataSet parameter identical
	 * to the edge 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.joinWithEdgesOnSource(graph.getEdges()
		.map(new ProjectSourceAndValueMapper()), new AddValuesMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,24\n" +
		"1,3,25\n" +
		"2,3,46\n" +
		"3,4,68\n" +
		"3,5,69\n" +
		"4,5,90\n" +
		"5,1,102\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 16
Source File: JoinWithEdgesITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithLessElementsDifferentType() throws Exception {
	/*
	 * Test joinWithEdges with the input DataSet passed as a parameter containing
	 * less elements than the edge 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.joinWithEdges(graph.getEdges().first(3)
		.map(new BooleanEdgeValueMapper()), new DoubleIfTrueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,24\n" +
		"1,3,26\n" +
		"2,3,46\n" +
		"3,4,34\n" +
		"3,5,35\n" +
		"4,5,45\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 17
Source File: GraphMutationsITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 18
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnSourceWithNoCommonKeys() throws Exception {
	/*
	 * Test joinWithEdgesOnSource with the input DataSet containing different keys than the edge 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.joinWithEdgesOnSource(TestGraphUtils.getLongLongTuple2SourceData(env),
		new DoubleValueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,20\n" +
		"1,3,20\n" +
		"2,3,60\n" +
		"3,4,80\n" +
		"3,5,80\n" +
		"4,5,120\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 19
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithOnTargetWithLessElements() throws Exception {
	/*
	 * Test joinWithEdgesOnTarget with the input DataSet passed as a parameter containing
	 * less elements than the edge DataSet, but of the same type
	 */
	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.joinWithEdgesOnTarget(graph.getEdges().first(3)
		.map(new ProjectTargetAndValueMapper()), new AddValuesMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,24\n" +
		"1,3,26\n" +
		"2,3,36\n" +
		"3,4,34\n" +
		"3,5,35\n" +
		"4,5,45\n" +
		"5,1,51\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example 20
Source File: JoinWithEdgesITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnTargetWithNoCommonKeys() throws Exception {
	/*
	 * Test joinWithEdgesOnTarget with the input DataSet containing different keys than the edge 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.joinWithEdgesOnTarget(TestGraphUtils.getLongLongTuple2TargetData(env),
		new DoubleValueMapper());

	DataSet<Edge<Long, Long>> data = res.getEdges();
	List<Edge<Long, Long>> result = data.collect();

	expectedResult = "1,2,20\n" +
		"1,3,40\n" +
		"2,3,40\n" +
		"3,4,80\n" +
		"3,5,35\n" +
		"4,5,45\n" +
		"5,1,140\n";

	compareResultAsTuples(result, expectedResult);
}