1

Noob 在这里使用 MongoDB Map Reduce...但是找不到任何关于这里出了什么问题的文档。无论如何-这是一个示例文档:

数组([_id] => MongoId 对象([$id] => 5076e95791fa5e4c69002ec3)

[dataProviders] => Array
    (
        [0] => Array
            (
                [key] => facebook
                [data] => Array
                    (
                        [id] => 100001583260053
                        [name] => MyName
                        [link] => http://www.facebook.com/Mynameagain
                        [username] => MyName
                        [birthday] => 02/19/1959
                        [hometown] => Array
                            (
                                [id] => 104022969633581
                                [name] => MyName
                            )

                        [location] => Array
                            (
                                [id] => 104022969633581
                                [name] => MyHome
                            )

                        [bio] => MyBio
                        [work] => Array
                            (
                                [0] => Array
                                    (
                                        [employer] => Array
                                            (
                                                [id] => 119738361373063
                                                [name] => .....
                                            )

                                        [start_date] => 0000-00
                                        [end_date] => 0000-00
                                    )

                            )

                        [education] => Array
                            (
                                [0] => Array
                                    (
                                        [school] => Array
                                            (
                                                [id] => 108891959147272
                                                [name] => ....
                                            )

                                        [type] => High School
                                    )

                                [1] => Array
                                    (
                                        [school] => Array
                                            (
                                                [id] => 133940149955590
                                                [name] => .....
                                            )

                                        [type] => College
                                    )

                            )

                        [gender] => female
                        [email] => MyEmail@myemail.com
                        [timezone] => 2
                        [locale] => US
                        [languages] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 106412222728952
                                        [name] => MyName
                                    )

                            )

                        [verified] => 1
                        [updated_time] => 2012-08-10T17:33:16+0000
                        [avatarUrl] => https://graph.facebook.com/1000/picture
                        [avatarUrlOriginal] => https://graph.facebook.com/1000/picture?type=large
                        [avatarUrlLarge] => https://graph.facebook.com/1000/picture
                    )

            )

    )

我的问题是我正在尝试运行一个基本的 MapReduce,只从该组中提取 Facebook 用户。基本上我想做一个非常简单的计数。据我所知,这应该可以工作,但我收到一条错误消息:

error_reporting(E_ALL);
ini_set('display_errors', '1');
include "phpmapreduce/lib/MongoMapReduce.php";
include "phpmapreduce/lib/MongoMapReduceResponse.php";
include "phpmapreduce/lib/XMongoCollection.php";

$collection_name = "users";

$map = "function(){emit({source: this.dataProviders.0.key}, {count: 1}); }}";

$reduce = "function(key, values){
            var sum= {count: []};
            for(var i in values)  
                sum += values[i];
            return sum;
            }";
$query = array('dataProviders.0.key' => 'facebook');

echo "Here's our map Function! <br/>";
$mapFunc = new MongoCode($map); 
echo "<pre>";
print_r($mapFunc);
$reduceFunc = new MongoCode($reduce);
print_r($reduceFunc);
$command= array(
       "mapreduce"=>'texasHoldem.users',
    "map"=>$mapFunc,
    "reduce"=>$reduceFunc,
        "query" =>$query,
         "out" => array(
             "inline" => 1)
);
try {
    $mongo = new Mongo('mongodb://1.2.3.4.5.6.7./mydb', array("replicaSet" => true));
}
catch(MongoConnectionException $e) {
    $msg = "Error Connecting to MongoDB" . "<br/>";
    die($msg);
}
echo "Connected" . "<br/>";
$db = $mongo->selectDb('yallapop');

echo "got mydb!" . "<br/>";

$db->setSlaveOkay(); // This line is needed to be able to read from slave
$collection= $db->selectCollection('texasHoldem.users'); // Users collection
echo "Got the Collection" . "<br/>";
echo "setting timeout" . "<br/>";
//$cursor = $collection->find()->timeout(30000);
MongoCursor::$timeout = -1;
echo "<pre>";
echo "let's do this! Running command" . "<br/>";

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

SO this works well up to this point: 
Connected
got myDB!
data established
setting timeout
Got the Collection
setting timeout

let's do this! Running command
Array
(
    [errmsg] => exception: couldn't compile code for: _map
    [code] => 13598
    [ok] => 0
)

我在任何地方都找不到有关此特定错误的任何信息。_map 函数无法编译。以前没见过这个。还有没有经验的?它使用 MongoDB 和 PHP 5.3。谢谢!

4

1 回答 1

2

看起来您的代码中有几个语法错误,这就是它无法编译的原因。

在你的map函数中,你有太多的右花括号,并且发出键也不应该是一个文档。

$map = "function() {
    emit( this.dataProviders.0.key, {count: 1});
}";

reduce函数中,如果你只是想把总和相加,你不需要一个数组 in sum。做就是了:

$reduce = "function(key, values) {
    var sum = {count: 0};
    for (var i in values) {
        sum.count += values[i].count;
    }
    return sum;
}";

试试这是否适合你。

于 2012-10-24T04:17:17.690 回答