Skip to content

Instantly share code, notes, and snippets.

@shinofara
Created June 13, 2013 17:45
Show Gist options
  • Save shinofara/5775744 to your computer and use it in GitHub Desktop.
Save shinofara/5775744 to your computer and use it in GitHub Desktop.
Fluentdで集めた、Nginxのログを、MongoDBのMapReduceを使ってアクセス解析入門!! ref: http://qiita.com/items/849fb219593c4505c997
use nginx
db.access.save({status:200});
db.access.save({status:200});
db.access.save({status:200});
db.access.save({status:200});
db.access.save({status:300});
db.access.save({status:400});
db.access.save({test:123});
db.access.find();
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f2"), "status" : 200 }
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f3"), "status" : 200 }
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f4"), "status" : 200 }
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f5"), "status" : 200 }
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f6"), "status" : 300 }
{ "_id" : ObjectId("51b9ffe092e0e471137ad2f7"), "status" : 400 }
{ "_id" : ObjectId("51b9ffe192e0e471137ad2f8"), "test" : 123 }
$ mongo log.js
MongoDB shell version: 2.4.0
connecting to: test
{
"result" : "access.status",
"timeMillis" : 1080,
"counts" : {
"input" : 7,
"emit" : 7,
"reduce" : 1,
"output" : 4
},
"ok" : 1,
}
$ mongo
MongoDB shell version: 2.4.0
connecting to: test
> use nginx
switched to db nginx
> db.access.status.find();
{ "_id" : "200", "value" : { "count" : 4 } }
{ "_id" : "300", "value" : { "count" : 1 } }
{ "_id" : "400", "value" : { "count" : 1 } }
{ "_id" : "undefined", "value" : { "count" : 1 } }
var colName = 'access.status';
var map = function() {
var label = String(this.status);
emit(
label,
{count: 1}
);
}
//簡単に言うと、
//keyには200
//valuesには[{count:1},{count:1},{count:1}]
//と言った感じでデータが渡ってきてカウントアップしてる感じです。
var reduce = function(key, values) {
var result = {count: 0};
values.forEach(function(value) {
result.count += value.count;
});
return result;
}
mongo = new Mongo('localhost');
mydb = mongo.getDB('nginx');
var res = mydb.access.mapReduce(map, reduce, {out: colName});
shellPrint(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment