Apache camel如何使用sql组件将map值插入数据库

时间:2015-12-31 11:23:50

标签: apache-camel camel-jdbc camel-sql

Apache camel如何使用SQL组件将映射值插入数据库





我的类文件:




  public class PolluxDataController {

列表与LT; PolluxData> stationsMasterList = new ArrayList< PolluxData>();
列表与LT; PolluxData> stationProccessedList = new ArrayList< PolluxData>();
地图<字符串,对象> stationMap = new HashMap< String,Object>();


 @SuppressWarnings( “未登记”)
公共地图< String,Object> processPolluxData(Exchange exchange)抛出异常{

 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文件是:


& #xA;
  sql.insertNewRecord = INSERT INTO GSI_DEVL.POLLUX_DATA(STID,CLLTR)VALUES(:#i​​d,#ltr)
  

&# xA;




Context.xml是




 <! - 配置要使用的Camel SQL组件JDBC数据源 - >
 < bean id =“sqlComponent”class =“org.apache.camel.component.sql.SqlComponent”>
 < property name =“dataSource”ref =“dataSource”/>
 < /豆腐>

 < bean name =“polluxDataController”id =“polluxDataController”class =“com.nielsen.polluxloadspring.controller.PolluxDataController”/>


 < camelContext trace =“false”xmlns =“http://camel.apache.org/schema/spring”>

 <! - 使用从给定文件加载的Camel属性占位符 - >
 < propertyPlaceholder id =“placeholder”location =“classpath:sql.properties”/>

 < camel:route id =“bindy-csv-marhalling-unmarshalling-exmaple”autoStartup =“true”>
 < camel:来自uri =“file:// D:// cameltest //输入?noop = true& amp; 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:log message =“Station Details is $ {body}”loggingLevel =“WARN”/>

 < camel:bean ref =“polluxDataController”method =“processPolluxData”/>

 < camel:log message =“bean进程后的站点详细信息$ {body}”loggingLevel =“WARN”/>

 < to uri =“sqlComponent:{{sql.insertNewRecord}}”/>
 < log message =“插入新的NewTopic $ {body [id]}”/>
 < log message =“插入新的NewTopic $ {body [ltr]}”/>

 < camel:log message =“COMPLETED BINDY SIMPLE CSV EXAMPLE”loggingLevel =“WARN”/>
 < /骆驼:路线>

 < / camelContext> 
  




问题是这只会向数据库插入一行,但该文件包含2000行我怎样才能实现此

& #xA;

2 个答案:

答案 0 :(得分:1)

如下所示更改Bean方法

public class PolluxDataController  {

List<PolluxData> stationsMasterList=new ArrayList<PolluxData>();

Map<String,Object> stationMap=null;
List<Map<String,Object>> stationProccessedList=new ArrayList<Map<String,Object>>();

@SuppressWarnings("unchecked")
public List<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=new HashMap<String,Object>();
               stationMap.put("id",value.getStationCode());
               stationMap.put("ltr", value.getStationShortDescription());                  
               stationProccessedList.add(stationMap);
    }

    return stationProccessedList;
}

}

通过添加参数batch = true来更改sql.properties,默认情况下,这会将列表中的所有内容插入到db not record记录中。如果您想一次只选择并插入两条记录,那么您的业务逻辑就是错误的。

答案 1 :(得分:0)

您的地图stationMap将只包含两个条目。在for (PolluxData value:stationsMasterList)中,您始终会为每个PolluxData重置这两个条目。只有一个地图有两个内部 - 只有一个插入,而不是2000.我认为业务逻辑有一些错误(填写地图的算法stationMap)。