我在 pyspark 中遇到一个奇怪的问题,我想在其中定义和使用 UDF。我总是收到这个错误:
TypeError: Invalid returnType: returnType should be DataType or str but is <'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)
问题可能出在 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()
类型,它应该可以正常工作。
关于apache-spark - UDF 的 pyspark 中的返回类型无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62379312/