我正在尝试使用以下结构筛选Dataframe中的行:
|-- age: integer (nullable = true)
|-- qty: integer (nullable = true)
|-- dates: array (nullable = true)
| |-- element: timestamp (containsNull = true)
例如,在这个Dataframe中,我只想要第一行:
+---------+------------+------------------------------------------------------------------+
| age | qty |dates |
+---------+------------+------------------------------------------------------------------+
| 54 | 1| [2020-12-31 12:15:20, 2021-12-31 12:15:20] |
| 45 | 1| [2020-12-31 12:15:20, 2018-12-31 12:15:20, 2019-12-31 12:15:20] |
+---------+------------+------------------------------------------------------------------+
这是我的密码:
val result = sqlContext
.table("scores")
result.filter(array_contains(col("dates").cast("string"),
2021)).show(false)
但我得到了一个错误:
org.apache.spark.sql.analysisexception:无法解析“array\u contains”(由于数据类型不匹配:参数必须是数组,后跟与>数组成员类型相同的值;
有人能帮忙吗?
3条答案
按热度按时间5jdjgkvh1#
你可以用
exists
函数来检查dates
数组包含2021年的日期:如果你想用
array_contains
,您需要将时间戳元素转换为年份:pxyaymoc2#
你需要使用
rlike
检查每个数组元素是否包含2021。array_contains
检查是否完全匹配,而不是部分匹配。pxiryf3j3#
您可以分解arraytype,然后根据需要进行处理:将列强制转换为字符串,然后应用筛选器: