org.apache.flink.graph.Triplet Java Examples

The following examples show how to use org.apache.flink.graph.Triplet. 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: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCsvWithConstantValueMapper() throws Exception {
	/*
	*Test fromCsvReader with edge path and a mapper that assigns a Double constant as value
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split = createTempFile(fileContent);

	Graph<Long, Double, String> graph = Graph.fromCsvReader(split.getPath().toString(),
			new AssignDoubleValueMapper(), env).types(Long.class, Double.class, String.class);

	List<Triplet<Long, Double, String>> result = graph.getTriplets().collect();
	//graph.getTriplets().writeAsCsv(resultPath);
	expectedResult = "1,2,0.1,0.1,ot\n" + "3,1,0.1,0.1,to\n" + "3,2,0.1,0.1,tt\n";
	compareResultAsTuples(result, expectedResult);
}
 
Example #2
Source File: GraphOperationsITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriplets() throws Exception {
	/*
	 * Test getTriplets()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

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

	DataSet<Triplet<Long, Long, Long>> data = graph.getTriplets();
	List<Triplet<Long, Long, Long>> result = data.collect();

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

	compareResultAsTuples(result, expectedResult);
}
 
Example #3
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithOnlyEdgesCsvFile() throws Exception {
	/*
	 * Test with one Csv file one with Edges data. Also tests the configuration method ignoreFistLineEdges()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent2 = "header\n1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";

	final FileInputSplit split2 = createTempFile(fileContent2);
	Graph<Long, NullValue, String> graph = Graph.fromCsvReader(split2.getPath().toString(), env)
			.ignoreFirstLineEdges()
			.ignoreCommentsVertices("hi")
			.edgeTypes(Long.class, String.class);

	List<Triplet<Long, NullValue, String>> result = graph.getTriplets().collect();
	expectedResult = "1,2,(null),(null),ot\n" +
			"3,2,(null),(null),tt\n" +
			"3,1,(null),(null),to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #4
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCsvWithConstantValueMapper() throws Exception {
	/*
	*Test fromCsvReader with edge path and a mapper that assigns a Double constant as value
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split = createTempFile(fileContent);

	Graph<Long, Double, String> graph = Graph.fromCsvReader(split.getPath().toString(),
			new AssignDoubleValueMapper(), env).types(Long.class, Double.class, String.class);

	List<Triplet<Long, Double, String>> result = graph.getTriplets().collect();
	//graph.getTriplets().writeAsCsv(resultPath);
	expectedResult = "1,2,0.1,0.1,ot\n" + "3,1,0.1,0.1,to\n" + "3,2,0.1,0.1,tt\n";
	compareResultAsTuples(result, expectedResult);
}
 
Example #5
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithCsvFile() throws Exception {
	/*
	 * Test with two Csv files one with Vertex Data and one with Edges data
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,1\n" +
			"2,2\n" +
			"3,3\n";
	final FileInputSplit split = createTempFile(fileContent);
	final String fileContent2 = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env)
			.types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #7
Source File: GraphOperationsITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriplets() throws Exception {
	/*
	 * Test getTriplets()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

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

	DataSet<Triplet<Long, Long, Long>> data = graph.getTriplets();
	List<Triplet<Long, Long, Long>> result = data.collect();

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

	compareResultAsTuples(result, expectedResult);
}
 
Example #8
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithOnlyEdgesCsvFile() throws Exception {
	/*
	 * Test with one Csv file one with Edges data. Also tests the configuration method ignoreFistLineEdges()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent2 = "header\n1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";

	final FileInputSplit split2 = createTempFile(fileContent2);
	Graph<Long, NullValue, String> graph = Graph.fromCsvReader(split2.getPath().toString(), env)
			.ignoreFirstLineEdges()
			.ignoreCommentsVertices("hi")
			.edgeTypes(Long.class, String.class);

	List<Triplet<Long, NullValue, String>> result = graph.getTriplets().collect();
	expectedResult = "1,2,(null),(null),ot\n" +
			"3,2,(null),(null),tt\n" +
			"3,1,(null),(null),to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #9
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #10
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithCsvFile() throws Exception {
	/*
	 * Test with two Csv files one with Vertex Data and one with Edges data
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,1\n" +
			"2,2\n" +
			"3,3\n";
	final FileInputSplit split = createTempFile(fileContent);
	final String fileContent2 = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env)
			.types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #11
Source File: GraphOperationsITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTriplets() throws Exception {
	/*
	 * Test getTriplets()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

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

	DataSet<Triplet<Long, Long, Long>> data = graph.getTriplets();
	List<Triplet<Long, Long, Long>> result = data.collect();

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

	compareResultAsTuples(result, expectedResult);
}
 
Example #12
Source File: GraphCreationWithCsvITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithOnlyEdgesCsvFile() throws Exception {
	/*
	 * Test with one Csv file one with Edges data. Also tests the configuration method ignoreFistLineEdges()
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent2 = "header\n1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";

	final FileInputSplit split2 = createTempFile(fileContent2);
	Graph<Long, NullValue, String> graph = Graph.fromCsvReader(split2.getPath().toString(), env)
			.ignoreFirstLineEdges()
			.ignoreCommentsVertices("hi")
			.edgeTypes(Long.class, String.class);

	List<Triplet<Long, NullValue, String>> result = graph.getTriplets().collect();
	expectedResult = "1,2,(null),(null),ot\n" +
			"3,2,(null),(null),tt\n" +
			"3,1,(null),(null),to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #13
Source File: GraphCreationWithCsvITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testCsvWithConstantValueMapper() throws Exception {
	/*
	*Test fromCsvReader with edge path and a mapper that assigns a Double constant as value
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split = createTempFile(fileContent);

	Graph<Long, Double, String> graph = Graph.fromCsvReader(split.getPath().toString(),
			new AssignDoubleValueMapper(), env).types(Long.class, Double.class, String.class);

	List<Triplet<Long, Double, String>> result = graph.getTriplets().collect();
	//graph.getTriplets().writeAsCsv(resultPath);
	expectedResult = "1,2,0.1,0.1,ot\n" + "3,1,0.1,0.1,to\n" + "3,2,0.1,0.1,tt\n";
	compareResultAsTuples(result, expectedResult);
}
 
Example #14
Source File: GraphCreationWithCsvITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #15
Source File: GraphCreationWithCsvITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithCsvFile() throws Exception {
	/*
	 * Test with two Csv files one with Vertex Data and one with Edges data
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	final String fileContent = "1,1\n" +
			"2,2\n" +
			"3,3\n";
	final FileInputSplit split = createTempFile(fileContent);
	final String fileContent2 = "1,2,ot\n" +
			"3,2,tt\n" +
			"3,1,to\n";
	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env)
			.types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);
}
 
Example #16
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCsvFileDelimiterConfiguration() throws Exception {
	/*
	 * Test with an Edge and Vertex csv file. Tests the configuration methods FieldDelimiterEdges and
	 * FieldDelimiterVertices
	 * Also tests the configuration methods LineDelimiterEdges and LineDelimiterVertices
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	final String fileContent = "header\n1;1\n" +
			"2;2\n" +
			"3;3\n";

	final FileInputSplit split = createTempFile(fileContent);

	final String fileContent2 = "header|1:2:ot|" +
			"3:2:tt|" +
			"3:1:to|";

	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env).
			ignoreFirstLineEdges().ignoreFirstLineVertices().
			fieldDelimiterEdges(":").fieldDelimiterVertices(";").
			lineDelimiterEdges("|").
			types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);

}
 
Example #17
Source File: GraphCreationWithCsvITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCsvFileDelimiterConfiguration() throws Exception {
	/*
	 * Test with an Edge and Vertex csv file. Tests the configuration methods FieldDelimiterEdges and
	 * FieldDelimiterVertices
	 * Also tests the configuration methods LineDelimiterEdges and LineDelimiterVertices
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	final String fileContent = "header\n1;1\n" +
			"2;2\n" +
			"3;3\n";

	final FileInputSplit split = createTempFile(fileContent);

	final String fileContent2 = "header|1:2:ot|" +
			"3:2:tt|" +
			"3:1:to|";

	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env).
			ignoreFirstLineEdges().ignoreFirstLineVertices().
			fieldDelimiterEdges(":").fieldDelimiterVertices(";").
			lineDelimiterEdges("|").
			types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);

}
 
Example #18
Source File: GraphCreationWithCsvITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCsvFileDelimiterConfiguration() throws Exception {
	/*
	 * Test with an Edge and Vertex csv file. Tests the configuration methods FieldDelimiterEdges and
	 * FieldDelimiterVertices
	 * Also tests the configuration methods LineDelimiterEdges and LineDelimiterVertices
	 */
	final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

	final String fileContent = "header\n1;1\n" +
			"2;2\n" +
			"3;3\n";

	final FileInputSplit split = createTempFile(fileContent);

	final String fileContent2 = "header|1:2:ot|" +
			"3:2:tt|" +
			"3:1:to|";

	final FileInputSplit split2 = createTempFile(fileContent2);

	Graph<Long, Long, String> graph = Graph.fromCsvReader(split.getPath().toString(), split2.getPath().toString(), env).
			ignoreFirstLineEdges().ignoreFirstLineVertices().
			fieldDelimiterEdges(":").fieldDelimiterVertices(";").
			lineDelimiterEdges("|").
			types(Long.class, Long.class, String.class);

	List<Triplet<Long, Long, String>> result = graph.getTriplets().collect();

	expectedResult = "1,2,1,2,ot\n" +
			"3,2,3,2,tt\n" +
			"3,1,3,1,to\n";

	compareResultAsTuples(result, expectedResult);

}
 
Example #19
Source File: EuclideanGraphWeighing.java    From Flink-CEPplus 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();

		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<>(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();
		}

	}
 
Example #20
Source File: EuclideanGraphWeighing.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();

		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<>(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();
		}

	}
 
Example #21
Source File: EuclideanGraphWeighing.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();

		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<>(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();
		}

	}