在MongoDB中使用MapReduce的共同朋友

时间:2016-08-17 18:17:26

标签: python mongodb mapreduce

我正在MongoDB中尝试MapReduce,我有以下数据类型的

的MongoDB集合
{ "_id" : ObjectId("57aea85af405910cfcd2bfeb"), "friendList" : [ "Karma", " Tom", " Ram", " Bindu", " Shiva", " Kishna", " Bikash", " Bakshi", " Dinesh" ], "user" : "Hari" }

{ "_id" : ObjectId("57aea85bf405910cfcd2bfec"), "friendList" : [ "Karma", " Sita", " Bakshi", " Hanks", " Shyam", " Bikash" ], "user" : "Howard" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfed"), "friendList" : [ "Dinesh", " Ram", " Hanks", " Bindu", " Howard", " Bikash", " Shyam", " Bakshi" ], "user" : "Sita" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfee"), "friendList" : [ "Hanks", " Tom", " Karma", " Hari", " Dinesh" ], "user" : "Shiva" }

{ "_id" : ObjectId("57aea85cf405910cfcd2bfef"), "friendList" : [ "Bakshi", " Kishna", " Hanks", " Shiva", " Bindu", " Hari", " Karma", " Sita" ], "user" : "Dinesh" }

我的代码

map = Code("""
           function () {
               for (var i=0; i<this.friendList.length; i++)
            {
            var friendValue = this.friendList[i];
            var userValue = this.user;

            var tempUserValue = userValue.toLowerCase().trim();
            var tempFriendValue = friendValue.toLowerCase().trim();

                if(tempUserValue>tempFriendValue)
                    {
                    var temp = userValue;
                    userValue = friendValue;
                    friendValue = temp; 
                    }

            var key = {"user" : userValue, "friend" : friendValue};


            emit(key, {"friendList":this.friendList});              
            }

                   }""") 

reduce = Code("""
            function(key, values){
            var combinedfriendList = {"friendList":[]};

            for (var i in values){
                var inter = values[i];
                    for(var j in inter.friendList){
                        combinedfriendList.friendList.push(inter.friendList[j]);
                        }
                        }


            return {"_id": {"user":key.user, "friend": key.friend}, "value":combinedfriendList};

            }""")

这里在map函数中,我已经完成了排序,并且还形成了复合键,以便相同的键通过相同的reducers,即输入到reduce()函数

Map()输出:

{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Bakshi"," Kishna"," Hanks"," Shiva"," Bindu"," Hari"," Karma"," Sita"    ]}}
{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]}}

现在在reducer中我想组合具有相同键的friendList。但是这里我的reduce函数不起作用?

预期输出

{"_id" : {"user" : "Dinesh","friend" : " Hari"},"value" : {"friendList" : ["Bakshi"," Kishna"," Hanks"," Shiva"," Bindu"," Hari"," Karma"," Sita",
"Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]}}

0 个答案:

没有答案