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

如何在Scala中将Hive的Int类型数据转为无科学计数法的String类型

解决Spark中Int转String出现科学计数法的问题

直接调用cast("String")转换较大Int类型数值时,Spark可能会输出科学计数法格式的字符串,你可以通过以下两种方法解决:

  • 方法一:先转为Long类型再转String
    利用Long类型转换为字符串时不会触发科学计数法的特性,先将Int转成Long再转String:
import org.apache.spark.sql.types.LongType
import org.apache.spark.sql.functions.col

val convertedDf = df.withColumn("birth_date", col("birth_date").cast(LongType).cast(StringType))
  • 方法二:使用format_string函数指定格式
    通过格式化函数强制以十进制整数格式输出字符串:
import org.apache.spark.sql.functions.format_string

val convertedDf = df.withColumn("birth_date", format_string("%d", col("birth_date")))

两种方法都能确保birth_date字段输出为20090502、20170509这类无科学计数法的字符串,之后再写入Parquet文件即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:40:02