Skip to content

Instantly share code, notes, and snippets.

@nuna
Created December 22, 2010 01:42
Show Gist options
  • Save nuna/750941 to your computer and use it in GitHub Desktop.
Save nuna/750941 to your computer and use it in GitHub Desktop.
Calculate bytes of the traffic of each domain using mongodb map/reduce.
#!/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