简体   繁体   中英

How to convert case class RDD to RDD[String]?

I am having one schema rdd. If I print that RDD, I will get the output like caseclass_name(col a, col b,col c) caseclass_name(col d,col e, col f) ..... ..... I need to display simply as (without case class name in front) col a, col b, col c col d, col e, col f

How can I get this? Please assist

val tenColumns1 = afterSplit1.filter(x => x.length == 4)
case class iclass(Id1:Int,Id2:Int,SaleDate:String,Code:String)

val insureRDD1 = tenColumns1.map( i => iclass(i(0).toInt,i(1).toInt,i(2),i(3))) insureRDD1.take(2).foreach(println)

outdput:

iclass(32,35,2013-10-05,AK)

iclass(36,38,2014-12-25,AK)

I need the output as:

32,35,2013-10-05,AK

36,38,2014-12-25,AK

Simplest solution is to override the toString method in your case class

case class iclass(Id1:Int,Id2:Int,SaleDate:String,Code:String) {
  override def toString(): String = {
    s"$Id1,$Id2,$SaleDate,$Code"
  }
}

If you have an RDD[iclass] and want to convert it to an RDD[String] , you can then just map it like insureRDD1.map(_.toString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM