[英]How to write a DataFrame schema to file in Scala
我有一個DataFrame從一個巨大的json文件加載並從中獲取模式。 架構基本上大約1000列。 我希望將printSchema的相同輸出保存在文件而不是控制台中。
有任何想法嗎?
如果您在本地環境中工作,則可以執行以下操作:
val filePath = "/path/to/file/schema_file"
new PrintWriter(filePath) { write(df.schema.treeString); close }
如果您使用的是HDFS,則需要提供URI。
這是printSchema()的主體:
/**
* Prints the schema to the console in a nice tree format.
* @group basic
* @since 1.3.0
*/
// scalastyle:off println
def printSchema(): Unit = println(schema.treeString)
// scalastyle:on println
所以你不能做太多,但我有一個可以在你的情況下工作的工作。 將輸出流設置為文件Stream,以便將其打印到您的文件中。
像這樣的東西
val out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
我希望我解決了你的疑問!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.