如何使用Apache Camel和MongoDB创建动态equals查询?

huangapple go评论144阅读模式
英文:

How do I create a dynamic equals query using Apache Camel and MongoDB?

问题

以下是翻译好的内容:

我想要从Apache Camel路由动态查询MongoDB集合。Apache Camel文档展示了如何使用constant值查询集合

from("direct:findOneByQuery")
    .setHeader(MongoDbConstants.CRITERIA, Filters.eq("name", "Raul Kripalani"))
    .to("mongodb:myDb?database=flights&collection=tickets&operation=findOneByQuery")
    .to("mock:resultFindOneByQuery");

虽然我确定这对某些人很有用,但我正在尝试使用动态生成的值查询集合,例如使用Exchange头。您该如何做到这一点?

英文:

I'd like to query a MongoDB collection dynamically from an Apache Camel route. The Apache Camel docs show how to query a collection using a constant value:

from("direct:findOneByQuery")
    .setHeader(MongoDbConstants.CRITERIA, Filters.eq("name", "Raul Kripalani"))
    .to("mongodb:myDb?database=flights&collection=tickets&operation=findOneByQuery")
    .to("mock:resultFindOneByQuery");

And while I'm sure that's useful to some folks, I was trying to query a collection with a dynamically generated value, using an Exchange header for instance. How do you do that?

答案1

得分: 1

起初,上面的语句无法编译。在另一个问题中,有人指出当前Apache Camel MongoDB文档中存在错误,并且上面的 setHeader 行应该更改为:

.setHeader(MongoDbConstants.CRITERIA, constant(Filters.eq("name", "Raul Kripalani")))

我最终采取的方法是通过创建匿名 Expression 来实现这一点:

import com.mongodb.client.model.Filters;
import com.mongodb.BasicDBObject;
import org.bson.conversions.Bson;

@Component
public class NotifyClientRoute extends RouteBuilder {
    public static final String NOTIFY_CLIENT_URI = "direct:notifyClient";

    @Override
    public void configure() throws Exception {

      from(NOTIFY_CLIENT_URI)
        .log("确定下一个删除请求的客户端,用于DR请求 '${header.drRequestId}'。")
        .setHeader(MongoDbConstants.CRITERIA, new Expression() {
            @Override
            public <T> T evaluate(Exchange exchange, Class<T> type) {
                String drRequestId = exchange.getIn().getHeader("drRequestId", String.class);

                Bson equalsClause = Filters.eq("drRequestId", drRequestId);

                // 或者:
                // Bson equalsClause = new BasicDBObject("drRequestId", new BasicDBObject("$eq", drRequestId));

                return exchange.getContext().getTypeConverter().convertTo(type, equalsClause);
            };
        })
        .to("mongodb:mongoClient?database=mydb&collection=mycollection&operation=findOneByQuery")
        .log("查询结果: '${body}'");
    }
}

附加说明:在 Expression 中,我没有想出那个返回语句。我遇到了类型错误,所以我查看了Camel库中另一个Apache Camel Expression 实现中包含的内容,然后找到了那个返回语句。

英文:

For starters, that statement above won't compile. It was pointed out to me in another question that there's an error in current Apache Camel MongoDB documentation and that the setHeader line above should read:

.setHeader(MongoDbConstants.CRITERIA, constant(Filters.eq(&quot;name&quot;, &quot;Raul Kripalani&quot;))

The way I ended up doing this was by creating an anonomyous Expression:

import com.mongodb.client.model.Filters;
import com.mongodb.BasicDBObject;
import org.bson.conversions.Bson;

@Component
public class NotifyClientRoute extends RouteBuilder {
    public static final String NOTIFY_CLIENT_URI = &quot;direct:notifyClient&quot;;

    @Override
    public void configure() throws Exception {

      from(NOTIFY_CLIENT_URI)
        .log(&quot;Determining which client gets the deletion request next for DR request &#39;${header.drRequestId}&#39;.&quot;)
        .setHeader(MongoDbConstants.CRITERIA, new Expression() {
            @Override
            public &lt;T&gt; T evaluate(Exchange exchange, Class&lt;T&gt; type) {
                String drRequestId = exchange.getIn().getHeader(&quot;drRequestId&quot;, String.class);

                Bson equalsClause = Filters.eq(&quot;drRequestId&quot;, drRequestId);

                // Alternatively:
                // Bson equalsClause = new BasicDBObject(&quot;drRequestId&quot;, new BasicDBObject(&quot;$eq&quot;, drRequestId));

                return exchange.getContext().getTypeConverter().convertTo(type, equalsClause);
            };
        })
        .to(&quot;mongodb:mongoClient?database=mydb&amp;collection=mycollection&amp;operation=findOneByQuery&quot;)
        .log(&quot;Query returned: &#39;${body}&#39;&quot;);
    }
}

Additional note: I didn't come up with that return statement in the Expression. I was running into type errors, and so I looked at what another Apache Camel Expression implementations included in the Camel libraries were returning, and I found that return clause.

huangapple
  • 本文由 发表于 2020年10月27日 10:55:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64547706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定