Spark 从 IBM Informix 数据库读取数据 "Not enough tokens are specified in the string representation of a date value"
Spark reading data from IBM Informix database "Not enough tokens are specified in the string representation of a date value"
我尝试使用以下语法连接到 spark 中的 Informix 数据库。
jdbcDF = sqlContext.read.format("jdbc").option("url", "jdbc:informix-sqli://192.168.x.xx:xxxx/INFORMIXSERVER=online").option("dbtable", "informix.detail").option("user", "user").option("password", "xxxxxx").option('driver','com.informix.jdbc.IfxDriver').load()
连接成功,我可以看到数据框的架构。
jdbcDF.printSchema()
root
|-- mobile_no: string (nullable = false)
|-- subscriber_code: string (nullable = false)
|-- connected_date: date (nullable = true)
|-- disconnected_on: date (nullable = true)
|-- att0: string (nullable = true)
但是当从数据框中检索数据时,
jdbcDF.show()
我收到以下错误。
Not enough tokens are specified in the string representation of a
date value. "disconnected_on"
我在网上发现了同样的问题,
IBM Knowledge Center
它说我需要更改 Informix 数据库中的数据库列,但在我的情况下这是不可能的。
在从 informix table 加载之前,我可以将 'disconnected_on' 字段转换为数据帧中的字符串吗?
为了投专栏,可以使用cast()
https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.cast
>>> df.select(df.age.cast("string").alias('ages')).collect()
[Row(ages=u'2'), Row(ages=u'5')]
您可以使用 drop()
删除旧列
https://spark.apache.org/docs/latest/api/python/pyspark.sql.html
drop(*cols)
Returns a new DataFrame that drops the specified column. This is a no-op if schema doesn’t contain the given column name(s).
Parameters: cols – a string name of the column to drop, or a Column to drop, or a list of string name of the columns to drop.
>>> df.drop('age').collect()
[Row(name=u'Alice'), Row(name=u'Bob')]
>>> df.drop(df.age).collect()
[Row(name=u'Alice'), Row(name=u'Bob')]
结合这两个函数,您可以添加一个新列 disconnected_on_str
,即 disconnected_on
cast
变为 string
,而 drop
旧列列 disconnected_on
:
jdbcDF_cast = jdbcDF.withColumn("disconnected_on_str", jdbcDF["disconnected_on"].cast("string")).drop("disconnected_on")
我尝试使用以下语法连接到 spark 中的 Informix 数据库。
jdbcDF = sqlContext.read.format("jdbc").option("url", "jdbc:informix-sqli://192.168.x.xx:xxxx/INFORMIXSERVER=online").option("dbtable", "informix.detail").option("user", "user").option("password", "xxxxxx").option('driver','com.informix.jdbc.IfxDriver').load()
连接成功,我可以看到数据框的架构。
jdbcDF.printSchema()
root
|-- mobile_no: string (nullable = false)
|-- subscriber_code: string (nullable = false)
|-- connected_date: date (nullable = true)
|-- disconnected_on: date (nullable = true)
|-- att0: string (nullable = true)
但是当从数据框中检索数据时,
jdbcDF.show()
我收到以下错误。
Not enough tokens are specified in the string representation of a date value. "disconnected_on"
我在网上发现了同样的问题, IBM Knowledge Center 它说我需要更改 Informix 数据库中的数据库列,但在我的情况下这是不可能的。
在从 informix table 加载之前,我可以将 'disconnected_on' 字段转换为数据帧中的字符串吗?
为了投专栏,可以使用cast()
https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.cast
>>> df.select(df.age.cast("string").alias('ages')).collect()
[Row(ages=u'2'), Row(ages=u'5')]
您可以使用 drop()
https://spark.apache.org/docs/latest/api/python/pyspark.sql.html
drop(*cols)
Returns a new DataFrame that drops the specified column. This is a no-op if schema doesn’t contain the given column name(s).
Parameters: cols – a string name of the column to drop, or a Column to drop, or a list of string name of the columns to drop.
>>> df.drop('age').collect()
[Row(name=u'Alice'), Row(name=u'Bob')]
>>> df.drop(df.age).collect()
[Row(name=u'Alice'), Row(name=u'Bob')]
结合这两个函数,您可以添加一个新列 disconnected_on_str
,即 disconnected_on
cast
变为 string
,而 drop
旧列列 disconnected_on
:
jdbcDF_cast = jdbcDF.withColumn("disconnected_on_str", jdbcDF["disconnected_on"].cast("string")).drop("disconnected_on")