[英]Python function such as max() doesn't work in pyspark application
Python 函数 max(3,6) 在 pyspark shell 下工作。 但是如果把它放在应用程序中并提交,它会抛出一个错误:TypeError: _() 需要 1 个参数(给出 2 个)
看起来您的应用程序中存在导入冲突,很可能是由于从pyspark.sql.functions
导入通配符:
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 1.6.1
/_/
Using Python version 2.7.10 (default, Oct 19 2015 18:04:42)
SparkContext available as sc, HiveContext available as sqlContext.
In [1]: max(1, 2)
Out[1]: 2
In [2]: from pyspark.sql.functions import max
In [3]: max(1, 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-bb133f5d83e9> in <module>()
----> 1 max(1, 2)
TypeError: _() takes exactly 1 argument (2 given)
除非你的工作相对有限,否则最好是perfix:
from pyspark.sql import functions as sqlf
max(1, 2)
## 2
sqlf.max("foo")
## Column<max(foo)>
或别名:
from pyspark.sql.functions import max as max_
max(1, 2)
## 2
max_("foo")
## Column<max(foo)>
如果您在确认您没有使用from pyspark.sql.functions import *
后仍然收到此错误,请尝试以下操作:
使用import builtins as py_builtin
,然后相应地使用相同的前缀调用它。 例如: py_builtin.max()
*添加 David Arenburg 和 user3610141 的评论作为答案,因为这有助于我解决 databricks 中的问题,其中与 python 内置的 pyspark 的 min() 和 max() 发生名称冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.