techie
techie

Reputation: 363

Dynamically fetch value from hashmap during select spark scala

I have a map defined with key value pair. While selecting column I for a dataframe I need to dynamically fetch column name as value from Map based on key provided like,

val map = HashMap("emp_id" -> "dept_id","emp_city" ->  "dept_city" );
val df1 = df.select($"$map.get($name)")

Here name will be key in map and I need to get related value in expression above but it throws error. What could be problem here

Upvotes: 0

Views: 957

Answers (2)

techie
techie

Reputation: 363

As suggested by Tzach, the problem was with string interpolation- changing the expression to $"df.${map.get(name).mkString}" fixed the issue

Upvotes: 0

user9587699
user9587699

Reputation: 11

Just use apply:

f.select(map(name))

Upvotes: 1

Related Questions