【问题标题】:Apache camel how to insert map value to data base using sql componentApache骆驼如何使用sql组件将地图值插入数据库
【发布时间】: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&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 行我怎样才能做到这一点

【问题讨论】:

  • 也许您需要使用拆分器将 2000 行拆分为 1 行,您可以一次插入一行:camel.apache.org/splitter
  • 我的要求是我的 bean &lt;camel:bean ref="polluxDataController" method="processPolluxData" /&gt; 将从列表中返回 Map。类型模型类的列表,即:List。我将从每个实例中检索 2 个值我的模特班。我需要使用骆驼 sql 组件来插入模型类值。我需要在 sql 的值部分中使用哪个变量? VALUES(:#stationCode,:#callLetter) 我可以使用循环插入这个吗,我的意思是我可以每次都调用它来获取新值吗?请帮我解决这个问题?我坚持这个

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


【解决方案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 ,默认情况下,这会将列表中的所有内容插入到数据库中,而不是一次记录。如果您想一次只选择并插入两条记录,那么您的业务逻辑是错误的。

【讨论】:

  • 如果这对您有帮助,您可以标记为已回答或投票吗.. 以便其他人以后可以查看此内容
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多