4

私はpyMongo 1.11とMongoDB 1.8.2を使用しています。かなり複雑な Map/Reduce を実行しようとしています。Mongo で関数のプロトタイプを作成して動作させましたが、Python に転送しようとすると、次のようになります。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Developer/R-and-D/<ipython-input-71-3c3a43221538> in <module>()
----> 1 results = db.user_actions.mapReduce(map, reduce, "user_entities_interactions")

/Library/Python/2.7/site-packages/pymongo/collection.pyc in __call__(self, *args, **kwargs)
   1099                         "call the '%s' method on a 'Collection' object it is "
   1100                         "failing because no such method exists." %
-> 1101                         self.__name.split(".")[-1])

TypeError: 'Collection' object is not callable. If you meant to call the 'mapReduce' method on a 'Collection' object it is failing because no such method exists.

私のコレクションは次のようになります。

{ "_id" : ObjectId("..."), "entity_id" : 1556, "user_id" : 466112 }
{ "_id" : ObjectId("..."), "entity_id" : 1366, "user_id" : 10057 }
{ "_id" : ObjectId("..."), "entity_id" : 234, "user_id" : 43650 }
{ "_id" : ObjectId("..."), "entity_id" : 6, "user_id" : 34430 }
{ "_id" : ObjectId("..."), "entity_id" : 461, "user_id" : 3416 }
{ "_id" : ObjectId("..."), "entity_id" : 994, "user_id" : 10057 }
{ "_id" : ObjectId("..."), "entity_id" : 296, "user_id" : 466112 }

Pythonで実行しているコードは次のとおりです。

map = Code("""function () {
        emit(this.user_id, { 
            user_id : this.user_id,
            entity_id : this.entity_id});
    }""")

reduce = Code("""function (key, values) {
        var entities = { user_id : values[0].user_id, entity_id : [ ] };
        for (var i = 0; i < values.length; i++) {
            entities.entity_id[i] = values[i].entity_id;
        }
        return entities;
    }""")
results = db.user_actions.mapReduce(map, reduce, "user_entities_interactions")

結果次のようになります。

{ "_id" : 3416, "value" : { "user_id" : 3416, "entity_id" : 461 } }
{ "_id" : 10057, "value" : { "user_id" : 10057, "entity_id" : [ 1366, 994 ] } }
{ "_id" : 34430, "value" : { "user_id" : 34430, "entity_id" : 6 } }
{ "_id" : 43650, "value" : { "user_id" : 43650, "entity_id" : 234 } }
{ "_id" : 466112, "value" : { "user_id" : 466112, "entity_id" : [ 1556, 296 ] } }

何が問題なのかはっきりしません。エラーは、「コレクション」オブジェクトに mapReduce メソッドがないことを示していますが、http: //api.mongodb.org/python/current/examples/map_reduce.html の例が機能し、「もの」とは何かコレクションじゃない?

また、なぜ group() でこれを行わないのか疑問に思っている場合は、20000 を超える一意のキーがあるためです。

4

4 に答える 4

6

とは呼ばれていませんmapReduceが、map_reduce。試す:

results = db.user_actions.map_reduce(map, reduce, "user_entities_interactions")
于 2011-08-05T21:51:06.457 に答える
2

問題

すべての回答で述べたように、問題は、最も使用されている Python コード スタイルに適合するように、 MapReduce メソッドpymongoが実際にはアンダースコア、つまりmap_reduceで記述されていることです。

紛らわしいエラー

TypeError: 'Collection' object is not callable. If you meant to call the 'mapReduce' method on a 'Collection' object it is failing because no such method exists.

このエラーは非常に紛らわしく、間違った方向に進む可能性があります。ここでのポイントは、MongoDB が、、などのドット使用する内部/システム コレクション名を使用していることです。したがって、コードを実行すると、実際には単一のコレクション、つまり Collection オブジェクトのインスタンスとして扱われ、存在しないそのオブジェクトでメソッドを実行しようとします。したがって、エラー。system.namespacessystem.indexessystem.profileuser_actions.mapReduceuser_actions.mapReduce__call__

このケースを考慮して、存在しない対応する Collection オブジェクトでメソッドpymongoを実行しようとしていた可能性を示唆しているのが良い点です。mapReduce

于 2015-05-05T22:01:43.833 に答える
0

そのリンクされたページをもう一度読むと、メソッドが呼び出されますmap_reduce

また、その例things コレクションであり、最初のドキュメントを挿入したときに作成されます。

于 2011-08-05T21:50:01.903 に答える