简体   繁体   English

如何在java中的mapReduce中调用mongodb服务器端函数

[英]how to call mongodb server side function inside mapReduce in java

i have stored two functions in the "system.js" collection named "mapfun" and "reducefun" and i am trying to call those function from java.我在名为“mapfun”和“reducefun”的“system.js”集合中存储了两个函数,我试图从java调用这些函数。 i am trying to call these function through MapReduceCommand.我正在尝试通过 MapReduceCommand 调用这些函数。 i am not able to call these functions.我无法调用这些函数。 can anyone please help me with this.任何人都可以帮我解决这个问题。 the two function looks like this //mapfun这两个函数看起来像这样 //mapfun

{ "_id" : "mapfun","value" : { "code" : "function(){var criteria; if(this.speed > 70 ){ criteria="overspeed";emit(criteria,this.speed); } }" } }

//reducefun //减少乐趣

{ "_id" : "reducefun", "value" : { "code" : "function(key,speed){ var total=0; for(var i=0;i<speed.length;i++){ total=total+speed[i]; } return total/speed.length; }" } }

and my mapreducecommand looks like this我的 mapreducecommand 看起来像这样

MapReduceCommand command=new MapReduceCommand(collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput output=collection.mapReduce(command);

i have passed the map and reduce function as strings in which i have called the mapfun and reducefun respectively and it looks like this我已经将 map 和 reduce 函数作为字符串传递,其中我分别调用了 mapfun 和 reducefun,它看起来像这样

String map = "function (){var x=mapfun();"
                + "return x;};";
String reduce = "function(key, speed) {var y=reducefun(key,speed);"
                + "return y;};";

what am i doing wrong here and how to correct this kindly help me with this.我在这里做错了什么以及如何纠正这个问题请帮助我解决这个问题。

The underlying problem is that the this variable is reassigned for each function call.潜在的问题是为每个函数调用重新分配了this变量。

To demonstrate, I ran a short test from the mongo shell.为了演示,我从 mongo shell 运行了一个简短的测试。

Fist create a collection containing 1 document:拳头创建一个包含 1 个文档的集合:

MongoDB Enterprise replset:PRIMARY> db.maptest.find({},{_id:0})
{ "canary" : "tweet" }

Then create a stored function that will take an argument emit 2 values, one using this and one using the passed argument:然后创建一个存储函数,该函数将接受一个参数发出 2 个值,一个使用this ,一个使用传递的参数:

MongoDB Enterprise replset:PRIMARY> db.system.js.find()
{ "_id" : "testfun", "value" : { "code" : "function(arg){emit(\"this.canary\",this.canary),emit(\"arg.canary\",arg.canary)}" } }

Then use the stored function in a mapReduce call:然后在 mapReduce 调用中使用存储的函数:

MongoDB Enterprise replset:PRIMARY> db.maptest.mapReduce(function(){testfun(this)},function(a,b){return {a:a,b:b}},{out:{inline:1}})
{
    "results" : [
        {
            "_id" : "arg.canary",
            "value" : "tweet"
        },
        {
            "_id" : "this.canary",
            "value" : undefined
        }
    ],
...

As you can see, this.canary was undefined, but arg.canary contained the value from the input document.如您所见, this.canary未定义,但arg.canary包含来自输​​入文档的值。

The mapReduce framework assigns this to the document currently being examined when calling the map function. MapReduce框架受让人this调用地图功能时,目前正在审查的文件。 When the stored function is called from within the map function it gets its very own this .当从 map 函数内部调用存储的函数时,它会获得自己的this However, by calling the function as testfun(this) the original this context of the map function is provided to the stored function as an argument.但是,通过将函数作为testfun(this)调用,映射函数的原始this上下文作为参数提供给存储的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM