MongoDb Java Driver


Its very easy to integrate the MongoDB with a java driver …
Download Java Driver
Once downloaded the connection to the mongodb is made using configServer() and other methods are self explanatory.

public class MongoDB {

	private String hostname ="localhost";
	private int port = 27017;
	private String dbStore = "myFirstApplication";
	private DB database;
	private DBCollection collection;
	public MongoDB(){
		
		configServer();
	}

	/**
	 * doing the configuration. By default the collection is set to testing.
	 */
	private void configServer() {
		try {
			Mongo mongo = new Mongo(hostname,port);
			 database = mongo.getDB(dbStore);
			 collection = database.getCollection("testing");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Insert the json object to DB
	 * @param jsonString
	 */
	public void insert(String jsonString){
		DBObject dbobject = (DBObject) JSON.parse(jsonString);
		collection.insert(dbobject);
	}
	
	/**
	 * find the json object using the key.. Not very efficient though
	 * @param key
	 * @param value
	 * @return
	 */
	public DBObject find(String key,String value){
		BasicDBObject query = new BasicDBObject(key,value);
		DBObject dbObject = collection.findOne(query);
		return dbObject;
	}
	
	
	/**
	 * find an object with the unique object id.
	 *  This throws IllegalArgumentException if the object id doesnt exist 
	 * @param objectId
	 * @return
	 */
	
	public DBObject find(ObjectId objectId) {
		BasicDBObject query = new BasicDBObject("_id",objectId);
		return collection.findOne(query);
	}
	/**
	 * delete the json on the basis of key
	 * @param key
	 * @param value
	 */
	public void delete (String key,String value){
		BasicDBObject query = new BasicDBObject(key,value);
		collection.remove(query);
		
	}
	
	/**
	 * delete by the uniqied id given by mongo
	 * @param objectId
	 */
	public void deleteById(ObjectId objectId){
		BasicDBObject query = new BasicDBObject("_id",objectId);
		collection.remove(query);
	}
	/*
	 * update the value of key in json string. First the object is found using "_id"
	 */
	public boolean update(String key,String value,ObjectId objectId){
		BasicDBObject query = new BasicDBObject("_id",objectId);
		DBObject dbObject = collection.findOne(query);
		if(dbObject != null){
			DBObject newObject = new BasicDBObject(key, value);
			DBObject oldObject = (DBObject) dbObject.get(key);
			if(oldObject != null){
				collection.update(dbObject,new BasicDBObject("$set",new BasicDBObject(key,value)));
				return true;
			}
		}
		return false;
	}
}

Rails 3 + MongDB , MongoMapper beginner tutorial


I recently used MongoDB at my work for mobile app and was tempted to try more. So i wrote a simple app in rails to play with it..

Lets assume rails is already installed.

References for tutorial:
MongoDb installation
MongoDB with Rails
Railcast of integrating rails with Mongo

The installation of MongoDB can be done using the first reference.
Lets create a rails project now to record daily expenses.

rails new expense_record --skip-active-record

We need to update the gemfile by adding mongo dependencies:

require 'rubygems'
require 'mongo'

gem 'rails', '3.0.10'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
source 'http://gemcutter.org'

gem "mongo_mapper"
gem "bson_ext", "1.3.1"
gem "bson" ,"1.3.1"
gem  "mongo","1.3.1"

Now run

bundle install

Create a new filemongo_config.rb in config/initializers folder.
Lets add the following code :

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "#expense-#{Rails.env}"

if defined?(PhusionPassenger)
   PhusionPassenger.on_event(:starting_worker_process) do |forked|
     MongoMapper.connection.connect if forked
   end
end

NOTE: here the #expense is the name of the database. By default Mongo listens to 27017 port. Also the database name should not contain “.”, such as “expense1.0” is wrong.

Now lets create an expense model with field “expense_name” “date_spent” “amount”.

rails g scaffold expense expense_name:string date_spent:string amount:integer --skip-migration --orm mongo_mapper

All the classes are created as expected except the model class expense

class Expense
  include MongoMapper::Document

  key :expense_name, String
  key :date_spent, String
  key :amount, Integer

end

Instead of ActiveRecord , MongoMapper is used which behaves pretty much the same.

Now we are good to run the project.

rails s 
<a href="http://localhost:3000/expenses"></a>

Happy coding..

Problem while deleting few ruby gems.


It may happen that at time while deleting the ruby gem the error can be :

ERROR: While executing gem ... (Gem::InstallError)
Unknown gem rake >= 0

This generally happens when there are multiple gem paths.

type ‘gem env’

RubyGems Environment:
– RUBYGEMS VERSION: 1.6.2
– RUBY VERSION: 1.9.2 (2011-07-09 patchlevel 290) [i386-darwin10.7.1]
– INSTALLATION DIRECTORY: /Users/hrastogi/.rvm/gems/ruby-1.9.2-p290
– RUBY EXECUTABLE: /Users/hrastogi/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
– EXECUTABLE DIRECTORY: /Users/hrastogi/.rvm/gems/ruby-1.9.2-p290/bin
– RUBYGEMS PLATFORMS:
– ruby
– x86-darwin-10
– GEM PATHS:
– /Users/xxxx/.rvm/gems/ruby-1.9.2-p290
– /Users/xxxx/.rvm/gems/ruby-1.9.2-p290@global
– GEM CONFIGURATION:
– :update_sources =&gt; true
– :verbose =&gt; true
– :benchmark =&gt; false
– :backtrace =&gt; false
– :bulk_threshold =&gt; 1000
– REMOTE SOURCES:
http://rubygems.org/

Gem is installed in multiple paths. Try deleting manually from one of the path

gem uninstall rake -i /Users/xxxx/.rvm/gems/ruby-1.9.2-p290@global

Extract HTML content using Xpath


Xpath is widely used to scrap the desired contents from a html page.

Here i am making an attempt to read the HTML page and extract information. For this i am going to use Python and YQL .

YQL is Yahoo Query Language ” an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, apps run faster with fewer lines of code and a smaller network footprint”

For E.g.

A query like this :

select * from html where url=”http://en.wikipedia.org/wiki/David_Guetta

will return the whole HTML page as a <results> attribute in the XML, but we are interested only in extracting useful data so we can append xpath to the query :

Consider I just have to extract the Background information of the Dj player which is present in the box on the right side .

If  you view the source that info is in the <table> tag with class=”infobox vcard” . One can append the XPath to YQL as follows:

select * from html where url=”http://en.wikipedia.org/wiki/David_Guetta&#8221; and xpath=\’//table[@class=”infobox vcard”]’

This query will return an xml in which the <results> tag will contains the details from the table. Now one could easily parse the remaining data to use it.

But I am not interested in all <tr> tags present in the table, i am only interested in <tr> tags with class=””. I can put more filter to the query

select * from html where url=”http://en.wikipedia.org/wiki/David_Guetta&#8221; and xpath=’//table[@class=”infobox vcard”]/tr[@class=””]’

Now i will get details something like:

 <tr class="">
            <th scope="row" style="text-align:left;">
                <p>Birth name</p>
            </th>
            <td class="nickname" style="">
                <p>David Pierre Guetta</p>
            </td>
        </tr>
        <tr class="">
            <th scope="row" style="text-align:left;">
                <p>Born</p>
            </th>
            <td class="" style="">
                <p>7 November 1967 <span style="display:none">(<span class="bday">1967-11-07</span>)</span>
                    <span class="noprint">(age&nbsp;43)</span>
                    <br/>
Paris, France</p>
            </td>
        </tr>

I tried this using python . If python is already installed in your computer , then installing YQL is simple :

1) If easy_install is present , just do

easy_install yql for windows
sudo easy_install yql for linux systems

2) Can download the tarball :
wget http://pypi.python.org/packages/source/y/yql/yql-0.2.tar.gz
tar -xzf yql-0.2.tar.gz
cd yql-0.2
python steup.py install

The YQL guide More Details aobut YQL

My sample program is :

import yql
if __name__ == ‘__main__’:
y = yql.Public()
query = ‘select * from html where url=”http://en.wikipedia.org/wiki/David_Guetta&#8221; and xpath=\’//table[@class=”infobox vcard”]/tr[@class=””]\”;
result = y.execute(query)
print result.rows

— Done —