Quick Start To Using MongoDB with Python on Linux

With it’s rapid growth in popularity MongoDB is quickly becoming one of the top NoSQL Databases out there and with Python being one of the top ten programming languages according to Tiobe Software’s Programming Community Index. I’ve decided to write a quick how-to to show you just how easy it is to get started with MongoDB and Python.

 

Assuming that you all ready have Python installed on your system. We’ll start with downloading and installing MongoDB. The first that you will need to do is download the appropriate package from http://www.mongodb.org/downloads to your /tmp directory.

 

$ curl http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.6.3.tgz > /tmp/mongo.tgz

 

Once you have downloaded the correct package, find a suitable directory to unpack it and move it to, such as /opt/mongodb.

 

$ cd /tmp

$ tar -zxf mongo.tgz

$ sudo mv mongodb-linux-i686-1.6.3 /opt/mongodb

 

Now you will need to create the data directory. By default, MongoDB stores it’s data in “/data/db”, but if for some reason you need to change the location of the data directory you will need to use the “–dbpath” option when starting the server. However I am sticking with the default directory.

 

$ sudo mkdir -p /data/db

$ sudo chown -R owerid /data/db

 

You can start MongoDB with the following command.

 

$ /opt/mongodb/bin/mongod

 

Now, test it out by using the MongoDB shell to connect to the server as follows.

 

$ /opt/mongodb/bin/mongo

MongoDB shell version: 1.6.3

connecting to: test

> db.foo.save( { Message : “Hello World” } )

> db.foo.find()

{ “_id” : ObjectId(“4cdd92bc4f6fb75dd2a7642d”), “Message” : “Hello World” }

>

 

Once you have verified that MongoDB is working you will need to download the Python driver for it which are called “PyMongo”. If you have the Python “setuptools” installed you should be able to do “easy_install pymongo” to install the drivers. Otherwise you can download the project source from http://pypi.python.org/pypi/pymongo/ to install the MongoDB drivers.

 

$ curl http://pypi.python.org/packages/source/p/pymongo/pymongo-1.9.tar.gz  > /tmp/pymongo-1.9.tar.gz

$ tar -zxf pymongo-1.9.tar.gz

$ cd pymongo-1.9/

$ python setup.py install

 

After everything is installed it’s time to test it all out, so just pop open your favor Python IDE or the Python Console and give it a go. Here is a simple example.

 

$ python

Python 2.4.3 (#1, Nov 11 2010, 13:34:43)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import pymongo

>>> connection = pymongo.Connection( “localhost”, 27017 )

>>> db = connection.test

>>> db.foo.save({ “Message” : “Hello World 2” })

ObjectId(‘4cdd95bfe1382330b5000000′)

>>> for message in db.foo.find():

… print message

{u’Message’: u’Hello World’, u’_id’: ObjectId(‘4cdd92bc4f6fb75dd2a7642d’)}

{u’Message’: u’Hello World 2′, u’_id’: ObjectId(‘4cdd95bfe1382330b5000000’)}

>>>

$

 

For more information on the PyMongo check out the website athttp://api.mongodb.org/python/1.9%2B/index.html.

 

Connecting Python to Oracle.

In the current world of heterogeneous networks it seems that you need to be able to connect to ever type of database system out there. In a recent project, I was asked, to take an existing MySQL and Python system and get it to connect to an existing Oracle Data Warehouse system. To be honest, I hadn’t actually ever connected Python to an Oracle Database before and from experience this would be either very painful or pretty straight forward.

Of course like everything Python it was pretty straight forward. The first thing you will need to get is the correct cx_Oracle module from http://cx-oracle.sourceforge.net/. The cx_Oracle module allows you to connect to Oracle databases and it conforms to the Python database API specification. Which makes life easier for everyone. After you have installed the module the rest is just a matter having the right permission to connect to the Oracle Database and writing the code.

Here is quick script showing you how to connect to Oracle with Python.

#!/usr/bin/python

import cx_Oracle

connstr=’scott/tiger’
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()
curs.arraysize=50
curs.execute(‘select 2+2 “aaa” ,3*3 from dual’)
print curs.description
print curs.fetchone()
conn.close()

For more information check out the Python Programming Language – Official Website

Why You Should Looking at Python.

Python is an interpreted, interactive, object-oriented programming language. Python includes modules, classes, exceptions, very high level dynamic data types and dynamic typing. Python has also been one of the top 10 programming languages, since May of 2004 according toTiobe Software’s Programming Community Index. Python is being used by some big named companies such as Google, Yahoo, Walt Disney, and Industrial Light & Magic to name a few. Above all else the learning curve is simple.

Here is a quick script that show you how easy it is to connect to a MySQL Database

#!/usr/bin/python
# Import the MySQL Module
import MySQLdb
#Create the database connection
conn = MySQLdb.connect (host = “localhost”, user = “rhosto”, passwd = “biteme”, db = “test”)
#create and execute the sql query
cursor = conn.cursor()
cursor.execute (“SELECT VERSION()”)
#get and print out the results
row = cursor.fetchone ()
print “server version:”, row[0]
#close you database connection
cursor.close ()
conn.close ()

Slacking off

Well I haven’t really wrote anything in the last couple of months even though, I have had plenty to write about. I went to OSCON 2010 in July and had a great time. I learned more about MongoDB and picked up a few more bits of information on MySQL, Memcached, and Linux.

In July I went to Novell’s Linux Days. I have to admit that Novell has some nice products. Not sure if I would personally pay for them, but if the company wanted to, I wouldn’t turn my nose up at them.

Building Thrift on RedHat/CentOS 5.x

What’s Thrift you ask? Well to quote their website.

Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

 

And why do I needed it? Well I have been testing Cassandra for at work and one of the developers want to generate some csharp code using the Thrift API, so of course he wanted me to do it, since he was having problems getting it to work on his Window box.

 

So here is the short notes for what I did.

 

Downloaded Thrift code:
thrift-incubating-0.2.0.tar.gz

Installed/Updated required packages:
sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel

Unpacked the code, compiled, and installed it:
tar -zxf thrift-incubating-0.2.0.tar.gz
cd thrift-incubating-0.2.0
./configure
make
sudo make install

That’s it, have fun. Next project going to look at Lucandra: A Cassandra-based Lucene backend.