I'm working with GraphX and Pregel with the Java API. I'm trying to implement a MaxValue Algorithm(Given a weighted graph and output is the max weight). But my implementation is not working:
public class Main {
public static void main(String[] args){
SparkConf conf = new SparkConf().setAppName("MaxValue").setMaster("spark://home:7077");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> text_file = sc.textFile(args[0]);
JavaRDD<String[]> text_file_arr = text_file.map(l -> l.split(" "));
//cache
text_file_arr.cache();
//create the vertex RDD
RDD<Tuple2<Object, Integer>> verteces = text_file_arr.map(
t-> new Tuple2<>((Object) Long.parseLong(t[0]), Integer.parseInt(t[t.length-1]))
).rdd();
//create edge RDD
RDD<Edge<Boolean>> edges = text_file_arr
.flatMap( l -> {
List<Edge<Boolean>> edgeList = new ArrayList<>();
long src = Long.parseLong(l[0]);
for (int i = 1;i<l.length-1;++i){
edgeList.add(new Edge(src,Long.parseLong(l[i]),true));
}
return edgeList.iterator();
})
.rdd();
//create the graph
Graph<Integer,Boolean> graph = Graph.apply(
verteces,
edges,
Integer.MIN_VALUE,
StorageLevel.MEMORY_AND_DISK(),
StorageLevel.MEMORY_AND_DISK(),
ClassTag$.MODULE$.apply(Integer.class),
ClassTag$.MODULE$.apply(Boolean.class)
);
graph.edges().toJavaRDD().collect().forEach(System.out::print);
graph.vertices().toJavaRDD().collect().forEach(System.out::print);
GraphOps<Integer,Boolean> graph_ops = new GraphOps<>(
graph,
ClassTag$.MODULE$.apply(Integer.class),
ClassTag$.MODULE$.apply(Boolean.class)
);
//run pregel
Graph<Integer,Boolean> graph_pregel = graph_ops.pregel(
Integer.MIN_VALUE,
3,
EdgeDirection.Either(),
new VProg(),
new SendMsg(),
new Merge(),
ClassTag$.MODULE$.apply(Integer.class)
);
graph_pregel.vertices().toJavaRDD().saveAsTextFile("out");
}
}
And this are the classes VProg, SendMsg and Merge.
class SendMsg extends AbstractFunction1<EdgeTriplet<Integer,Boolean>, Iterator<Tuple2<Object, Integer>>> implements Serializable {
@Override
public Iterator<Tuple2<Object, Integer>> apply(EdgeTriplet<Integer, Boolean> et) {
System.out.println(et.srcId()+" ---> "+et.dstId()+" with: "+et.srcAttr()+" ---> "+et.dstId());
if (et.srcAttr() > et.dstAttr()) {
return JavaConverters.asScalaIteratorConverter(Arrays.asList(et.toTuple()._1()).iterator()).asScala();
}else{
return JavaConverters.asScalaIteratorConverter(new ArrayList<Tuple2<Object, Integer>>().iterator()).asScala();
}
}
}
class VProg extends AbstractFunction3<Object, Integer, Integer, Integer> implements Serializable{
@Override
public Integer apply(Object l, Integer treeNodeThis, Integer treeNodeIn) {
if (treeNodeThis > treeNodeIn) {
System.out.println(l + " : " + treeNodeThis);
return treeNodeThis;
} else {
System.out.println(l + " : " + treeNodeIn);
return treeNodeIn;
}
}
}
class Merge extends AbstractFunction2<Integer, Integer, Integer> implements Serializable{
@Override
public Integer apply(Integer n1, Integer n2) {
return (n1>n2)? n1:n2;
}
}
The problem is, that after VProg runs on a node SendMsg is getting executed but the values aren't updated. That means, that VProg is returning the new value but the graph is still the inputed graph. I also tried other algorithms and got the same problem. Maybe I wrote my classes VProg, SendMsg or Merge wrong?
The graph is connected with 7 nodes and each node has the value 2^nodenumber.
I also tried with the class Pregel, same problem... I'm using Spark 2.0.0 and Java 8