How to convert Row of a Scala DataFrame into case class most efficiently?

前端 未结 4 882
情书的邮戳
情书的邮戳 2020-12-23 09:49

Once I have got in Spark some Row class, either Dataframe or Catalyst, I want to convert it to a case class in my code. This can be done by matching

someRow          


        
4条回答
  •  一个人的身影
    2020-12-23 09:56

    As far as I know you cannot cast a Row to a case class, but I sometimes chose to access the row fields directly, like

    map(row => myCaseClass(row.getLong(0), row.getString(1), row.getDouble(2))
    

    I find this to be easier, especially if the case class constructor only needs some of the fields from the row.

提交回复
热议问题