pyspark 中 UDF 的返回类型无效

use*_*848 3 apache-spark apache-spark-sql pyspark

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

类型错误:返回类型无效:返回类型应为 DataType 或 str,但为 <'pyspark.sql.types.IntegerType'>

我的代码其实很简单:

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)
Run Code Online (Sandbox Code Playgroud)

问题可能出在 IntegerType 上,但我不知道出了什么问题。我正在使用Python version 3.5.3spark version 2.4.1

Shu*_*ain 5

由于您直接使用IntegerType而不调用它会导致问题

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

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