简体   繁体   English

我们如何使用 jdbc 驱动程序 Z572D4E421E5E06B9BC111D82EZ 服务器从 mysql 服务器读取 spark scala 中的无效日期列

[英]How can we read invalid date column in spark scala from mysql server using jdbc driver url (connection)

I am getting error while reading this column from mysql server从 mysql 服务器读取此列时出现错误

id ID date日期
1 1 0000-00-00 0000-00-00
2 2 0000-00-01 0000-00-01

in the above data set we can handle 0000-00-00 by using mysql server Additional parameter zeroDateTimeBehavior=convertToNull在上述数据集中我们可以使用 mysql 服务器处理 0000-00-00 附加参数 zeroDateTimeBehavior=convertToNull

but i don't know how to handle this type of date 0000-00-01但我不知道如何处理这种类型的日期 0000-00-01

help me error message i got帮帮我我收到的错误消息

Exception in User Class: org.apache.spark.SparkException : Job aborted due to stage failure: Task 0 in stage 6.0 failed 4 times, most recent failure: Lost task 0.3 in stage 6.0 (TID 11) (10.100.4.111 executor 1): java.sql.SQLException: YEAR

i am using this我正在使用这个

val a = "jdbc:mysql://<host_name>:3306/<database_name>?zeroDateTimeBehavior=convertToNull"

val mysqlServerDF = sparkSession.read.format("jdbc")
                .option("url", a)
                .option("query", sql)
                .option("user",jdbcUserName)
                .option("password", jdbcPassword)
                .load()

sql is a sql query example "select * from table" sql 是一个 sql 查询示例“从表中选择 *”

If fixing such dates in your database is not an option, I think your best bet would be to handle it directly in your sql query.如果无法在数据库中修复此类日期,我认为最好的办法是直接在sql查询中处理它。 Eg we can compare for valid date range, which is '1000-01-01' to '9999-12-31' according to docs :例如,我们可以根据文档比较有效日期范围,即“1000-01-01”到“9999-12-31”:

val sql = """
  select 
    id, 
    case 
      when 
        not cast(date as char(10)) 
        between '1000-01-02' and '9999-12-30' 
      then 
        null 
      else 
        date 
    end 
  from table1"""

val mysqlServerDF = sparkSession.read.format("jdbc")
                .option("url", a)
                .option("query", sql)
                .option("user",jdbcUserName)
                .option("password", jdbcPassword)
                .load()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM