gpt4 book ai didi

scala - 由 : java. lang.IllegalArgumentException 引起:无法为 null 获取 JDBC 类型

转载 作者:行者123 更新时间:2023-12-02 18:31:17 31 4
gpt4 key购买 nike

加载 Null 时出现以下错误spark 中数据库的值。 Datatype目标表是 smallint

Caused by: java.lang.IllegalArgumentException: Can't get JDBC type for null

代码 :
val hivedata = spark.sql(s"""select 1 as column1 , B a column2 , NULL as column3 from table""")

hivedata .write.mode(SaveMode.Append).jdbc(url = con, table = targettable, Pconnectionropertiess)

谁能帮帮我吗

最佳答案

cast(NULL as smallint)你必须做...这会将 null 转换为 short类型如下图所示。

val df1 =spark.sql(
" select 1 as column1 , 2 column2 , cast(NULL as smallint) as column3 from table ")
df1.show
df1.printSchema()

结果 :
+-------+-------+-------+
|column1|column2|column3|
+-------+-------+-------+
| 1| 2| null|
+-------+-------+-------+

root
|-- column1: integer (nullable = false)
|-- column2: integer (nullable = false)
|-- column3: short (nullable = true)


否则你的方式它将是空类型而不是小整数类型..
val df1 =spark.sql(" select 1 as column1 , 2 column2 ,  NULL   as column3 from table  ")
df1.show
df1.printSchema()
+-------+-------+-------+
|column1|column2|column3|
+-------+-------+-------+
| 1| 2| null|
+-------+-------+-------+

root
|-- column1: integer (nullable = false)
|-- column2: integer (nullable = false)
|-- column3: null (nullable = true)

这就是您收到异常的原因。

关于scala - 由 : java. lang.IllegalArgumentException 引起:无法为 null 获取 JDBC 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528619/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com