我正在尝试使用spark.read.format读取Oracle表,它适用于所有表,除了少数表的列的数据类型为ROWID之外。
下面是我的代码
var df = spark.read.format("jdbc").
option("url", url).
option("driver", driver).
option("dbtable", dbTable).load()
println(df.first)
我遇到以下错误
18/09/08 11:38:17 WARN scheduler.TaskSetManager: Lost task 0.0 in stage 5.0 (TID 23, gbrdsr000002985.intranet.barcapint.com, executor 21): java.sql.SQLException: Invalid column type: getLong not implemented for class oracle.jdbc.driver.T4CRowidAccessor
at oracle.jdbc.driver.GeneratedAccessor.getLong(GeneratedAccessor.java:440)
at oracle.jdbc.driver.GeneratedStatement.getLong(GeneratedStatement.java:228)
at oracle.jdbc.driver.GeneratedScrollableResultSet.getLong(GeneratedScrollableResultSet.java:620)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$org$apache$spark$sql$execution$datasources$jdbc$JdbcUtils$$makeGetter$8.apply(JdbcUtils.scala:365)
at org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anonfun$org$apache$spark$sql$execution$datasources$jdbc$JdbcUtils$$makeGetter$8.apply(JdbcUtils.scala:364)
分析:我检查了JdbcUtils对象,发现java.sql.Types.ROWID的情况映射到spark的LongType。但是在Oracle JDBC驱动程序中未为ROWID类型实现getLong。 我有信心,如果我设法将其映射到StringType,它将可以工作,但我找不到任何解决方法。请在这里提出解决方案。
到目前为止,我正在使用ResultSet读取数据,并随后使用自定义架构将其转换为DataFrame,但它杀死了并行性。我还想检查是否有任何选项可以修改JdbcUtils的代码,因为在类定义中提到了“ private [sql]”,因此我无法扩展DataFrameReader来创建自定义代码。
答案 0 :(得分:0)
val df=spark.read
.format("jdbc")
.option("driver","oracle.jdbc.OracleDriver")
.option("url","jdbc:oracle:thin:@localhost:1521:orcl")
.option("user","oracle1")
.option("password","oracle1")
.option("dbtable","(select Cast(RID as VARCHAR2(18)) from sample.ALL_RESULTS_DATA) my_table")
.load()
RID是oracle中数据类型ROWID的列名。
上面的代码会将ROWID数据类型转换为String,以便我们可以使用Spark读取它
结果:
-------------------------+
|CAST(RIDASVARCHAR2(18))AS|
+-------------------------+
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|
| AAAIVuAABAAAMhCAAA|