Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use a variable as spark selected fields

I’m fresh with scala, there’s a dataframe with lots of columns, I would like to select some fields but have to list them all every time as below, how can I define a variable stands for them and pass in scala?

df.select("a", "b", "c", "d", "e", "f") 

expected:

df.select(variable val) 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can pass a list of columns, something like this:

import org.apache.spark.sql.functions.col

val fields = List("a", "b", "c", "d").map(col)
df.select(fields: _*)

map(col) transforms your list of strings into columns.
fields: _* transforms your List into multiple arguments

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading