java.sql.SQLException:将 Spark 数据帧保存到 Sybase 时找不到类型"TIMESTAMP"



我在java应用程序中使用spark,我必须将数据框保存到现有的db表中。数据库是Sybase。Spark版本3.0.1。我在数据框中有几个Timestamp类型的字段。所以看起来他们不能映射到类型的数据库字段'datetime',但为什么不呢?我正在使用这个语句试图插入数据框架:

outputDS.write().mode(SaveMode.Append).jdbc(URL, tableName, properties);

也有语句的例子,我如何创建时间戳列在输出数据集:

.withColumn("DateCreated", lit(new TimeStamp(System.currentTimeMillis())).cast(DataTypes.TimestampType))

模式:

dataframe模式:

rDay: timestamp
rName: string
rValue: double
rId: integer
rCountry: string
rRegion: string 
rCustomerId: string 
rLevel: string 
rUserCreated: string
rUserUpdated: string
rDateCreated: timestamp 
rDateUpdated: timestamp 

db table schema:

rId                bigint
rCustomerId        bigint 
rCountry           varchar(50)
rRegion            varchar(15)
rName              varchar(50)
rValue             decimal(8,4)
rLevel             varchar(30)
rDay               datetime
rUserCreated       varchar(15)
rDateCreated       datetime 
rUserUpdated       varchar(15)
rDateUpdated       datetime 

所以,正如我所理解的,从spark数据框插入行到现有的db表,他们的模式必须是相等的。但是为了达到这个目的,我得到了这个异常:

java.sql。SQLException: Can't find type 'TIMESTAMP'

当我尝试更改字段的时间戳类型时,例如DateType,我得到另一个异常,如"在数据库中已经有一个对象'tableName',所以我猜这是因为模式这次不匹配。那么,有没有什么办法可以做到呢?提前感谢!

不需要再次强制转换为DataTypes.TimestampType作为:

.withColumn("DateCreated", lit(new Timestamp(System.currentTimeMillis())))

已经给出timestamp类型的列DateCreated

此外,您应该使用Spark内置函数current_timestamp:

.withColumn("DateCreated", current_timestamp())

我得到这个异常java.sql.SQLException: Can't find type 'TIMESTAMP'

我不太了解Sybase,但你可以尝试在你写入表之前将时间戳列字符串化:

.withColumn("DateCreated", date_format(current_timestamp(), "yyyy-MM-dd HH:mm:ss"))