如何使用MongoDB中的对象丰富有效负载(camel-mongodb)

时间:2016-03-24 15:45:18

标签: mongodb apache-camel enterprise-integration

我正在尝试从MongoDb中提取对象并将其添加到我当前的有效负载并将其保存在另一个数据库中:

@Override
public void configure() throws Exception
{
    from(kafkaEndpoint)
            .convertBodyTo(DBObject.class)
            .enrich("mongodb:mongoDb?database=myDbName1&collection=UserColl&operation=findOneByQuery",
                    (original, external) -> {
                        DBObject originalBody = original.getIn().getBody(DBObject.class);
                        DBObject externalBody = external.getIn().getBody(DBObject.class);

                        Map<String, DBObject> map = new HashMap<String, DBObject>();
                        map.put("original", originalBody);
                        map.put("external", externalBody);

                        original.getIn().setBody(map);
                        return original;
                    })
            .to("mongodb:mongoDb?database=myDbName2&collection=UserColl&operation=insert");
}

充实的问题从保存原始对象的In.body中获取查询...

那么如何将查询( {“entity.id”:“”} )传递给 enrich(mongoldb:...)并保留原始对象以进行合并结果如何?

感谢。

1 个答案:

答案 0 :(得分:0)

    @Override
    public void configure() throws Exception
    {
        from(kafkaEndpoint)
                .convertBodyTo(DBObject.class)
                .enrich("direct:findOneByQuery",     // <-------
                        (original, external) -> {
                            DBObject originalBody = original.getIn().getBody(DBObject.class);
                            DBObject externalBody = external.getIn().getBody(DBObject.class);

                            Map<String, DBObject> map = new HashMap<String, DBObject>();
                            map.put("original", originalBody);
                            map.put("external", externalBody);

                            original.getIn().setBody(map);
                            return original;
                        })
                .to("mongodb:mongoDb?database=myDbName2&collection=UserColl&operation=insert");

    }

    from("direct:findOneByQuery")
            .process(new Processor()
            {
                @Override
                public void process(Exchange exchange) throws Exception
                {
                    DBObject body = exchange.getIn().getBody(DBObject.class);
                    DBObject query = BasicDBObjectBuilder.start()
                            .append("entity._id", body.get("_id"))
                            .get();

                    exchange.getIn().setBody(query);
                }
            })
            .to("mongodb:mongoDb?database=myDbName1&collection=UserColl&operation=findOneByQuery");

 //