简体   繁体   中英

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:

dataframe.select($"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM