I have a variable declare like this :
val jobnameSeq = Seq( ("42409245", "name12"),("42461545", "name4"),("42409291", "name1"),("42413872", "name3"),("42417044", "name2"))
I want to be able to create a function usable in Spark SQL to substitute 42461545 by name4 in an sql query.
I tried to declare this function:
val jobnameDF = jobnameSeq.toDF("jobid","jobname")
sqlContext.udf.register("getJobname", (id: String) => (
jobnameDF.filter($"jobid" === id).select($"jobname")
)
)
To be use like this in sql:
select getjobname(jobid), other, field from table
But jobnameDF.filter($"jobid" === id).select($"jobname") returns a DF not a string and I can't figure out how to simply convert this value to string as there will be only one result each time.
If a Seq is not the object to use in this case, I'm open to suggestion.
Edit:
Though the suggested answer works, here's what I did exactly to make this work:
#Convert my seq to a hash map
val jobMap = jobnameSeq.toMap
#declare a sql function so I could use it in sparksql (I need to be accessible to people that don't know scala
sqlContext.udf.register("getJobname", (id: String) => (
jobMap(id)
)
)