小编Pra*_*pad的帖子

在文本文件中写入/存储数据帧

我试图写dataframetext文件。如果文件包含单列,那么我可以在文本文件中写入。如果文件包含多列,那么我将面临一些错误

文本数据源仅支持单列,您有 2 列。

object replace {

  def main(args:Array[String]): Unit = {

    Logger.getLogger("org").setLevel(Level.ERROR)

    val spark = SparkSession.builder.master("local[1]").appName("Decimal Field Validation").getOrCreate()

    var sourcefile = spark.read.option("header","true").text("C:/Users/phadpa01/Desktop/inputfiles/decimalvalues.txt")

     val rowRDD = sourcefile.rdd.zipWithIndex().map(indexedRow => Row.fromSeq((indexedRow._2.toLong+1) +: indexedRow._1.toSeq)) //adding prgrefnbr               
                         //add column for prgrefnbr in schema
     val newstructure = StructType(Array(StructField("PRGREFNBR",LongType)).++(sourcefile.schema.fields))

     //create new dataframe containing prgrefnbr

     sourcefile = spark.createDataFrame(rowRDD, newstructure)
     val op= sourcefile.write.mode("overwrite").format("text").save("C:/Users/phadpa01/Desktop/op")

  }

}
Run Code Online (Sandbox Code Playgroud)

scala apache-spark

5
推荐指数
2
解决办法
6万
查看次数

标签 统计

apache-spark ×1

scala ×1