诸如max()之类的Python函数在pyspark应用程序中不起作用

use*_*141 2 python pyspark

Python函数max(3,6)在pyspark shell下工作。但是,如果将其放入应用程序中并提交,则会引发错误:TypeError:_()恰好接受1个参数(给定2个)

Dea*_*ock 6

如果即使在确认您没有使用过之后仍收到此错误from pyspark.sql.functions import *,请尝试以下操作:

使用import builtins as py_builtin 然后用相同的前缀对应地调用它。例如:py_builtin.max()

*添加 David Arenburg 和 user3610141 的评论作为答案,因为这可以帮助我解决 databricks 中的问题,其中与 python 内置的 pyspark 的 min() 和 max() 发生名称冲突。


zer*_*323 5

看起来您的应用程序中存在导入冲突,最有可能是由于来自的通配符导入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)
Run Code Online (Sandbox Code Playgroud)

除非您的工作相对有限,否则最好采用以下两种做法:

from pyspark.sql import functions as sqlf

max(1, 2)
## 2

sqlf.max("foo")
## Column<max(foo)>
Run Code Online (Sandbox Code Playgroud)

或别名:

from pyspark.sql.functions import max as max_

max(1, 2)
## 2

max_("foo")
## Column<max(foo)>
Run Code Online (Sandbox Code Playgroud)

  • 我通过“导入 __builtin__”然后“打印 __builtin__.max(1,2)”解决了问题。非常感谢! (2认同)
  • @user3610141你的意思是导入内置**s**。我认为值得回答。 (2认同)