简体   繁体   中英

Add header to RDD[string] spark scala

hi guys i have an RDD[string] , that i want to add to it a header before saving it to a text file , here the code i did

val projectionsTxt = rowMatrix.rows.map(l => l.toString.filter(c => c != '[' & c != ']'))
      val res = projectionsTxt.map(p => {
        var tokens = p.split(",")

        for (i <- 0 to tokens.length - 1) {

          tokens(i) = (BigDecimal(tokens(i)).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble).toString()

        }
        tokens.mkString(",")

      })
      val header: RDD[String] = sc.parallelize(Array("col1","col2","col3"))

   header.union(res).saveAsTextFile(strFilePath)

any help please i've not been able to get it done . i would like to get data and their header .Thanks

try : val header: RDD[String] = sc.parallelize(Array("col1,col2,col3"))
The column List must match with a String ( = a line ). You have a RDD[String] . so you must declare an array of one 'String' element.

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