HackerDuck HackerDuck - 3 months ago 17
Scala Question

Cannot print the contents of RDD

I try to print the contents of my RDD

RDD[(String,List[(String,String)])]
:

val sc = new SparkContext(conf)
val splitted = rdd.map(line => line.split(","))
val processed = splitted.map(x=>(x(1),List((x(0),x(2),x(3),x(4)))))
val grouped = processed.reduceByKey((x,y) => (x ++ y))
System.out.println(grouped)


However instead of getting the contents I see:

ShuffledRDD[4] at reduceByKey at Consumer.scala:88


UPDATE:

The content of TXT file:

100001082016,230,111,1,1
100001082016,121,111,1,1
100001082016,110,111,1,1


UPDATE 2 (the whole code):

class Consumer()
{

def run() = {
val conf = new SparkConf()
.setAppName("TEST")
.setMaster("local[*]")
val sc = new SparkContext(conf)
val rdd = sc.textFile("file:///usr/test/myfile.txt")
val splitted = rdd.map(line => line.split(","))
val processed = splitted.map(x=>(x(1),List((x(0),x(2),x(3),x(4)))))
val grouped = processed.reduceByKey((x,y) => (x ++ y))
System.out.println(grouped)
}

}

Answer

There is no issue here :

scala> val rdd = sc.parallelize(Seq("100001082016,230,111,1,1","100001082016,121,111,1,1","100001082016,110,111,1,1"))
// rdd: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[0] at parallelize at <console>:27

scala> val splitted = rdd.map(line => line.split(","))
// splitted: org.apache.spark.rdd.RDD[Array[String]] = MapPartitionsRDD[1] at map at <console>:29

scala> val processed = splitted.map(x=>(x(1),List((x(0),x(2),x(3),x(4)))))
// processed: org.apache.spark.rdd.RDD[(String, List[(String, String, String, String)])] = MapPartitionsRDD[2] at map at <console>:31

scala> val grouped = processed.reduceByKey((x,y) => (x ++ y))
// grouped: org.apache.spark.rdd.RDD[(String, List[(String, String, String, String)])] = ShuffledRDD[3] at reduceByKey at <console>:33

scala> grouped.collect().foreach(println)
// (121,List((100001082016,111,1,1)))
// (110,List((100001082016,111,1,1)))
// (230,List((100001082016,111,1,1)))

The following is wrong. It works as expect but you have to understand the language correctly to know what is expect :

scala> System.out.println(grouped)
// ShuffledRDD[3] at reduceByKey at <console>:33

EDIT: Just to be clear, if you wish to print a collection, you'll need to use the mkString method available for the collection that you'd need to print converting it into the format you'd want.

Comments