验证datamapper中的字段(CSV到JSON)。

时间:2015-02-11 01:22:46

标签: mule datamapper

使用mule datamapper从CSV转换为JSON。我想检查必填字段是否为空。如果为空,则记录该字段并将其丢弃以供进一步处理。

我知道在脚本选项中我们有if(input.data.length> 0)。 但如果失败,如何丢弃整行呢?

1 个答案:

答案 0 :(得分:1)

只需将整个转换封装在if语句的开始和结束括号中,就可以在mule datamapper中执行此操作。像这样:

if ( input.Quantity > 0 ) {
    output.id = input.id;
    output.Customer = input.Customer;
    output.Quantity = input.Quantity;
    output.Price = input.Price;
}

然而,一种不同的,可能更好的方法是让datamapper将每一行转换为JSON,然后在流中分离和过滤为单独的步骤。

<flow name="filterindatamapperFlow2" doc:name="filterindatamapperFlow2">
    <file:inbound-endpoint path="/tmp/inbox" doc:name="Inbound file"/>
    <data-mapper:transform config-ref="CSV_To_UnfilteredJSON" doc:name="CSV To Unfiltered JSON"/>
    <request-reply>
        <vm:outbound-endpoint path="splittandprocess" exchange-pattern="one-way"/>
        <vm:inbound-endpoint path="result"/>
    </request-reply>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <file:outbound-endpoint path="/tmp/outbox" doc:name="Outbound file"/>
</flow>

<flow name="splittandprocess">
    <vm:inbound-endpoint path="splittandprocess" exchange-pattern="one-way"/>
    <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>
    <splitter expression="#[payload]" doc:name="Splitter"/>
    <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
    <message-filter  doc:name="Filter Out Orders With No Quantity" onUnaccepted="handleFilteredMessages">
        <expression-filter expression="#[payload['Quantity'] > 0]" />
    </message-filter>
    <collection-aggregator failOnTimeout="false" timeout="1000"/>
    <vm:outbound-endpoint path="result" exchange-pattern="one-way"/>
</flow>
<flow name="handleFilteredMessages">
    <logger message="Payload filtered #[payload]" level="ERROR" doc:name="Logger"/>
</flow>