英文:
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("name", "Raul Kripalani"))
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 = "direct:notifyClient";
@Override
public void configure() throws Exception {
from(NOTIFY_CLIENT_URI)
.log("Determining which client gets the deletion request next for DR request '${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);
// Alternatively:
// 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("Query returned: '${body}'");
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论