Skip to content
Permalink
Browse files

Count tweets per user using map-reduce

  • Loading branch information
gregmoreno committed Sep 4, 2012
1 parent 7d6e7ba commit ccc3c0b85f9023bdb04986b825b1932a815e35f2
Showing with 54 additions and 6 deletions.
  1. +1 −4 Gemfile
  2. +2 −2 collect.rb
  3. +10 −0 counter.rb
  4. +37 −0 status_counter.rb
  5. +4 −0 tweetminer.rb
@@ -3,7 +3,4 @@ source "http://rubygems.org"
gem "tweetstream"
gem "mongo"
gem "bson_ext"

group :development do
gem "awesome_print"
end
gem "awesome_print", :require => "ap"
@@ -1,10 +1,10 @@
# Collects user tweets and saves them to a mongodb

require "bundler"
require File.dirname(__FILE__) + "/tweetminer"

Bundler.require

require File.dirname(__FILE__) + "/tweetminer"

# We use the TweetStream gem to access Twitter's Streaming API.
# https://github.com/intridea/tweetstream

@@ -0,0 +1,10 @@
require "bundler"
Bundler.require

require File.dirname(__FILE__) + "/tweetminer"

settings = YAML.load_file File.dirname(__FILE__) + '/mongo.yml'
miner = TweetMiner.new(settings)

results = miner.status_count_by_user
ap results
@@ -0,0 +1,37 @@
module StatusCounter

class UserCounter
def map_command
<<-EOS
function() {
emit(this.user.id_str, 1);
}
EOS
end

def reduce_command
<<-EOS
function(key, values) {
var count = 0;
for(i in values) {
count += values[i]
}
return count;
}
EOS
end

end

def status_count_by_user
counter = UserCounter.new
statuses.map_reduce(counter.map_command, counter.reduce_command, default_mr_options)
end

def default_mr_options
{:out => {:inline => 1}, :raw => true }
end

end
@@ -1,6 +1,10 @@
require "mongo"

require File.dirname(__FILE__) + "/status_counter"

class TweetMiner
include StatusCounter

attr_writer :db_connector
attr_reader :options

0 comments on commit ccc3c0b

Please sign in to comment.
You can’t perform that action at this time.