我正在尝试将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] –
是的,它可以替换为空。让我更新答案 –