Created
December 22, 2010 01:42
-
-
Save nuna/750941 to your computer and use it in GitHub Desktop.
Calculate bytes of the traffic of each domain using mongodb map/reduce.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby19 | |
# -*- coding: utf-8 -*- | |
require 'mongo' | |
connection = Mongo::Connection.new | |
db = connection.db('apache-log') | |
logs = db.collection('log') | |
map =<<'_FUNC_' | |
function() { | |
var host = this.uri.match(/^http:\/\/([^\/]+)\/.*/)[1]; | |
emit(host, this.bytes); | |
} | |
_FUNC_ | |
reduce =<<_FUNC_ | |
function(key, values) { | |
var total = 0; | |
values.forEach(function(e) { | |
total += e; | |
}); | |
return total; | |
} | |
_FUNC_ | |
result = logs.map_reduce(map, reduce, limit: 100) | |
result.find().sort('value', -1).each do |i| | |
puts i.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment