mongo-mapreduceテスト(10)——段階総括(2)

8241 ワード


mongo-mapreduceテスト(1)-count/sum/where条件
 
mongo-mapreduceテスト(2)-列転行とfinalize関数
 
mongo-mapreduceテスト(3)-group by having
 
mongo-mapreduceテスト(4)-avg
 
mongo-mapreduceテスト(5)-max/min
 
mongo-mapreduceテスト(6)-総合テスト
 
mongo-mapreduceテスト(7)-jsストレージ・プロシージャの使用
 
mongo-mapreduceテスト(8)——段階総括(1)
 
mongo-mapreduceテスト(9)-python呼び出し
 
mongo-mapreduceテスト(10)——段階総括(2)
 
mongo-mapreduceテスト(11)-トレースデバッグ
MRの出力結果セットのオプションは次のとおりです.
out: { <action>: <collectionName>

         [, db: <dbName>]

         [, sharded: <boolean> ]

         [, nonAtomic: <boolean> ] }

1.actionでは、4つのオプションが使用できます(デフォルトはreplace)
{ replace : "collectionName"} - the output will be inserted into a collection which will atomically replace any existing collection with the same name.{ merge : "collectionName"} - This option will merge new data into the old output collection. In other words, if the same key exists in both the result set and the old collection, the new key will overwrite the old one.{ reduce : "collectionName"} - If documents exists for a given key in the result set and in the old collection, then a reduce operation (using the specified reduce function) will be performed on the two values and the result will be written to the output collection. If a finalize function was provided, this will be run after the reduce as well.{ inline : 1} - With this option, no collection will be created, and the whole map-reduce operation will happen in RAM. Also, the results of the map-reduce will be returned within the result object. Note that this option is possible only when the result set fits within the 16MB limit of a single document.デフォルトでは、MRは毎回結果テーブルを空にしてからテーブルにデータを挿入します(count機能を使用してテストします).
> var map = function() {... emit(this.name, 1);... };> var reduce = function(key, values) {... return values.length;... };1.1. replace:MRを実行し、デフォルトではreplaceパラメータを使用して結果セットを削除してから再構築>db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});{"result": "tianyc_test3_result2","timeMillis": 197,"counts": {"input": 11,"emit": 11,"reduce": 3,"output": 3},"ok": 1,}> db.tianyc_test3_result2.find(){“_id”:“neu”,“value”:4}{“_id”:“xtt”,“value”:5}{“_id”:“yct”,“value”:2}1.2 merge:テストデータの修正.MRを実行し、mergeパラメータを使用して新しい結果を結果セットに更新します.>db.tianyc_test3.insert({name:'neu',dic:10})> db.tianyc_test3.insert({name:'xjbu',dic:1})> db.tianyc_test3.insert({name:'xjbu',dic:2})> db.tianyc_test3.remove({_id:'yct'})> db.tianyc_test3.mapReduce(map, reduce, {out: {merge:"tianyc_test3_result2"}});{"result": "tianyc_test3_result2","timeMillis": 9,"counts": {"input": 14,"emit": 14,"reduce": 4,"output": 4},"ok": 1,}> db.tianyc_test3_result2.find(){“_id”:“neu”,“value”:5}{“_id”:“xtt”,“value”:5}{“_id”:“yct”,“value”:2}{“_id”:“xjbu”,“value”:2}>1.3 reduce:MRを実行し、reduceパラメータを使用して、新しい結果をtianyc_test3_result 2の既存のセットを再度reduceし、最終結果を結果セットに更新します.>db.tianyc_test3.mapReduce(map, reduce, {out: {reduce:"tianyc_test3_result2"}});{"result": "tianyc_test3_result2","timeMillis": 245,"counts": {"input": 14,"emit": 14,"reduce": 4,"output": 4},"ok": 1,}> db.tianyc_test3_result2.find(){“_id”:“neu”,“value”:2}{“_id”:“xtt”,“value”:2}{“_id”:“yct”,“value”:2}{“_id”:“xjbu”,“value”:2}1.4 inline:MRを実行し、{inline:1}パラメータを使用してMR結果を画面に直接出力します.>db.tianyc_test3.mapReduce(map, reduce, {out: {inline : 1}});{"results": [{"_id": "neu","value": 5},{"_id": "xjbu","value": 2},{"_id": "xtt","value": 5},{"_id": "yct","value": 2}],"timeMillis": 21,"counts": {"input": 14,"emit": 14,"reduce": 4,"output": 4},"ok": 1,}>
2.dbの場合
指定したdbにMR結果セットを出力できます.デフォルトは現在のdbです.このパラメータの利点は,MR結果をすべて1つの専用データベースに格納できることである.次は小さなテストです.
#GpsデータベースでMRを実行し、結果セットを指定してtestライブラリに出力します.
> use Gpsswitched to db Gps> var map = function() {... emit(this.name, {cnt: 1});... };> var reduce = function(key, values) {... var count = 0;... for (var i=0;i db.tianyc_test3.mapReduce(map, reduce, {out: {replace:"tianyc_test3_result",db:'test'}});{ "result": { "db": "test", "collection": "tianyc_test3_result"}, "counts": { "input": NumberLong(14), "emit": NumberLong(14), "reduce": NumberLong(4), "output": NumberLong(4) }, "timeMillis": 458, "timing": { "shardProcessing": 12, "postProcessing": 445 }, "shardCounts": { "seta/10.x.x.1:1111,10.x.x.2:2222": { "input": 14, "emit": 14, "reduce": 4, "output": 4 } }, "postProcessCounts": { "setb/10.x.x.3:3333,10.x.x.4:4444": { "input": NumberLong(4), "reduce": NumberLong(0), "output": NumberLong(4) } }, "ok": 1,}> use testswitched to db test> show collectionssystem.indexestianyc_test3_result> db.tianyc_test3_result.find(){ "_id": "neu", "value": 5 }{ "_id": "xjbu", "value": 2 }{ "_id": "xtt", "value": 5 }{ "_id": "yct", "value": 2 }
参照mongodb公式サイト