【问题标题】:Spark filter DataFrame by comparing listSpark通过比较列表过滤DataFrame
【发布时间】:2019-01-12 21:30:49
【问题描述】:

我在 Spark 上使用 Python。我想过滤指定字段等于整个列表的行。

df.show()
+--------------------+---------------+
|                 _id|             a1|
+--------------------+---------------+
|[596d799cbc6ec95d...|[1.0, 2.0, 3.0]|
|[596d79a2bc6ec95d...|     [1.0, 2.0]|
+--------------------+---------------+

我希望结果是

+--------------------+---------------+
|                 _id|             a1|
+--------------------+---------------+
|[596d79a2bc6ec95d...|     [1.0, 2.0]|
+--------------------+---------------+

我用这个

df.filter(df.a1 == [1., 2.])

但它失败了。

追溯

  File "/Users/gzc/Documents/spark-2.1.1-bin-hadoop2.7/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o129.equalTo.
: java.lang.RuntimeException: Unsupported literal type class java.util.ArrayList [1, 2]
    at org.apache.spark.sql.catalyst.expressions.Literal$.apply(literals.scala:75)
    at org.apache.spark.sql.functions$.lit(functions.scala:101)
    at org.apache.spark.sql.Column.$eq$eq$eq(Column.scala:267)
    at org.apache.spark.sql.Column.equalTo(Column.scala:290)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:280)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:748)

【问题讨论】:

  • 你的过滤条件是什么?你想过滤掉 a1 = [1.0, 2.0] 的行吗?
  • @ShankarKoirala 是的,我想要a1 == [1.0, 2.0]的行

标签: python apache-spark apache-spark-sql


【解决方案1】:

您应该使用文字 array,如下所示:

from pyspark.sql.functions import array, lit

df = sc.parallelize([(1, [1., 2.]), (2, [1., 2., 3.])]).toDF(["id", "a1"])
df.where(df.a1 == array(*(lit(x) for x in [1., 2.]))).show()
+---+----------+
| id|        a1|
+---+----------+
|  1|[1.0, 2.0]|
+---+----------+

【讨论】:

  • 这就是我想要的。虽然它比我预期的要复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 2015-12-22
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 2018-11-15
  • 2019-05-25
相关资源
最近更新 更多