[英]Write/store dataframe in text file
我正在嘗試將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")
}
}
您可以將數據幀轉換為 rdd 並將行轉換為字符串並將最后一行寫為
val op= sourcefile.rdd.map(_.toString()).saveAsTextFile("C:/Users/phadpa01/Desktop/op")
已編輯
正如@philantrovert 和@Pravinkumar 指出的那樣,上述內容會在輸出文件中附加[
和]
,這是真的。 解決方案是replace
它們replace
為empty
字符
val op= sourcefile.rdd.map(_.toString().replace("[","").replace("]", "")).saveAsTextFile("C:/Users/phadpa01/Desktop/op")
甚至可以使用regex
我建議使用csv
或其他分隔格式。 以下是在 Spark 2+ 中以最簡潔/優雅的方式寫入 .tsv 的示例
val tsvWithHeaderOptions: Map[String, String] = Map(
("delimiter", "\t"), // Uses "\t" delimiter instead of default ","
("header", "true")) // Writes a header record with column names
df.coalesce(1) // Writes to a single file
.write
.mode(SaveMode.Overwrite)
.options(tsvWithHeaderOptions)
.csv("output/path")
您可以另存為文本CSV文件 ( .format("csv")
)
結果將是一個 CSV 格式的文本文件,每列將用逗號分隔。
val op = sourcefile.write.mode("overwrite").format("csv").save("C:/Users/phadpa01/Desktop/op")
更多信息可以在火花編程指南中找到
我認為使用“子字符串”更適合我覺得的所有場景。
請檢查以下代碼。
sourcefile.rdd
.map(r => { val x = r.toString; x.substring(1, x.length-1)})
.saveAsTextFile("C:/Users/phadpa01/Desktop/op")
我使用 databricks api 將我的 DF 輸出保存到文本文件中。
myDF.write.format("com.databricks.spark.csv").option("header", "true").save("output.csv")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.