gpt4 book ai didi

java - 使用 MongoDB Java 驱动程序的 MapReduce 因 BSONElement 断言类型错误而失败

转载 作者:行者123 更新时间:2023-11-29 06:44:28 25 4
gpt4 key购买 nike

我对 MongoDB 和 MapReduce 还很陌生。我需要对数据库中的集合执行一些 MapReduce。 MAPREDUCE_MAX 函数有效,因为我能够在 Mongo 交互式 shell (v.1.8.2) 中完成我的需求。但是,我在尝试使用 Mongo Java 驱动程序 (v. 2.6.3) 执行相同操作时遇到错误

我的 MAPREDUCE_MAX 函数如下所示:

String MAP =
"function(){" +
"if(this.type != \"checkin\"){return;}" +
"if(!this.venue && !this.venue.id){return;}" +
"emit({userId:this.userId, venueId:this.venue.id}, {count:1});" +
"};";


String REDUCE_MAX =
"function(key, values){" +
"var res = {count:0};" +
"values.forEach(function(value){result.count += value.count;});" +
"return res;" +
"};";

这是我正在执行的命令:

MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, null, null);

这是我得到的错误:

com.mongodb.CommandResult$CommandFailure: command failed [command failed [mapreduce] { "assertion" : "wrong type for BSONElement (replace) 10 != 2" , "assertionCode" : 13111 , "errmsg" : "db assertion failure" , "ok" : 0.0}

不知道是哪个BSONElement类型不对。我已经用谷歌搜索了 assertionCode: 13111。我还查看了 MongoDB 日志,但没有找到任何线索。

有没有人知道我可能遗漏了什么/做错了什么?如果你们需要更多详细信息,请告诉我。

最佳答案

今天我偶然发现了我的错误,并想在这里分享解决方案,以防万一有人遇到类似的问题。

mapReduce 方法的调用导致了问题:

MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, null, null);

查看此方法的 Javadoc:

/**
* performs a map reduce operation
* Runs the command in REPLACE output mode (saves to named collection)
*
* @param map
* map function in javascript code
* @param outputTarget
* optional - leave null if want to use temp collection
* @param reduce
* reduce function in javascript code
* @param query
* to match
* @return
* @throws MongoException
* @dochub mapreduce
*/

它指出命令是使用 REPLACE 作为输出模式执行的,如果需要一个临时集合,outputTarget 应该是 null .

不幸的是,在 mapReduce 方法中使用的构造函数MapReduceCommand 只允许 outputTarget 可以为空,如果 OutputType 设置为 INLINE(根据 MapReduceCommand.getOutputTarget() 的 Javadoc)。

所以我所要做的就是将第三个参数从 null 更改为一些 String,如下所示:

MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, "tmp", null);

这就像是我在试图弄清楚为什么它不起作用时唯一没有尝试过的参数。我希望有人会觉得这有帮助。

关于java - 使用 MongoDB Java 驱动程序的 MapReduce 因 BSONElement 断言类型错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7306329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com