7. Get Tweets
from pymongo.connection import Connection
import sys
import tweepy
connection = Connection("localhost")
db = connection.foo
import tweepy
api = tweepy.API()
tweets = api.search('#JustinBieber', rpp=100)
for tweet in tweets:
db.foo.save(tweet.__getstate__())
8. Insert TO MongoDB
from pymongo.connection import Connection
import sys
import tweepy
connection = Connection("localhost")
db = connection.foo
import tweepy
api = tweepy.API()
for num in range(1,16):
tweets = api.search('#JustinBieber', rpp=100, page=num)
for tweet in tweets:
db.foo.save(tweet.__getstate__())
9. Count Frequency in mongo
MAP
map = function(){
words = this.text.split(' ');
for ( i in words ){
emit({ key: words[i] }, {count: 1});
}
};
10. Count Frequency in mongo
REDUCE
reduce = function (key, values) {
var count = 0;
values.forEach(function (v) {count += v.count;});
return {count:count};
}
11. Count Frequency in mongo
EXECUTE
res = db.foo.mapReduce( map, reduce, {out: "mystring"});
13. Get From MongoDB
from pymongo.connection import Connection
import sys
import tweepy
connection = Connection("localhost")
db = connection.foo
cursor = db.mystring.find()
for d in cursor:
print d