2017-12-09 8 views
1

AVROファイル形式とHive外部表を使用してデータにアクセスするために、いくつかのOracle表をHadoopに保管しました。
私はインポート時にOracleのTO_CHAR関数を使用して、書式設定されたStringとしてDateおよびTimestampの値を格納しました。日付/タイムスタンプ文字列をOracle DBの日付タイムスタンプ列に書き込む方法は?

ここで、この正確なデータをSparkで日付列を持つOracleテーブルにエクスポートしたいとします。私は、次のコマンドを使用します。

// Create a data frame from the Hive table 
val data = sqlContext.sql("select * from avro_table") 

// export df to existing oracle table 
data.write.mode("overwrite").jdbc(jdbcString, "tableName", prop) 

しかし、その後、私はエラーを取得:

ORA-00902: invalid data type

それが日付列に文字列を挿入しようとするためです。 SparkデータフレームからOracle日付/タイムスタンプ列に日付/タイムスタンプ文字列を挿入する安全な方法はありますか?安全であれば、タイムゾーン情報を失うことはありません。

+0

あなたはdata.printSchema'とあなたの質問ではOracleで日付列の形式は、それが完全にするので、答えは「良く得ることができる '含めることができます"? –

答えて

1

あなたはそれらの対応する型を意識したものに日付/タイムスタンプの値を文字列化からの変換を行うためにto_dateto_timestampおよび/またはdate_format機能を使用する必要があります。

date_format(dateExpr: Column, format: String): Column Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument.

to_date(e: Column, fmt: String): Column Converts the column into a DateType with a specified format (see http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html) return null if fail.

to_timestamp(s: Column, fmt: String): Column Convert time string to a Unix timestamp (in seconds) with a specified format (see http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html) to Unix timestamp (in seconds), return null if fail.

使用selectwithColumn演算子。次のように

サンプルコードは次のようになります。

data.withColumn("real_date", date_format(...)) 
    .write 
    .mode("overwrite") 
    .jdbc(jdbcString, "tableName", prop) 
関連する問題