2

次の構文を使用して、Informixデータベースにsparkで接続しようとしました。IBM 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() 

私は次のエラーを取得します。

の日付値の文字列表現にトークンが不足しています。 「disconnected_on」

は私が IBM Knowledge Center 、インターネットで同じ問題を発見し、それは私がInformixデータベースのデータベース列を変更する必要があるが、ことはできません私の場合で述べています。

私は 'disconnected_on'フィールドをinformixテーブルからロードする前にデータフレーム内の文字列にキャストできますか?

答えて

4

列をキャストするためには、あなたはあなたがdrop()

https://spark.apache.org/docs/latest/api/python/pyspark.sql.html

ドロップを使用して、古い列を削除することができます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')] 

を使用することができます(* 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_oncaststringされる新しい列disconnected_on_strを、追加することができ、かつdrop古い列disconnected_on

jdbcDF_cast = jdbcDF.withColumn("disconnected_on_str", jdbcDF["disconnected_on"].cast("string")).drop("disconnected_on") 
関連する問題