0
votes

I have two pair RDDs let say

 RDD1 : [(1,a),(2,b),(3,c)]    
 RDD2 : [(1,d),(2,e),(3,f)]

Now am joining these RDDs using join

 RDD3 = RDD1.join(RDD2);

And i have displayed the elements in RDD3 with below code

 for(Tuple2<Integer,Tuple2<String,String>> tuple : RDD3.collect()) 
                      System.out.println(tuple._1()+":"+tuple._2()._1()+","+tuple._2()._2());

i have seen weird results like

5:b,e
4:a,d 
6:c,f

where as i expected like

1:a,d
1:b,e 
1:c,f

Is there any way to get a desired output like the above ? or else am interpreting RDD behavior wrongly ? Please suggest

Edit :

Actually i am reading data like this

JavaDoubleRDD data1 = sc.parallelizeDoubles(Arrays.asList(45.25,22.15,33.24));
JavaDoubleRDD data2 = sc.parallelizeDoubles(Arrays.asList(23.45,19.35,12.45));

and then

JavaPairRDD<Double,Double> lat1 = data1.cartesian(data1);
JavaRDD<Double> lat2 = lat1.map(new Function<Tuple2<Double,Double>,Double>() {
    @Override
    public Double call(Tuple2<Double,Double> t) {
        return Math.pow(t._1()-t._2(),2);
    }
});
 //flag and flag1 are static variables initially equal to 1
JavaPairRDD<Integer,Double> lat3 = lat2.mapToPair(new PairFunction<Double,Integer,Double>() {
    @Override
     public Tuple2<Integer,Double> call(Double d) {
        return new Tuple2<Integer,Double>(flag++,d);
    }
});
System.out.println("Latitude values display");  
    for(Tuple2<?,?> tuple : lat3.collect()) {
                  System.out.println(tuple._1()+":"+tuple._2());
    } 
JavaPairRDD<Double,Double> long1 = data2.cartesian(data2);
JavaRDD<Double> long2 = long1.map(new Function<Tuple2<Double,Double>,Double>() {
        @Override
        public Double call(Tuple2<Double,Double> t) {
                return Math.pow(t._1()-t._2(),2);
    }
});
    JavaPairRDD<Integer,Double> long3 = long2.mapToPair(new PairFunction<Double,Integer,Double>() {
        @Override
        public Tuple2<Integer,Double> call(Double d ) {
                return new Tuple2<Integer,Double>(flag1++,d);
        }
});
System.out.println("Longitude values display"); 
    for(Tuple2<?,?> tuple : long3.collect()) {
                  System.out.println(tuple._1()+":"+tuple._2());
    }
System.out.println("latitude and longitude values join");
JavaPairRDD<Integer,Tuple2<Double,Double>> weightmatrix1 = lat3.join(long3);
System.out.println("Weightmatrix1 Display");
    for(Tuple2<?,Tuple2<?,?>> tuple : weightmatrix1.collect()) {
                  System.out.println(tuple._1()+":"+tuple._2()._1()+","+tuple._2()._2());
    }   

So what am doing is calculating weight matrix based on latitude and longitude values

2
Not enough info. I suspect you have a problem in the code you are not showing. - Sean Owen
@Sean Owen: I have added my code. I need to construct a matrix look like distances data based on latitude,longitude values. - swagath001
I think the problem is pretty obvious from your use of global static variables. Your problem statement isn't what your code does. - Sean Owen

2 Answers

5
votes

When I do:

scala> val rdd1 = sc.parallelize(Array((1,"a"),(2,"b"),(3,"c")))
scala> val rdd2 = sc.parallelize(Array((1,"d"),(2,"e"),(3,"f")))
scala> val rdd3 = rdd1.join(rdd2)
scala> rdd3.toArray.foreach(println(_))

I consistently get:

(1,(a,d))
(2,(b,e))
(3,(c,f))
0
votes

That's what I tried with expected results:

val data1 = sc.parallelize(Array((1,"a"),(2,"b"),(3,"c")))
val data2 = sc.parallelize(Array((1,"d"),(2,"e"),(3,"f")))
val data3 = data1.join(data2)
data3.collect().map(tuple => tuple._1 + ":"+tuple._2._1+","+tuple._2._2).foreach(println(_))

Obtaining:

1:a,d
2:b,e
3:c,f

So this is scala. I suppose in Java should be the same output.