Sunday, March 24, 2013

Importing the tweets from Twitter API to MongoDB

Importing the Tweets from twitter using the twitter api is very simple and straight forward.In the coming example I am importing the Sachin Tendulkar(Legendary Cricketer ) tweets to the MongoDb.Here you can find the URL of json document of Sachin tweets :

Java Program to import the tweets and save it the MongoDb.

package com.rajkrrsinghblogspot;

import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.util.JSON;

public class GetTweets {
 public static void main(String[] args) throws IOException {
  try {
   MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
   DB database = client.getDB("tweetDb");
   DBCollection collection = database.getCollection("tweetCollection");
   List<DBObject> tweets = getSachinTweets();
   for(DBObject doc : tweets){
   System.out.println("Import done");
  } catch (UnknownHostException e) {

private static List<DBObject> getSachinTweets() throws IOException {
 URL url = new URL("");
 InputStream is = url.openStream();
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
 int endOfStream;
 while((endOfStream = != -1){
 String tweetString = bos.toString();
 return (List<DBObject>) JSON.parse(tweetString);


Run the Program as Java Application but make sure that you have started the mongod (mongod --dbpath) before running the program,look out for the message import done on the console.
Now check the mongo shell