user3551523 user3551523 - 4 months ago 25
Scala Question

How to make a dataframe into a case class?

Lots of documentations show that it's possible to go from a case class to a dataframe, but I haven't been able to find a good way of going from a dataframe to a case class.

Let's say I have a dataframe with 50 columns, but would like to select out about 5 columns and make it into a new table. I could approach it this way:

sqlContext.sql("select [1, 2, 3, 4, 5] from test").registerTempTable("newTable")

But the newTable will have some other columns like 6, 7 as a customized value (or 0 for now, but this column just doesn't exist in the test table).
To solve this, I tried to create a case class that looks like this:

case class newTable(1, 2, 3, 4, 5, 6, 7)

In the end, I would want to extract column 1 through 5 from the test table, then input 6, 7 whatever I would like to. I just haven't found a good way of doing this.


you can use like this:$"1".as("1"), $"2".as("2"), $"3".as("3"), $"4".as("4"), $"5".as("5")).as[newTable]

Note : you should match the column name as the field name in your case class