Apache camel SQL语法问题
我的任务是使用Camel版本2.20.0创建Camel路由,该版本从CSV文件中获取一行,并在SQL语句where子句中使用该行的值,合并结果并再次输出。如果我在SQL语句中硬编码标识符,它就可以正常工作,如果我尝试使用动态URI,就会出现错误 路线是:Apache camel SQL语法问题,apache-camel,camel-sql,Apache Camel,Camel Sql,我的任务是使用Camel版本2.20.0创建Camel路由,该版本从CSV文件中获取一行,并在SQL语句where子句中使用该行的值,合并结果并再次输出。如果我在SQL语句中硬编码标识符,它就可以正常工作,如果我尝试使用动态URI,就会出现错误 路线是: from("file:///tmp?fileName=test.csv") .split() .tokenize("\n") .streaming() .parallelProcessing(true) .setHeader("userID",
from("file:///tmp?fileName=test.csv")
.split()
.tokenize("\n")
.streaming()
.parallelProcessing(true)
.setHeader("userID", constant("1001"))
//.enrich("sql:select emplid,name from employees where emplid = '1001'",
.enrich("sql:select name from employees where emplid = :#userID",
new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange,
Exchange newExchange) {...
正如我所说,如果我用硬编码1001取消注释该行,它将查询db并按预期工作。但是,使用“:userID”语法,我得到一个Oracle错误:
java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId ProcessorId Processor Elapsed (ms)
[route3 ] [route3 ] [file:///tmp?fileName=test.csv ] [ 43]
[route3 ] [log5 ] [log ] [ 2]
[route3 ] [setHeader2 ] [setHeader[userID] ] [ 0]
[route3 ] [enrich2 ] [enrich[constant{sql:select name from employees where emplid = :#userID] [ 40]
表显然在那里,因为它在值被硬编码时工作,所以它与传递动态值有关。我尝试了很多关于如何传递变量的变体,在单引号内,使用来自正文的值而不是标题,等等。虽然我看到了很多类似的工作示例,但还没有找到有效的组合
我已打开跟踪,但标题似乎也已正确设置:
o.a.camel.processor.interceptor.Tracer : >>> (route3) setHeader[userID, 1001] --> enrich[constant{sql:select name from employees where emplid = :#userID}] <<< Pattern:InOnly, Headers:{CamelFileAbsolute=true, CamelFileAbsolutePath=/tmp/test.csv, CamelFileLastModified=1513116018000, CamelFileLength=26, CamelFileName=test.csv, CamelFileNameConsumed=test.csv, CamelFileNameOnly=test.csv, CamelFileParent=/tmp, CamelFilePath=/tmp/test.csv, CamelFileRelativePath=test.csv, userID=1001}, BodyType:String, Body:1001,SomeValue,MoreValues
从Camel文档:
pollEnrich或enrich不访问当前数据库中的任何数据
Exchange,这意味着在轮询时无法使用任何现有的
您可能在Exchange上设置的标题
实现你想要的东西的推荐方法是使用recipientList,所以我建议你仔细阅读
编辑:
正如里卡多·扎尼尼(Ricardo Zanini)在回答中正确指出的那样,从2.16版开始,使用驼峰版本实际上可以实现这一点。由于OP使用2.20,我的答案无效
但是,我将保留我的答案,但我想指出,只有当您使用的是比2.16更旧的版本时,这才有效。来自:
从Camel 2.16开始,enrich和pollEnrich都支持使用表达式计算uri的动态端点,允许使用来自当前Exchange的数据。换句话说,上面所说的一切不再适用,它只是起作用
当您使用2.20时,我认为您可以尝试以下示例:
from("file:///tmp?fileName=test.csv")
.split()
.tokenize("\n")
.streaming()
.parallelProcessing(true)
.setHeader("userID", constant("1001"))
//.enrich("sql:select emplid,name from employees where emplid = '1001'",
.enrich("sql:select name from employees where emplid = ':#${in.header.userID}'",
new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange,
Exchange newExchange) {...
查看文档中的主题以获取更多示例
总之,表达式可以是:
sql:从employees中选择名称,其中Employeed=':${in.header.userID}'
编辑:
对不起,我错过了:。你可以看到一个小男孩
只需注意列类型。如果是整数,则不需要引号
干杯 不幸的是,当我尝试这样做时,我得到了另一个异常:org.springframework.jdbc.UncategorizedSQLException:PreparedStatementCallback;SQL[]的未分类SQLException;SQL状态[99999];错误代码[17034];位置45处不支持的SQL92令牌;嵌套异常是java.sql.SQLException:位置45处不受支持的SQL92标记,该位置为'{'。这几乎就像没有发生替换一样。我尝试了使用冒号和不使用冒号,得到了相同的错误,没有冒号,它会在位置44处抛出错误。下面是没有标记的错误:org.springframework.jdbc.UncategorizedSQLException:PreparedStatementCallback;uncategorizedsqlexption for SQL[];SQL state[99999];错误代码[17034];位置44处不支持的SQL92标记;嵌套异常为java.sql.SQLException:位置44处不支持的SQL92标记消息历史以[route3][enrich2][enrich[constant{sql:select name from employees,其中emplid=${in.header.use][56]结束我错过了引号。请尝试以下操作:sql:从employees中选择名称,其中employees ID='${in.header.userID}'。但奇怪的是..似乎动态表达式不起作用,这是在2.16版本中实现的..另外,尝试将常量添加为整数:.setHeaderuserID,constant1001。您的列是整数还是varchar?
from("file:///tmp?fileName=test.csv")
.split()
.tokenize("\n")
.streaming()
.parallelProcessing(true)
.setHeader("userID", constant("1001"))
//.enrich("sql:select emplid,name from employees where emplid = '1001'",
.enrich("sql:select name from employees where emplid = ':#${in.header.userID}'",
new AggregationStrategy() {
public Exchange aggregate(Exchange oldExchange,
Exchange newExchange) {...