我正在尝试通过 spark sql context 通过以下查询
从大型机表中获取记录:data_config.db2_qry =从Scheema中选择A.E_No,A.E_Name.Employee A WITH UR
但它抛出以下错误
com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL错误:SQLCODE = -199,SQLSTATE = 42601,SQLERRMC = WITH;其中组顺序相减减去工会),FETCH,DRIVER = 4.19.26 < / p>
但是如果我直接在大型机控制台中运行相同的查询,它将很好地工作。
如何在Spark的SQL上下文中使用 WITH 子句?
我正在使用Spark版本2.4.0
我正在检索以下记录
filt_cond =“(” + data_config.db2_qry +“)ref_id”
db2Df = sqlContext.read.format(“ jdbc”)。option(“ url”,data_config.db2_url).option(“ driver”, “ com.ibm.db2.jcc.DB2Driver”)。option( “ dbtable”,filt_cond).option(“用户”,data_config.db2_uname).option(“ password”, data_config.db2_passwd).load()
答案 0 :(得分:1)
问题出在向下发送到Mainframe DB2的查询中,用于更改“ WITH UR”的spark jdbc方法选择需要更改。
这里使用的spark jdbc读取方法是
def jdbc(url: String, table: String, properties: Properties): DataFrame
,用这种方法,我们将以下查询推送到db2 sql引擎
"select a, b, c, d from table where d is not null with UR as table"
,这与在Mainframe DB2 SQL引擎中推送的查询不同。 spark将sql发送为
select a, b, c from (select a, b, c from table where d is not null with UR) as table
这是麻烦开始的地方。
如果要在Mainframe SPUFI或QMF或其他工具中看到相同的SQL错误,请尝试通过spark而不是我们在代码中编写的内容来运行构造的查询。
要克服在SQL上添加“ WITH UR”语法的问题,而不是通过上述spark jdbc方法切换到以下允许我们构造谓词的spark jdbc方法。
def jdbc(url: String, table: String, predicates: Array[String],
connectionProperties: Properties): DataFrame
将SQL推送为""select a, b, c, d from table as tbl"
与predicates= Array("d is not null with UR")
在这种情况下,预期查询被下推。希望这可以帮助您找到解决的方向。
在这里您可以查看关于spark jdbc读取方法的更多详细信息-Link