简体   繁体   English

将标头添加到RDD [string] spark scala

[英]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 嗨伙计们我有一个RDD [string],我想在将它保存到文本文件之前添加一个标题,这里是我做的代码

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")) try: val header: RDD[String] = sc.parallelize(Array("col1,col2,col3"))
The column List must match with a String ( = a line ). 列List必须与String(=一行)匹配。 You have a RDD[String] . 你有一个RDD [String]。 so you must declare an array of one 'String' element. 所以你必须声明一个'String'元素的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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