原因:java.lang.illegalargumentexception:无法获取null的jdbc类型

r8xiu3jd  于 2021-05-31  发布在  Hadoop
关注(0)|答案(1)|浏览(1174)

加载时出现以下错误 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)

有人能帮我吗
c8ib6hqw

c8ib6hqw1#

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)

另一方面,它将是nulltype而不是small int类型。。

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)

这就是你得到例外的原因。

相关问题