DecimalType(20, 0) 在 spark 中不包含 7 位整数

DecimalType(20, 0) does not hold 7 digit integer in spark

我尝试在 spark 中将 int 替换为 Decimal,但出现以下错误。

spark.createDataFrame([{'abc': 7010930}], StructType([StructField('abc', DecimalType(20, 0), True)]))

field abc: DecimalType(20,0) can not accept object 7010930 in type <class 'int'>

我理解如果精度是20应该可以得到7位数,但是我哪里错了?

您可以使用 python class decimal.Decimal:

传递小数点而不是 int 对象
from decimal import Decimal

df = spark.createDataFrame(
    [(Decimal(7010930),)],
    StructType([StructField('abc', DecimalType(20, 0), True)])
)

df.printSchema()

#root
# |-- abc: decimal(20,0) (nullable = true))