将字符串转换为Bigint DataFrame Spark Scala



我试图将值插入数据帧中,其中字段是string类型中的postgresql数据库中,其中字段是大int类型。

我没有找到如何将它们铸造为大 int。我在integertype之前使用了我没有问题。但是使用此数据框,演员会导致我负面整数

val sparkSession = SparkSession.builder.master("local").appName("spark session example").getOrCreate()
  val cabArticleGold = sparkSession.sqlContext.load("jdbc", Map("url" -> "jdbc:oracle:thin:System/maher@//localhost:1521/XE", "dbtable" -> "IPTECH.TMP_ARTCAB")).select("CODEART", "CAB").limit(10)
import sparkSession.sqlContext.implicits._
 cabArticleGold.show()
cabArticleGold.withColumn("CAB",'CAB.cast(IntegerType)).foreach(row=>println(row(1)))
232524399
-1613725482
232524423
-1613725465
232524437
-1191331072
3486
-1639094853
232524461
1564177573

使用Big Int的任何帮助将不胜感激。我知道scala支持Big Int,但是我该怎么做?

对于大整数,您应该使用 LongType

cabArticleGold.withColumn("CAB", 'CAB.cast(LongType))

cabArticleGold.withColumn("CAB", 'CAB.cast("long"))

您也可以使用DecimalType

cabArticleGold.withColumn("CAB", 'CAB.cast(DecimalType(38, 0)))

cabArticleGold.withColumn("CAB", 'CAB.cast("decimal(38, 0)"))

最新更新