Pyspark-在嵌套集合中找到最古老的日期
我有以下数据框
root
|-- AUTHOR_ID: integer (nullable = false)
|-- Books: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- NAME: string (nullable = true)
| | |-- DATE: TimestampType (nullable = true)
如何查找每个作者最早出版的书籍?我想检索日期
{
"AUTHOR_ID": 1,
"FIRST_PUBLICATION": <Date>
"Books": "[ ... ]"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
多种做法,让我们尝试一下窗口函数
跟随您编辑
Many ways of doing, Lets Try window functions
Following you Edits
对于
Spark v3
,使用Spark 高阶函数
是最佳解决方案,df.show(truncate=False)
df.printSchema( )
我们可以得到每个作者日期最少的书,如下
所示
For
Spark v3
usingSpark Higher-order functions
is the best solution,df.show(truncate=False)
df.printSchema()
We can get the book with the least date for each author as the following
Result
从 Spark 2.4 开始,您可以使用
array_min
函数检索数组的最小元素。您可以将此函数应用于仅包含日期的数组。要构建仅包含日期的数组,可以使用 Books 列上的 ="nofollow noreferrer">getField
方法。这是完整的代码:
Since Spark 2.4, you can use the
array_min
function to retrieve the minimum element of an array. You apply this function to an array that contains only the dates. To build the array that contains only dates, you can usegetField
method onBooks
column.Here is the complete code: