将df.show()的内容另存为Spark-scala应用程序中的字符串 [英] Saving contents of df.show() as a string in spark-scala app
本文介绍了将df.show()的内容另存为Spark-scala应用程序中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将df.show()的输出保存为字符串,以便我可以直接通过电子邮件发送.
I need to save the output of df.show() as a string so that i can email it directly.
例如,以下示例摘自官方spark文档,
For ex., the below example taken from official spark docs,:
val df = spark.read.json("examples/src/main/resources/people.json")
// Displays the content of the DataFrame to stdout
df.show()
// +----+-------+
// | age| name|
// +----+-------+
// |null|Michael|
// | 30| Andy|
// | 19| Justin|
// +----+-------+
我需要将上表另存为在控制台中打印的字符串.我确实看过log4j来打印日志,但是只记录输出结果却找不到任何信息.
I need to save the above table as a string which is printed in the console. I did look at log4j to print the log, but couldnt come across any info on logging only the output.
有人可以帮我吗?
推荐答案
scala.Console
对于此类问题具有withOut
方法:
scala.Console
has a withOut
method for this kind of thing:
val outCapture = new ByteArrayOutputStream
Console.withOut(outCapture) {
df.show()
}
val result = new String(outCapture.toByteArray)
这篇关于将df.show()的内容另存为Spark-scala应用程序中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文