|
| 1 | +package tutorial; |
| 2 | + |
| 3 | +// importing packages |
| 4 | + |
| 5 | +import com.google.gson.Gson; |
| 6 | +import org.apache.flink.api.common.functions.FilterFunction; |
| 7 | +import org.apache.flink.api.common.functions.FlatMapFunction; |
| 8 | +import org.apache.flink.api.common.functions.MapFunction; |
| 9 | +import org.apache.flink.api.java.DataSet; |
| 10 | +import org.apache.flink.api.java.ExecutionEnvironment; |
| 11 | +import org.apache.flink.api.java.utils.ParameterTool; |
| 12 | +import org.apache.flink.core.fs.FileSystem; |
| 13 | +import org.apache.flink.graph.Edge; |
| 14 | +import org.apache.flink.graph.Graph; |
| 15 | +import org.apache.flink.graph.Vertex; |
| 16 | +import org.apache.flink.graph.library.SingleSourceShortestPaths; |
| 17 | +import org.apache.flink.types.NullValue; |
| 18 | +import org.apache.flink.util.Collector; |
| 19 | +import scala.Tuple2; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +// bipartite graph -> use projection -> for recommendations |
| 24 | + |
| 25 | +/* |
| 26 | +Implementing Degree of Separation using Flink's Gelly Graph API |
| 27 | +*/ |
| 28 | +public class DegreeSeparation { |
| 29 | + final static Gson gson = new Gson(); |
| 30 | + |
| 31 | + public static void main(String[] args) throws Exception { |
| 32 | + |
| 33 | + // returns the execution environment (the context 'Local or Remote' in which a program is executed) |
| 34 | + // LocalEnvironment will cause execution in the current JVM |
| 35 | + // RemoteEnvironment will cause execution on a remote setup |
| 36 | + final ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment(); |
| 37 | + |
| 38 | + // provides utility methods for reading and parsing the program arguments |
| 39 | + // in this tutorial we will have to provide the input file and the output file as arguments |
| 40 | + final ParameterTool parameters = ParameterTool.fromArgs(args); |
| 41 | + |
| 42 | + // register parameters globally so it can be available for each node in the cluster |
| 43 | + environment.getConfig().setGlobalJobParameters(parameters); |
| 44 | + |
| 45 | + // read text file from the parameter 'input' passed in args |
| 46 | + // line-by-line and returns them as Strings |
| 47 | + DataSet<String> textLines = environment.readTextFile(parameters.get("input")); |
| 48 | + |
| 49 | + // Author -> Collaborating Author |
| 50 | + DataSet<Tuple2<String, String>> authors = textLines.flatMap(new Tokenizer()); |
| 51 | + |
| 52 | + // convert the dataset to edges in a graph |
| 53 | + DataSet<Edge<String, NullValue>> edges = authors.map(new MapFunction<Tuple2<String, String>, Edge<String, NullValue>>() { |
| 54 | + @Override |
| 55 | + public Edge<String, NullValue> map(Tuple2<String, String> value) { |
| 56 | + Edge<String, NullValue> edge = new Edge(); |
| 57 | + edge.setSource(value._1()); // author |
| 58 | + edge.setTarget(value._2()); // collaboration |
| 59 | + return edge; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + // creates graph from the edges generated |
| 64 | + Graph<String, NullValue, NullValue> collaborationGraph = Graph.fromDataSet(edges, environment); |
| 65 | + |
| 66 | + // we need to add weights since we will apply SingleSourceShortestPaths |
| 67 | + Graph<String, NullValue, Double> wCollaborationGraph = collaborationGraph.mapEdges(new MapFunction<Edge<String, NullValue>, Double>() { |
| 68 | + @Override |
| 69 | + public Double map(Edge<String, NullValue> stringNullValueEdge) { |
| 70 | + return 1.0; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + // use the SingleSourceShortestPaths to get all the collaboration authors for the collaboration authors |
| 75 | + // for a specified authors (similar to friends of friends) |
| 76 | + SingleSourceShortestPaths<String, NullValue> singleSourceShortestPaths = new SingleSourceShortestPaths<String, NullValue>(parameters.get("author"), 1000); |
| 77 | + DataSet<Vertex<String, Double>> result = singleSourceShortestPaths.run(wCollaborationGraph); |
| 78 | + |
| 79 | + System.out.println(result.count()); |
| 80 | + |
| 81 | + // the collaboration authors for the collaboration authors for a specified authors (similar to friends of friends) |
| 82 | + DataSet<Vertex<String, Double>> resultAuthor = result.filter(new FilterFunction<Vertex<String, Double>>() { |
| 83 | + @Override |
| 84 | + public boolean filter(Vertex<String, Double> value) { |
| 85 | + if (value.f1 == 2.0) { |
| 86 | + return true; |
| 87 | + } else { |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + // output the final result |
| 94 | + // check that the argument 'output' was passed to save in that path |
| 95 | + if (parameters.has("output")) { |
| 96 | + resultAuthor.writeAsText(parameters.get("output"), FileSystem.WriteMode.OVERWRITE); |
| 97 | + environment.execute("Graph API Tutorial"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class Tokenizer implements FlatMapFunction<String, Tuple2<String, String>> { |
| 102 | + |
| 103 | + @Override |
| 104 | + public void flatMap(String value, Collector<Tuple2<String, String>> out) { |
| 105 | + Publication publication = gson.fromJson(value, Publication.class); |
| 106 | + ArrayList<Author> authors = publication.getAuthors(); |
| 107 | + |
| 108 | + // no collaboration (one author) |
| 109 | + if (authors.size() <= 1) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + for (int i = 0; i < authors.size() - 1; i++) { |
| 114 | + String currentAuthor = authors.get(i).name; |
| 115 | + for (int j = i + 1; j < authors.size(); j++) { |
| 116 | + String collaboration = authors.get(j).name; |
| 117 | + |
| 118 | + // must output two tuples since we need to create |
| 119 | + // two edges for an undirected edge |
| 120 | + out.collect(new Tuple2<String, String>(currentAuthor, collaboration)); |
| 121 | + out.collect(new Tuple2<String, String>(collaboration, currentAuthor)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments