title: Spark Structured Streaming for appending to text file using foreach
body: <p>I want to append lines to a text file using structured streaming. This code results in <code>SparkException: Task not serializable</code>. I think <code>toDF</code> is not allowed. How could I get this code to work?</p>
  
  <pre><code>df.writeStream
    .foreach(new ForeachWriter[Row] {
      override def open(partitionId: Long, version: Long): Boolean = {
        true 
      }
  
      override def process(row: Row): Unit = {
         val df = Seq(row.getString(0)).toDF
  
         df.write.format("text").mode("append").save(output)
      } 
  
      override def close(errorOrNull: Throwable): Unit = {
      }      
    }).start
  </code></pre>