Spark 有一个函数array_contains,可用于检查ArrayType 列的内容,但不幸的是,它似乎不能处理复杂类型的数组。但是,可以使用 UDF(用户定义函数)来做到这一点:
from pyspark.sql.types import *
from pyspark.sql import Row
import pyspark.sql.functions as F
schema = StructType([StructField("extra_features", ArrayType(StructType([
StructField("key", StringType(), False),
StructField("value", StringType(), True)])),
False)])
df = spark.createDataFrame([
Row([{'key': 'a', 'value': '1'}]),
Row([{'key': 'b', 'value': '2'}])], schema)
# UDF to check whether {'key': 'a', 'value': '1'} is in an array
# The actual data of a (nested) StructType value is a Row
contains_keyval = F.udf(lambda extra_features: Row(key='a', value='1') in extra_features, BooleanType())
df.where(contains_keyval(df.extra_features)).collect()
这会导致:
[Row(extra_features=[Row(key=u'a', value=u'1')])]
您还可以使用 UDF 添加另一列来指示键值对是否存在:
df.withColumn('contains_it', contains_keyval(df.extra_features)).collect()
结果:
[Row(extra_features=[Row(key=u'a', value=u'1')], contains_it=True),
Row(extra_features=[Row(key=u'b', value=u'2')], contains_it=False)]