-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_2.java
More file actions
executable file
·115 lines (102 loc) · 4.89 KB
/
Exercise_2.java
File metadata and controls
executable file
·115 lines (102 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package exercise_2;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.graphx.*;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.storage.StorageLevel;
import scala.Tuple2;
import scala.collection.Iterator;
import scala.collection.JavaConverters;
import scala.reflect.ClassTag$;
import scala.runtime.AbstractFunction1;
import scala.runtime.AbstractFunction2;
import scala.runtime.AbstractFunction3;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Exercise_2 {
private static class VProg extends AbstractFunction3<Long,Integer,Integer,Integer> implements Serializable {
@Override
public Integer apply(Long vertexID, Integer vertexValue, Integer message) {
if (message == Integer.MAX_VALUE) { // superstep 0
return vertexValue;
} else { // superstep > 0
return Math.min(vertexValue,message);
}
}
}
private static class sendMsg extends AbstractFunction1<EdgeTriplet<Integer,Integer>, Iterator<Tuple2<Object,Integer>>> implements Serializable {
@Override
public Iterator<Tuple2<Object, Integer>> apply(EdgeTriplet<Integer, Integer> triplet) {
Tuple2<Object,Integer> sourceVertex = triplet.toTuple()._1();
Tuple2<Object,Integer> dstVertex = triplet.toTuple()._2();
if(sourceVertex._2 != Integer.MAX_VALUE){
if(sourceVertex._2 + triplet.toTuple()._3()< dstVertex._2) {
//
return JavaConverters.asScalaIteratorConverter(Arrays.asList(new Tuple2<Object,Integer>(triplet.dstId(),sourceVertex._2 + triplet.toTuple()._3())).iterator()).asScala();
} else {
//do nothing
return JavaConverters.asScalaIteratorConverter(new ArrayList<Tuple2<Object,Integer>>().iterator()).asScala();
}
} else {
//do nothing
return JavaConverters.asScalaIteratorConverter(Arrays.asList(new Tuple2<Object,Integer>(triplet.dstId(),sourceVertex._2)).iterator()).asScala();
}
}
}
private static class merge extends AbstractFunction2<Integer,Integer,Integer> implements Serializable {
@Override
public Integer apply(Integer o, Integer o2) {
return Math.min(o, o2);
}
}
public static void shortestPaths(JavaSparkContext ctx) {
Map<Long, String> labels = ImmutableMap.<Long, String>builder()
.put(1l, "A")
.put(2l, "B")
.put(3l, "C")
.put(4l, "D")
.put(5l, "E")
.put(6l, "F")
.build();
List<Tuple2<Object,Integer>> vertices = Lists.newArrayList(
new Tuple2<Object,Integer>(1l,0),
new Tuple2<Object,Integer>(2l,Integer.MAX_VALUE),
new Tuple2<Object,Integer>(3l,Integer.MAX_VALUE),
new Tuple2<Object,Integer>(4l,Integer.MAX_VALUE),
new Tuple2<Object,Integer>(5l,Integer.MAX_VALUE),
new Tuple2<Object,Integer>(6l,Integer.MAX_VALUE)
);
List<Edge<Integer>> edges = Lists.newArrayList(
new Edge<Integer>(1l,2l, 4), // A --> B (4)
new Edge<Integer>(1l,3l, 2), // A --> C (2)
new Edge<Integer>(2l,3l, 5), // B --> C (5)
new Edge<Integer>(2l,4l, 10), // B --> D (10)
new Edge<Integer>(3l,5l, 3), // C --> E (3)
new Edge<Integer>(5l, 4l, 4), // E --> D (4)
new Edge<Integer>(4l, 6l, 11) // D --> F (11)
);
JavaRDD<Tuple2<Object,Integer>> verticesRDD = ctx.parallelize(vertices);
JavaRDD<Edge<Integer>> edgesRDD = ctx.parallelize(edges);
Graph<Integer,Integer> G = Graph.apply(verticesRDD.rdd(),edgesRDD.rdd(),1, StorageLevel.MEMORY_ONLY(), StorageLevel.MEMORY_ONLY(),
scala.reflect.ClassTag$.MODULE$.apply(Integer.class),scala.reflect.ClassTag$.MODULE$.apply(Integer.class));
GraphOps ops = new GraphOps(G, scala.reflect.ClassTag$.MODULE$.apply(Integer.class),scala.reflect.ClassTag$.MODULE$.apply(Integer.class));
ops.pregel(Integer.MAX_VALUE,
Integer.MAX_VALUE,
EdgeDirection.Out(),
new VProg(),
new sendMsg(),
new merge(),
ClassTag$.MODULE$.apply(Integer.class))
.vertices()
.toJavaRDD()
.foreach(v -> {
Tuple2<Object,Integer> vertex = (Tuple2<Object,Integer>)v;
System.out.println("Minimum cost to get from "+labels.get(1l)+" to "+labels.get(vertex._1)+" is "+vertex._2);
});
}
}