Shankar
Shankar

Reputation: 8957

Spark SQL DF - How to pass multiple values dynamically for `isin` method of `Column`

I am trying to filter the Spark SQL DataFrame with where condition.

For example: [ working one]

df.where(col("pType").isin("type1","type2"))

What I am trying is, instead of hard coding the pType values, I am trying to build it dynamically. So i build a List(String) with all the pType values.

For example, if I have List("type1","type2"), how can I use this list to isin method?

Upvotes: 1

Views: 2412

Answers (1)

Tzach Zohar
Tzach Zohar

Reputation: 37822

You can use Scala's syntax for converting a collection into a "repeated parameter" (AKA "varargs" in Java-speak, as well as other languages), See section 4.6.2 in the Scala Language Specification:

val list = List("type1","type2")
df.where(col("pType").isin(list: _*))

Upvotes: 3

Related Questions