How to use Column.isin with list?

后端 未结 5 1570
眼角桃花
眼角桃花 2020-11-29 06:00
val items = List(\"a\", \"b\", \"c\")

sqlContext.sql(\"select c1 from table\")
          .filter($\"c1\".isin(items))
          .collect
          .foreach(println)         


        
相关标签:
5条回答
  • 2020-11-29 06:05

    According to documentation, isin takes a vararg, not a list. List is actually a confusing name here. You can try converting your List to vararg like this:

    val items = List("a", "b", "c")
    
    sqlContext.sql("select c1 from table")
              .filter($"c1".isin(items:_*))
              .collect
              .foreach(println)
    

    Your variant with mkString compiles, because one single String is also a vararg (with number of arguments equal to 1), but it is proably not what you want to achieve.

    0 讨论(0)
  • 2020-11-29 06:05

    Spark has now (since 2.4.0) a method called isInCollection, which is just what you are looking for, instead of isIn.

    (shouldn't they unify the methods?)

    0 讨论(0)
  • 2020-11-29 06:19

    It worked like this in Java Api (Java 8)

    .isin(sampleListName.stream().toArray(String[]::new))));
    

    sampleListName is a List

    0 讨论(0)
  • 2020-11-29 06:23

    As Tomalak has mentioned it :

    isin(java.lang.Object... list)
    A boolean expression that is evaluated to true if the value 
    of this expression is contained by the evaluated values of the arguments.
    

    Therefore, you just could fix this making the following change :

    val items = List("a", "b", "c").map(c => s""""$c"""")
    
    0 讨论(0)
  • 2020-11-29 06:23

    Even easier:

    sqlContext.sql("select c1 from table")
              .filter($"c1".isin("a", "b", "c"))
              .collect
              .foreach(println)
    

    Unless you have a lot of list values, which isn't the case usually.

    0 讨论(0)
提交回复
热议问题