蒙哥地图减少第一次 [英] Mongo Map Reduce first time

查看:76
本文介绍了蒙哥地图减少第一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Map/Reduce用户,并使用MongoDB.我有很多页面访问数据,我想通过使用Map/Reduce来有所了解.下面基本上是我想要做的,但是作为一个初学者,Map/Reduce我认为这超出了我的知识!

First time Map/Reduce user here, and using MongoDB. I have a lot of page visit data which I'd like to make some sense of by using Map/Reduce. Below is basically what I want to do, but as a total beginner a Map/Reduce, I think this is above my knowledge!

  1. 浏览过去30天内所有访问过的页面,其中external = true.
  2. 然后在每个页面上找到所有访问
  3. 按推荐地点对所有访问进行分组
  4. 对于每个引荐位置,计算有多少次访问了具有特定类型"并且在标签"中也包含特定单词的网页.

数据库和集合的组织方式为

The database and collection are organised as

$mongo->dbname->visits

示例文档为:

{"url": "www.example.com", "type": "a", "refer": {"external": true, "domain": "twitter.com", "url": "http://www.twitter.com/page"}, "page": "1235", "user": "1232", "time": 1234567890}

然后我要查找带有特定标签的B型文档.

And then I want to find documents of type B with a certain tag.

{"url": "www.example.com", "type": "b", "page": "745", "user": "1232", "time": 1234567890, "tags": {"a", "b", "c"}}

如果有影响,我正在使用普通的Mongo PHP扩展.

I'm using the normal Mongo PHP extension if that has an impact.

推荐答案

好的,我想出了一些我认为可能会做的事情.请注意,由于我不能100%确定您的模式(考虑到您的示例显示类型c中的refer而不是类型b)(我不确定这是否是遗漏或考虑您的情况,所以这可能无法完全正常工作)想要通过引荐查看)...无论如何,这就是我想出的内容:

Ok, I've come up with something that I think may do what you want. Note, that this may not work exactly since I'm not 100% sure of your schema (considering your examples show refer available in type a, but not b (I'm not sure if that's an omission, or what considering you want to view by referer)... Anyway, here's what I've come up with:

地图功能:

function() {
    var obj = {
        "types": {},
        "tags": {},
    }
    obj.types[this.type] = 1;
    if (this.tags) {
        for (var tag in this.tags) {
            obj.tags[this.tags[tag]] = 1;
        }
    }
    emit(this.refer.url, obj);
}

Reduce函数:

function(key, values) {
    var obj = {
        "types": {},
        "tags": {},
    }
    for (var i = 0; i < values.length; i++) {
        for (var type in values[i].types) {
            if (!type in obj.types) {
                obj.types[type] = 0;
            }
            obj.types[type] += values[i].types[type];
        }
        for (var tag in values[i].tags) {
            if (!tag in obj.tags) {
                obj.tags[tag] = 0;
            }
            obj.tags[tag] += values[i].tags[tag];
        }
    }
    return obj;
}

因此,基本上,它是如何工作的. Map函数使用了refer.url键(我根据您的描述猜测了这个键).因此,最终结果看起来像一个数组,其中_id等于refer.url(它基于url进行分组).然后,它创建一个在其下具有两个对象(类型和标签)的对象.该对象的原因是map和reduce可以发出相同格式的对象.除此之外,我认为这应该是相对自我解释的(如果您不理解,我可以尝试解释更多)...

So basically, how it works is this. The Map function uses a key of refer.url (what I guessed based on your description). So the end result will look like an array with _id equal to refer.url (It groups based on url). It then creates an object that has two objects under it (types and tags). The reason for the object is so that map and reduce can emit the same format object. Other than that, I THINK that it should be relatively self explanatory (If you don't understand, I can try to explain more)...

因此,让我们在PHP中实现此功能(假设$map$reduce是包含上述内容的字符串,以简化它们):

So let's implement this in PHP (Assuming that $map and $reduce are strings with the above contained with them for terseness):

$mapFunc = new MongoCode($map);
$reduceFunc = new MongoCode($reduce);
$query = array(
    'time' => array('$gte' => time() - (60*60*60*24*30)),
    'refer.external' => true
);
$collection = 'visits';
$command = array(
    'mapreduce' => $collection,
    'map' => $mapFunc,
    'reduce' => $reduceFunc,
    'query' => $query,
);

$statsInfo = $db->command($command);

$statsCollection = $db->selectCollection($sales['result']);

$stats = $statsCollection->find();

foreach ($stats as $stat) {
    echo $stats['_id'] .' Visited ';
    foreach ($stats['value']['types'] as $type => $times) {
        echo "Type $type $times Times, ";
    }
    foreach ($stats['value']['tags'] as $tag => $times) {
        echo "Tag $tag $times Times, ";
    }
    echo "\n";
}

注意,我尚未对此进行测试.这正是基于我对您的架构的理解以及对Mongo及其Map-Reduce实现的理解而得出的.

Note, I haven't tested this. This is just what I've come up with based on my understanding of your schema, and from my understanding of Mongo and its Map-Reduce implementation...

这篇关于蒙哥地图减少第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆