0

Apache骆驼如何使用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&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: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 行我怎么能做到这一点

4

2 回答 2

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;
}

}

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

于 2016-01-08T21:43:14.457 回答
0

您的地图stationMap将仅包含两个条目。在for (PolluxData value:stationsMasterList)你总是为每个重置这两个条目PolluxData。只有一张地图,里面有两个实体——只有一个插入,而不是 2000 年。我认为业务逻辑有问题(stationMap可能是填充地图的算法)。

于 2016-01-08T21:31:13.337 回答