0

骆驼SQL 2.22.1

在我的路线中,我有:

from("direct:targetDatabase").startupOrder(1)
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println("BODY:"+exchange.getIn().getBody());
                System.out.println(
                     exchange.getIn().
                       getBody(MessageContentsList.class).get(1));
           }
        })

从控制台我看到:

BODY:[[{"id":2314,"code":"3498HK","value":30.88}], testInsert1]
testInsert1

我想使用这个值来选择使用 SQLComponent 执行哪个 sql 语句。

我有 sql.properties 文件:

sql.insertdata=insert into gsdata values(:#id, :#code, :#value)
sql.testInsert1=insert into gsdata2 values(:#id, :#code, :#value)

在我的路线中,我知道这有效:

.to("sqlComponent:{{sql.insertdata}}")

但我想这样做(或以某种方式实现):

.toD("sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}")

这在部署到 Tomcat 时给了我一个骆驼运行时异常。

24-Oct-2018 19:52:36.433 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
 org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Split[simple{${body}} -> [process[Processor@0x7784550a], DynamicTo[]]] <<< in route: Route(route4)[[From[direct:targetDatabase]] -> [process[Proc... because of Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1830)
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:136)
    at org.apache.camel.spring.CamelContextFactoryBean.start(CamelContextFactoryBean.java:369)
    at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:416)
    at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:94)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:985)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route route4 at: >>> Split[simple{${body}} -> [process[Processor@0x7784550a], DynamicTo[]]] <<< in route: Route(route4)[[From[direct:targetDatabase]] -> [process[Proc... because of Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1346)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:206)
    at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1145)
    at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3731)
    at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3445)
    at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:209)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3253)
    at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3249)
    at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3272)
    at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3249)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3165)
    at org.apache.camel.spring.SpringCamelContext.start(SpringCamelContext.java:133)
    ... 26 more
Caused by: java.lang.IllegalArgumentException: Property with key [sql.${bodyAs(java.util.List).get(1)] not found in properties from text: sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.getPropertyValue(DefaultPropertiesParser.java:270)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.readProperty(DefaultPropertiesParser.java:156)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.doParse(DefaultPropertiesParser.java:115)
    at org.apache.camel.component.properties.DefaultPropertiesParser$ParsingContext.parse(DefaultPropertiesParser.java:99)
    at org.apache.camel.component.properties.DefaultPropertiesParser.parseUri(DefaultPropertiesParser.java:62)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:235)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:178)
    at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2552)
    at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:735)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:455)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:441)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:185)
    at org.apache.camel.model.ProcessorDefinition.createChildProcessor(ProcessorDefinition.java:204)
    at org.apache.camel.model.SplitDefinition.createProcessor(SplitDefinition.java:101)
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:562)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:523)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:239)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1343)
    ... 38 more

我已经尝试了该语句的许多变体,但均未成功。有三个问题:

  1. 如何动态设置 sqlcomponent 将用于获取适当 sql 的值?
  2. 如何正确地从正文中检索值?
  3. 有一个更好的方法吗?

非常感谢,这是有史以来最好的网站!

4

2 回答 2

0

得到了一个有效的答案:

.setProperty("sqlQuery", simple("${properties:sql.${bodyAs(java.util.List).get(1)}}"))
.log(LoggingLevel.INFO,"SQLQuery: ${exchangeProperty[sqlQuery]}")
.unmarshal(csv)
.split(body())
.toD("sql:${property.sqlQuery}?dataSource=testDataSource&batch=true")
.end()

我现在可以有一个专门针对每种预期的 CSV 格式文件的 SQL 插入查询。不必重新编译以支持其他 CSV。

道具文件中的示例 SQL(用于 PostreSQL)

10=insert into source_upload_data(st_id,st_code,st_avg_cost,uploadid)values(:#id::integer,:#name,:#amount::numeric,10);

示例 CSV:

id,name,amount
1,Box,230.21
2,Bucket,7865.43
3,Tyre,89
于 2019-02-07T23:53:05.920 回答
0

你能试一下吗 :

toD(simple("sqlComponent:{{sql.${bodyAs(java.util.List).get(1)}}}"))

文档

我会在动态路由之前将所需的值存储在交换属性中。

于 2018-10-24T12:13:36.887 回答