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

Friday, March 22, 2013

Authentication to MongoDb using Java

MongoDB provides basic support for authentication with the auth setting. For multi-instance deployments (i.e. replica sets, and sharded clusters) use the keyFile setting, which implies auth, and allows intra-deployment authentication and operation. Be aware of the following behaviors of MongoDB’s authentication system:

Enable auth settings to MongoDb requires few steps:
Invoke addUser command on mongo shell:

Start the mongod in --auth mode as shown

Check whether auth setting are working

Now test with the java program to authenticate the database in trusted enviornment.
package com.rajkrrsinghblogspot;


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

public class MongoAuth {

public static void main(String[] args) {
try {
MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));

DB database = client.getDB("blog");
boolean auth = database.authenticate("rajkumar", "password".toCharArray());
DBCollection collection = database.getCollection("posts");

   System.out.println("Authentication is Successful");  
   BasicDBObject query = new BasicDBObject().append("author", "rajkumar");
   BasicDBObject projection = new BasicDBObject().append("author", true);
   DBObject doc = collection.findOne(query,projection);
   System.out.println("Authentication is UnSuccessful");  

} catch (UnknownHostException e) {