通过比较列表 Spark 过滤 DataFrame [英] Spark filter DataFrame by comparing list

查看:43
本文介绍了通过比较列表 Spark 过滤 DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Python on Spark. I want to filter rows where a specified field equals a whole list.

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)

推荐答案

你应该使用文字 array 如下:

You should use a literal array as follows:

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]|
+---+----------+

这篇关于通过比较列表 Spark 过滤 DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆