0

I have a log of the steps of my application, and would like to print the result of "df.show(5)" to a file as a sample of the data that is received by the process.

I found this solution, which allows me to store the result of "show" into a String. The String prints nicelly into the console, but when I try to write it to a file all I get is a blank Line.

This is where I am directing that String to write the file, but no errors are given, and the resulting line on file is blank:

def writeLineOnFile(input: String, path: String): Unit = {
    val file = new File(path)
    val fw = new FileWriter(file, true)
    try {
      fw.write( input)
    }
    finally fw.close()
  }

Piece of code used to try to transform the output from df.show into a printable string, but comes out empty in the file:

def getOutAsString(input: Unit): String ={
    val oldPs = Console.out
    val baos = new ByteArrayOutputStream()
    val ps =  new java.io.PrintStream(baos);
    Console.withOut(ps) {
      input
    }
    new String(baos.toByteArray)
  }
Welsige
  • 149
  • 1
  • 10

1 Answers1

0

maybe try this:

  /**
     * write a `String` to the `path`.
     */
    def writeLineOnFile(input: String, path: String): Unit = {
        val file = new File(path)
        val bw = new BufferedWriter(new FileWriter(file))
        bw.write(input)
        bw.close()
    }
amer
  • 1,528
  • 1
  • 14
  • 22