【发布时间】:2015-12-31 11:23:49
【问题描述】:
Apache camel 如何使用 SQL 组件将地图值插入数据库
我的班级文件:
public class PolluxDataController {
List<PolluxData> stationsMasterList=new ArrayList<PolluxData>();
List<PolluxData> stationProccessedList=new ArrayList<PolluxData>();
Map<String,Object> stationMap=new HashMap<String,Object>();
@SuppressWarnings("unchecked")
public Map<String, Object> processPolluxData(Exchange exchange) throws Exception {
stationsMasterList= (List<PolluxData>) exchange.getIn().getBody();
for (PolluxData value:stationsMasterList){
System.out.println(value.getStationCode() +","+value.getStationShortDescription());
stationMap.put("id",value.getStationCode());
stationMap.put("ltr", value.getStationShortDescription());
}
return stationMap;
}
sql.properties 文件是:
sql.insertNewRecord=INSERT INTO GSI_DEVL.POLLUX_DATA(STID,CLLTR) VALUES(:#id,#ltr)
Context.xml 是
<!-- configure the Camel SQL component to use the JDBC data source -->
<bean id="sqlComponent" class="org.apache.camel.component.sql.SqlComponent">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="polluxDataController" id="polluxDataController" class="com.nielsen.polluxloadspring.controller.PolluxDataController" />
<camelContext trace="false" xmlns="http://camel.apache.org/schema/spring">
<!-- use Camel property placeholder loaded from the given file -->
<propertyPlaceholder id="placeholder" location="classpath:sql.properties" />
<camel:route id="bindy-csv-marhalling-unmarshalling-exmaple" autoStartup="true">
<camel:from uri="file://D://cameltest//input?noop=true&delay=10" />
<camel:log message="CAMEL BINDY CSV MARSHALLING UNMARSHALLING EXAMPLE" loggingLevel="WARN"/>
<camel:unmarshal ref="bindyDataformat" >
<camel:bindy type="Csv" classType="com.nielsen.polluxloadspring.model.PolluxData" />
</camel:unmarshal>
<camel:log message="Station Details are ${body}" loggingLevel="WARN" />
<camel:bean ref="polluxDataController" method="processPolluxData" />
<camel:log message="Station Details after bean process ${body}" loggingLevel="WARN" />
<to uri="sqlComponent:{{sql.insertNewRecord}}" />
<log message="Inserted new NewTopic ${body[id]}" />
<log message="Inserted new NewTopic ${body[ltr]}" />
<camel:log message="COMPLETED BINDY SIMPLE CSV EXAMPLE" loggingLevel="WARN" />
</camel:route>
</camelContext>
问题是这只会在数据库中插入一行,但文件包含 2000 行我怎样才能做到这一点
【问题讨论】:
-
也许您需要使用拆分器将 2000 行拆分为 1 行,您可以一次插入一行:camel.apache.org/splitter
-
我的要求是我的 bean
<camel:bean ref="polluxDataController" method="processPolluxData" />将从列表中返回 Map。类型模型类的列表,即:List 。我将从每个实例中检索 2 个值我的模特班。我需要使用骆驼 sql 组件来插入模型类值。我需要在 sql 的值部分中使用哪个变量? VALUES(:#stationCode,:#callLetter)我可以使用循环插入这个吗,我的意思是我可以每次都调用它来获取新值吗?请帮我解决这个问题?我坚持这个
标签: apache-camel camel-jdbc camel-sql