我正在尝试根据值列表从数据框进行过滤,并且能够按照示例1中的方式运行它。但是,当我将元素转换为list然后将列表传递给'isin'函数时在过滤器函数内部不起作用(如示例2所示)。
val df1 = sc.parallelize(Seq((1,"abcd"), (2,"defg"), (3, "ghij"),(4,"xyzz"),(5,"lmnop"),(6,"pqrst"),(7,"wxyz"),(8,"lmnoa"),(9,"jklm"))).toDF("c1","c2")
//example 1:
val df2 = df1.filter(substring(col("c2"), 0, 3).isin("abc","def","ghi"))
//example 2:
val given_list = List("abc","def","ghi")
val df3 = df1.filter(substring(col("c2"), 0, 3).isin(given_list))
运行示例2时的错误消息如下:
Exception in thread "main" java.lang.RuntimeException: Unsupported literal type class scala.collection.immutable.$colon$colon List(abc, def, ghi)
19/10/22 17:03:10 INFO spark.SparkContext: Invoking stop() from shutdown hook
19/10/22 17:03:10 INFO server.AbstractConnector: Stopped Spark@5817c15f{HTTP/1.1,[http/1.1]}{0.0.0.0:4040}
19/10/22 17:03:10 INFO ui.SparkUI: Stopped Spark web UI at http://192.---.---.---:----
19/10/22 17:03:10 INFO spark.MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
19/10/22 17:03:10 INFO memory.MemoryStore: MemoryStore cleared
19/10/22 17:03:10 INFO storage.BlockManager: BlockManager stopped
19/10/22 17:03:10 INFO storage.BlockManagerMaster: BlockManagerMaster stopped
19/10/22 17:03:10 INFO scheduler.OutputCommitCoordinator$OutputCommitCoordinatorEndpoint: OutputCommitCoordinator stopped!
19/10/22 17:03:10 INFO spark.SparkContext: Successfully stopped SparkContext
19/10/22 17:03:10 INFO util.ShutdownHookManager: Shutdown hook called
19/10/22 17:03:10 INFO util.ShutdownHookManager: Deleting directory /tmp/spark-d17c100d-3e95-4016-a2fd-4e1e02b2449f
谢谢。
答案 0 :(得分:2)
方法isin
采用一个Any*
varargs参数,而不是像List
这样的集合。您可以使用“ splat”运算符(即_*
),如下所示:
df1.filter(substring(col("c2"), 0, 3).isin(given_list: _*))
Spark 2.4
+确实提供了方法isInCollection
,该方法带有一个Iterable
集合,该集合可以按以下方式使用:
df1.filter(substring(col("c2"), 0, 3).isInCollection(given_list))