user3180835
user3180835

Reputation: 111

Why does my println in rdd prints the string of elements?

When I try to print the contents of my RDD, it prints something like displayed below, how can I print the content? Thanks!

scala> lines
res15: org.apache.spark.rdd.RDD[Array[String]] = MapPartitionsRDD[3] at filter at <console>:23



scala> lines.take(5).foreach(println)

[Ljava.lang.String;@6d3db5d1
[Ljava.lang.String;@6e6be45e
[Ljava.lang.String;@6d5e0ff4
[Ljava.lang.String;@3a699444
[Ljava.lang.String;@69851a51

Upvotes: 3

Views: 7663

Answers (2)

Greg
Greg

Reputation: 589

Depending on how you want to print them out you can change your line that prints the elements to:

scala> lines.take(5).foreach(indvArray => indvArray.foreach(println))

Upvotes: 4

Justin Pihony
Justin Pihony

Reputation: 67075

This is because it uses the toString implementation for the given object. In this case Array prints out the type and hash. If you convert it to a List then it will be a prettier output due to List's toString implementation

scala>println(Array("foo"))
[Ljava.lang.String;HASH    

scala>println(Array("foo").toList)
List(foo)

Upvotes: 6

Related Questions