Apache spark pyspark中UDF的返回类型无效

Apache spark pyspark中UDF的返回类型无效,apache-spark,pyspark,apache-spark-sql,Apache Spark,Pyspark,Apache Spark Sql,我在pyspark中面临一个奇怪的问题,我想定义和使用UDF。我总是遇到这样的错误: TypeError:返回类型无效:返回类型应为DataType或str,但为 我的代码实际上非常简单: from pyspark.sql import SparkSession from pyspark.sql.types import IntegerType def square(x): return 2 def _process(): spark = SparkSession.build

我在pyspark中面临一个奇怪的问题,我想定义和使用UDF。我总是遇到这样的错误:

TypeError:返回类型无效:返回类型应为DataType或str,但为

我的代码实际上非常简单:

from pyspark.sql import SparkSession
from pyspark.sql.types import IntegerType

def square(x):
    return 2

def _process():
    spark = SparkSession.builder.master("local").appName('process').getOrCreate()
    spark_udf = udf(square,IntegerType)
问题可能在于IntegerType,但我不知道这有什么问题。我使用的是Python版本3.5.3和spark版本2.4.1,因为您直接使用IntegerType而不调用它会导致问题

def _process():
    spark = SparkSession.builder.master("local").appName('process').getOrCreate()
    spark_udf = udf(square,IntegerType())
尝试调用IntegerType类型,它应该可以正常工作。

因为您直接使用IntegerType而不调用它会导致问题

def _process():
    spark = SparkSession.builder.master("local").appName('process').getOrCreate()
    spark_udf = udf(square,IntegerType())

尝试调用IntegerType类型,它应该可以正常工作。

IntegerType后面缺少括号。正确的声明应该是spark\u udf=udfsquare,IntegerType如果IntegerType后面缺少括号。正确的声明应该是spark\u udf=udfsquare,IntegerType