我正在嘗試將dataframe
寫入text
文件。如果一個文件包含單列,那麼我可以寫入文本文件。如果文件包含多列,那麼我面臨一些錯誤在文本文件中寫入/存儲數據幀
文本數據源僅支持單列,並且您有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")
}
}
我認爲這會在每行的兩端添加'['和']''。 – philantrovert
,但它爲每條記錄添加了「[]」每條記錄.eg:[2,12.2,12.2] –
是的,它可以替換爲空。讓我更新答案 –