我正在尝试将值插入到其中字段为 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)"))
关于postgresql - 将字符串转换为 BigInt 数据帧 spark scala,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46039717/