Saving contents of df.show() as a string in spark-scala app

后端 未结 2 1476
猫巷女王i
猫巷女王i 2021-01-04 21:21

I need to save the output of df.show() as a string so that i can email it directly.

For ex., the below example taken from official spark docs,:

val d         


        
相关标签:
2条回答
  • 2021-01-04 21:55

    Workaround is to redirect standard output to variable:

    val baos = new java.io.ByteArrayOutputStream();
    val ps =  new java.io.PrintStream(baos);
    
    val oldPs = Console.out
    Console.setOut(ps)
    df.show()
    val content = baos.toString()
    Console.setOut(oldPs)
    

    Note that I have one deprecation warning here.

    You can also re-implement method Dataset.showString, which generated data. It uses take in background. Maybe it's also a good moment to create PR to make showString public? :)

    0 讨论(0)
  • 2021-01-04 22:04

    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)
    
    0 讨论(0)
提交回复
热议问题