PySpark中字符串转Integer返回null问题排查
问题原因及解决办法
- 核心原因:PySpark里的
IntegerType是32位有符号整数,最大值仅为2147483647,你要转换的数字1670900472389远大于这个上限,强制转换时数值溢出,最终返回null。 - 解决办法:
- 直接改用64位整数类型
LongType(),它能容纳更大的数值,修改后的代码如下:df = df.withColumn("lastupdatedtime_new", col("lastupdatedtime").cast(LongType())) - 如果这个字符串是毫秒级时间戳,后续需要转成可读时间格式的话,可以先转成
LongType,再通过from_unixtime函数转换(注意要除以1000,因为该函数默认处理秒级时间戳):from pyspark.sql.functions import from_unixtime, col df = df.withColumn("lastupdatedtime_new", from_unixtime(col("lastupdatedtime").cast(LongType())/1000))
- 直接改用64位整数类型
内容的提问来源于stack exchange,提问作者Jatin
相关产品推荐
相关产品推荐

