对于基于相等的查询,可以使用
array_contains:
df = sc.parallelize([(1, [1, 2, 3]), (2, [4, 5, 6])]).toDF(["k", "v"])df.createOrReplaceTempView("df")# With SQLsqlContext.sql("SELECT * FROM df WHERe array_contains(v, 1)")# With DSLfrom pyspark.sql.functions import array_containsdf.where(array_contains("v", 1))如果要使用更复杂的谓词,则必须使用
explode或使用UDF,例如,如下所示:
from pyspark.sql.types import BooleanTypefrom pyspark.sql.functions import udfdef exists(f): return udf(lambda xs: any(f(x) for x in xs), BooleanType())df.where(exists(lambda x: x > 3)("v"))在Spark 2.4中。或更高版本,也可以使用高阶函数
from pyspark.sql.functions import exprdf.where(expr("""aggregate( transform(v, x -> x > 3), false, (x, y) -> x or y)"""))要么
df.where(expr(""" exists(v, x -> x > 3)"""))Python包装器应该在3.1(SPARK-30681)中可用。



