拆分Json数组并插入多个消息驼峰

时间:2018-01-08 11:13:04

标签: json apache-camel jbossfuse jsonpath

我正在使用Jboss fuse 6.3和apache camel 2.17.我想将一条巨大的json消息拆分为camel spring dsl中的多条消息。

示例消息结构如下所示,我有9个国家/地区需要分成两个数组作为5个消息,最后一条消息只包含一个数组。消息的顺序并不重要。

输入讯息:

[{
        "Cities": [{

            "name": "Paris",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Marseille",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Lyon",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Toulouse",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Nice",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "FR"
    },
    {
        "Cities": [{

            "name": "Berlin",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Hamburg",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Munich",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Cologne",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Frankfurt",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "DE"
    }, {
        "Cities": [{

            "name": "mumbai",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Bangalore",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Delhi",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Chattisgarh",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Hyderabad",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "IN"
    }, {
        "Cities": [{

            "name": "Aberdeen",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Armagh",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Bangor",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Bath",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Belfast",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "UK"
    }, {
        "Cities": [{

            "name": "Budapest",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Debrecen",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Szeged",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Miskolc",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Pecs",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "HUNGARY"
    }, {
        "Cities": [{

            "name": "Alingsas",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Arboga",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Arvika",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Askersund",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Avesta",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "SWEDEN"
    }, {
        "Cities": [{

            "name": "Amsterdam",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Den Helder",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Edam",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Haarlem",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Purmerend",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "NETHERLANDS"
    }, {
        "Cities": [{

            "name": "Abrantes",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Agualva-Cacem",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Agueda",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Albufeira",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Alcacer do Sal",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "PORTUGAL"
    }, {
        "Cities": [{

            "name": "Barcelona",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "madrid",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Saragossa",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Salamanca",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Granada",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "SPAIN"
    }
]

预期产出:

首先拆分:

[{
        "Cities": [{

            "name": "Paris",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Marseille",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Lyon",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Toulouse",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Nice",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "FR"
    },
    {
        "Cities": [{

            "name": "Berlin",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Hamburg",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Munich",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Cologne",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Frankfurt",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "DE"
    }
]

第二次分裂:

[{
        "Cities": [{

            "name": "mumbai",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Bangalore",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Delhi",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Chattisgarh",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Hyderabad",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "IN"
    },{
        "Cities": [{

            "name": "Aberdeen",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "Armagh",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Bangor",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Bath",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Belfast",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "UK"
    }
]

。 。

第五次分裂:

[{
        "Cities": [{

            "name": "Barcelona",
            "lat": "45.75",
            "lng": "4.583333"
        }, {

            "name": "madrid",
            "lat": "45.97",
            "lng": "4.54"
        }, {

            "name": "Saragossa",
            "lat": "24.75",
            "lng": "4.75"
        }, {

            "name": "Salamanca",
            "lat": "37.75",
            "lng": "4.755"
        }, {

            "name": "Granada",
            "lat": "89.75",
            "lng": "4.77"
        }],
        "country": "SPAIN"
    }
]

我在下面尝试从Ricardo的帖子中引用Apache Camel with Json Array split。但我不确定如何在spring dsl中配置聚合(AggregationStrategies.groupedExchange())。

骆驼路线:

<split streaming="true">
                    <jsonpath>$</jsonpath>
                    <aggregate completionTimeout="1000" completionSize="2" groupExchanges="true">

                    </aggregate>

                    <log message="${body}"></log>
                </split>

2 个答案:

答案 0 :(得分:0)

这是一个如何使用Spring DSL分割JSON数组的简单示例:

<split>
    <jsonpath>$</jsonpath>
    <!-- Add rest of the logic here -->
</split>

答案 1 :(得分:0)

上述问题中的Spring DSL示例就是这样:

<camel:route>
    <camel:from uri="direct:start" />
    <camel:split streaming="true">
        <camel:jsonpath>$</camel:jsonpath>
        <camel:aggregate completionSize="5"
            completionTimeout="1000" groupExchanges="true">
            <camel:correlationExpression>
                <camel:constant>true</camel:constant>
            </camel:correlationExpression>
            <camel:log message="${body}"></camel:log>
            <camel:to uri="mock:result"></camel:to>
        </camel:aggregate>
    </camel:split>
</camel:route>

这是一个完整的实施示例here.

groupExchange属性只返回默认聚合策略:GroupedExchangeAggregationStrategy

您可以实现自己的并将其作为bean参数传递给您的DSL,如下所示:

<camel:route>
    <camel:from uri="direct:start" />
    <camel:split streaming="true">
        <camel:jsonpath>$</camel:jsonpath>
        <camel:aggregate strategyRef="stringConcatStrategy" completionSize="5"
            completionTimeout="1000" >
            <camel:correlationExpression>
                <camel:constant>true</camel:constant>
            </camel:correlationExpression>
            <camel:log message="${body}"></camel:log>
            <camel:to uri="mock:result"></camel:to>
        </camel:aggregate>
    </camel:split>
</camel:route>

干杯!