title: groupByKey in Spark dataset, execute custom logic along the aggregation
body: <p>Is it possible to execute custom logic while grouping a Spark dataset? Here example of just printing to console, but I would like to e.g. save grouped datasets (after having implemented additional operations) to separate files. In my example, printing "Hey" to console does not work.</p>
  
  <pre><code>// import spark.implicits._
  
  case class Student(name: String, grade: String)
  
  val students = sc.parallelize(Seq(
    Student("John", "A"),
    Student("John", "B"),
    Student("Amy", "C")
  )).toDF().as[Student]
  
  def someFunc(key:String, values:Iterator[Student]): TraversableOnce[(String,Student)] = {
    println("Hey") // HOW TO GET THIS DONE ?
   return values.map(x =&gt; (key, x))
  }
  
  val groups = students.groupByKey(t =&gt; t.name).flatMapGroups(someFunc).show()
  </code></pre>