amazon-web-services - 在pyspark中查找列表的最大值/最小值

标签 amazon-web-services pyspark apache-spark-sql

我知道这是一个非常微不足道的问题,我很惊讶我在互联网上找不到答案,但是可以在 pyspark 中找到列表的最大值或最小值吗? 在 Python 中,它很容易通过

max(list)

但是,当我在 pyspark 中尝试相同操作时,出现以下错误:

An error was encountered:
An error occurred while calling z:org.apache.spark.sql.functions.max. Trace:
py4j.Py4JException: Method max([class java.util.ArrayList]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339)
    at py4j.Gateway.invoke(Gateway.java:276)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

关于我做错了什么有什么想法吗?

更新:添加我所做的: 这是我的 list :

cur_datelist

输出:

['2020-06-10', '2020-06-11', '2020-06-12', '2020-06-13', '2020-06-14', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20', '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08', '2020-08-09', '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-13', '2020-08-14', '2020-08-15', '2020-08-16', '2020-08-17', '2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21', '2020-08-22', '2020-08-23', '2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27', '2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31']

该类是“列表”: 类型(cur_datelist)

<class 'list'>

我假设这是一个普通的 pythonic 列表。 所以当我尝试 max(cur_datelist) 时,我得到了上面提到的错误。

最佳答案

对于列表,pyspark 和 python 没有区别,但列不同。这是我的 pyspark 的结果。

# just a list
l = [1, 2, 3]
print(max(l))

# 3

# dataframe with the array column
df = spark.createDataFrame([(1, [1, 2, 3]), (2, [4, 5, 6])]).toDF('id', 'list')

import pyspark.sql.functions as f

df.withColumn('max', f.array_max(f.col('list'))).show()

#+---+---------+---+
#| id|     list|max|
#+---+---------+---+
#|  1|[1, 2, 3]|  3|
#|  2|[4, 5, 6]|  6|
#+---+---------+---+

您的错误来自 python 原生函数和 spark 列函数之间的 max 函数重叠!为避免这种情况,请指定您的 pyspark 函数。然后max表示python原件。

import pyspark.sql.functions as f

l = ['2020-06-10', '2020-06-11', '2020-06-12', '2020-06-13', '2020-06-14', '2020-06-15', '2020-06-16', '2020-06-17', '2020-06-18', '2020-06-19', '2020-06-20', '2020-06-21', '2020-06-22', '2020-06-23', '2020-06-24', '2020-06-25', '2020-06-26', '2020-06-27', '2020-06-28', '2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02', '2020-07-03', '2020-07-04', '2020-07-05', '2020-07-06', '2020-07-07', '2020-07-08', '2020-07-09', '2020-07-10', '2020-07-11', '2020-07-12', '2020-07-13', '2020-07-14', '2020-07-15', '2020-07-16', '2020-07-17', '2020-07-18', '2020-07-19', '2020-07-20', '2020-07-21', '2020-07-22', '2020-07-23', '2020-07-24', '2020-07-25', '2020-07-26', '2020-07-27', '2020-07-28', '2020-07-29', '2020-07-30', '2020-07-31', '2020-08-01', '2020-08-02', '2020-08-03', '2020-08-04', '2020-08-05', '2020-08-06', '2020-08-07', '2020-08-08', '2020-08-09', '2020-08-10', '2020-08-11', '2020-08-12', '2020-08-13', '2020-08-14', '2020-08-15', '2020-08-16', '2020-08-17', '2020-08-18', '2020-08-19', '2020-08-20', '2020-08-21', '2020-08-22', '2020-08-23', '2020-08-24', '2020-08-25', '2020-08-26', '2020-08-27', '2020-08-28', '2020-08-29', '2020-08-30', '2020-08-31']
print(max(l))

# 2020-08-31

或者,

import builtins as p

print(p.max(l))
# 2020-08-31

关于amazon-web-services - 在pyspark中查找列表的最大值/最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63650179/

相关文章:

post - 增加 Amazon API Gateway 的最大 POST 大小

python - 在 Spark 中更新数据框列

scala - Scala Spark 中的编码器[行]

apache-spark - 如何在HiveContext中设置hive.metastore.warehouse.dir?

scala - 使用 SparkSession 创建广播变量? Spark 2.0

python - 使用 Python 连接到 AWS Elasticache Redis 集群

java - 如何在 aws lambda 中关闭 aws 客户端

amazon-web-services - 即使 AWS CloudFormation 中的条件为 false,获取命令也不能为 null

apache-spark - Google Dataproc Pyspark - BigQuery 连接器 super 慢

apache-spark - 黑斑羚与 SparkSQL : built-in function translation: fnv_hash