You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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))
      

内容的提问来源于stack exchange,提问作者Jatin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 21:10:28