Search Here

Sunday, December 14, 2014

Get Hands Dirty with NoSQL + Cloud Data Storage - Part2 - Programming in NoSQL

Hello Readers,

Last week, I was talking about NoSQL Cloud Data Storage, MongoDB in the "Get Hands Dirty with NoSQL + Cloud Data Storage" post. (it shows the details about how to create a free 512MB, MongoDB NoSQL database in https://mongolab.com/signup/ also introduce few industry buzz words.)

I hope, you might have signed up in mongolab.com and created a test MongoDB for your expriment.

Database without programmatic interface is like a watching a Rajinikanth movie without Rajinkanth fans croud. What a boaring thing to do. Rajinikanth movies should be always enjoyed with fans croud in a normal big movie theatres (Not alone in home or in HiFi multiplexs) .

MongoDB provides lots of programming language integrations. Technically, the piece of software which helps your application to talk to Database is called " Database Driver".

Here is the list of MongoDB drivers for major programming languages like C, C++, Java, Perl, PHP, Python, etc...

As I have promised in the Part1, In this post, I will show you, How to connect to MongoDB which is hosted in clouds via Perl programming language.

Why Perl?

Hmm. Where do I start?, I started my career with C programming language, I also wrote applications in Java. I got introduced to Perl when I was working in Hewlett-Packard, initially Perl looks weird but when I was learning more and more, I realized its power. I was still working on Java but then I moved to Nokia, Where I got chance to learn Python mainly for PyS60 (Python for Symbian) project.

@work, I designed and developed multiple SW test and Build framework, Currently I am working in NXP Semiconductor, Where I got chance to design and develop a highly secure, centralized, cryptography key management, cryptography operations SOAP web services using Perl + SOAP::Lite + Apache + Gtk2.

So with these limited knowledge, I can say, Perl is super powerful for most of the application development areas.

I chose Perl to show the MongoDB database programming interface but the MongoDB API and programming flow is almost similar for all other programming languages.

Preparation

To get start, we need a PC with Perl + MongoDB driver installation+ MongoDB in the cloud. I am on Fedora 20 Linux PC. Almost all the linux distributions comes with pre installed Perl.

Note: For Windows PC, I highly recommend to install ActiveState perl and use activestate perl's nice feature called PPM to install the Perl Modules. I shall write a separate post on how to install perl modules in Linux and windows with different approaches.

There are multiple perl drivers for MongoDB. Officially  "MongoDB" Perl driver is supported by MongoDB development team. This Perl driver is a wrapper over C MongoDB APIs, So this driver gives good performance and mostly support latest MongoDB APIs.

You can install perl modules using CPAN but you need a local installation of MongoDB and its development package (header files and libs). Since we are talking about cloud, we dont have anything in local machine, So I use the yum tool to install perl-MongoDB driver.

sudo yum install perl-MongoDB

Once the installation is complete, You can check the status like below,


perl -MMongoDB -e ' print $MongoDB::VERSION'

For me, the above command shows,


0.702.2


Ok, Good, Driver is Sane and working.

Now, we need to login to our MongoDB cloud database in https://mongolab.com/ and get few details,

  1. MongoDB database host name and port - Get it in the "Database" page. Ex: ds063140.mongolab.com:63140
  2. MongoDB database name - Get it in the "Database" page. Ex: npointsolutionsmdb
  3. MongoDB collection name - Get it in the "Database" page. Ex: userDetailsCollection 
  4. MongoDB user name and password - Get it in the "Users" page. Ex: username=user1, password=user123
Here is my fully working Perl code to show how to connect to MongoDB running in the google cloud. I name this perl script as a mongoDB_test.pl


#!perl
use strict;
use warnings;
use Try::Tiny;
use MongoDB;
use Data::Dumper;

#Global Variables

$|=1; #Auto flush STDOUT

my $dbHost = 'ds063140.mongolab.com:63140';
my $dbUser = 'user1';
my $dbPassword = 'user123';
my $dbName = 'npointsolutionsmdb';
my $dbCollectionName = 'userDetailsCollection';
my $dbClientObj = '';
my $dbObj = '';
my $userDetailsCollectionObj = '';
my $dbCursor = '';
my $userDetailsRecordHashRef = '';

local $,= ' , '; #Make array prints nicer
my $now = '';

#1. Connect to MongoDB 
try
{
 $dbClientObj = MongoDB::MongoClient->new(host=>$dbHost,username=>$dbUser,password=>$dbPassword,
                                          db_name=>$dbName,query_timeout => 10000);
 print "\n INFO: MongoDB in $dbHost is connected.";
}
catch
{
 die 'ERROR: Can not create MongoDB client Object: ', $_;
};

#2. Get the Mongo DB object
$dbObj = $dbClientObj->get_database($dbName);

#3. Get all Collection Names
print "\n All Collections: ", $dbObj->collection_names();

#4. Get the Collection Object
$userDetailsCollectionObj = $dbObj->get_collection($dbCollectionName);

#5. Get all the documents / records from the collection
$dbCursor = $userDetailsCollectionObj->find();
$dbCursor->immortal(1);

#Loop through all the user documents
print "\n INFO: All documents in the Collection $dbCollectionName \n";
while ($userDetailsRecordHashRef = $dbCursor->next()) 
{
 #MongoDB Cursor retrives all the fields in the Document as a name-value pair hash reference
    print "\n", Dumper (\$userDetailsRecordHashRef);
}

#6. Adding a new user document into collection
$now = scalar localtime();
try
{
    print "\n INSERT ID: ", $userDetailsCollectionObj->insert(
                             { 
                              userName => 'testPerlUser'.int(rand(100)), 
                              OS => 'Fedora 20 Linux',
                              LastLogin => $now,
                              DeviceArch => "x86_64",
                              Score => int(rand(500))
                             }, {safe => 1});
}
catch
{
 print "\n ERROR: Can not insert a record into Collection. $_";
};

#7. Query our document is in collection or not
print "\n INFO: Find all perl users";
$dbCursor = $userDetailsCollectionObj->find({userName => qr/^testPerlUser/i}); #All the userName starts with testPerlUser
$dbCursor->immortal(1);

#Loop through all the user documents
while ($userDetailsRecordHashRef = $dbCursor->next()) 
{
    #MongoDB Cursor retrives all the fields in the Document as a name-value pair hash reference
    print "\n", Dumper (\$userDetailsRecordHashRef);
}

#8. All is well, CLose connection
undef $dbClientObj;
print "\n INFO: Done. Quit";
exit (0);


Here is the output from my PC when I ran the above code,


 INFO: MongoDB in ds063140.mongolab.com:63140 is connected.
 All Collections:  , system.indexes , userDetailsCollection , objectlabs-system , objectlabs-system.admin.collections
 INFO: All documents in the Collection userDetailsCollection 

 , $VAR1 = \{
            '_id' => bless( {
                            'value' => '54846970e4b0d2e1a43f8654'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Dec 07, 2014',
            'DeviceArch' => 'x86_64',
            'Score' => 103,
            'OS' => 'Linux Fedora 20',
            'userName' => 'test1'
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            'LastLogin' => 'Dec 06, 2014',
            '_id' => bless( {
                            'value' => '548469bee4b0d2e1a43f8656'
                          }, 'MongoDB::OID' ),
            'userName' => 'test2',
            'OS' => 'Windows 7',
            'Score' => 54
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            'LastLogin' => 'Sun Dec 14 20:58:56 2014',
            '_id' => bless( {
                            'value' => '548dacb85c3cc20b44000000'
                          }, 'MongoDB::OID' ),
            'OS' => 'Fedora 20 Linux',
            'Score' => 480,
            'userName' => 'testPerlUser55'
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            'LastLogin' => 'Sun Dec 14 21:06:45 2014',
            '_id' => bless( {
                            'value' => '548dae8d98e64d8c45000000'
                          }, 'MongoDB::OID' ),
            'userName' => 'testPerlUser41',
            'OS' => 'Fedora 20 Linux',
            'Score' => 424
          };

 , $VAR1 = \{
            'OS' => 'Fedora 20 Linux',
            'Score' => 108,
            'userName' => 'testPerlUser67',
            'DeviceArch' => 'x86_64',
            'LastLogin' => 'Sun Dec 14 21:07:00 2014',
            '_id' => bless( {
                            'value' => '548dae9c2d09dc9845000000'
                          }, 'MongoDB::OID' )
          };

 , $VAR1 = \{
            '_id' => bless( {
                            'value' => '548db07a0f9cd12946000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 21:14:58 2014',
            'DeviceArch' => 'x86_64',
            'Score' => 316,
            'OS' => 'Fedora 20 Linux',
            'userName' => 'testPerlUser57'
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            'LastLogin' => 'Sun Dec 14 21:15:30 2014',
            '_id' => bless( {
                            'value' => '548db09ae3528e9546000000'
                          }, 'MongoDB::OID' ),
            'OS' => 'Fedora 20 Linux',
            'Score' => 211,
            'userName' => 'testPerlUser57'
          };

 INSERT ID:  , 548db2c5cb53ab1b47000000
 INFO: Find all perl users
 , $VAR1 = \{
            '_id' => bless( {
                            'value' => '548dae8d98e64d8c45000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 21:06:45 2014',
            'DeviceArch' => 'x86_64',
            'userName' => 'testPerlUser41',
            'Score' => 424,
            'OS' => 'Fedora 20 Linux'
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            '_id' => bless( {
                            'value' => '548dacb85c3cc20b44000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 20:58:56 2014',
            'userName' => 'testPerlUser55',
            'OS' => 'Fedora 20 Linux',
            'Score' => 480
          };

 , $VAR1 = \{
            'userName' => 'testPerlUser57',
            'Score' => 211,
            'OS' => 'Fedora 20 Linux',
            '_id' => bless( {
                            'value' => '548db09ae3528e9546000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 21:15:30 2014',
            'DeviceArch' => 'x86_64'
          };

 , $VAR1 = \{
            'userName' => 'testPerlUser57',
            'Score' => 316,
            'OS' => 'Fedora 20 Linux',
            'LastLogin' => 'Sun Dec 14 21:14:58 2014',
            '_id' => bless( {
                            'value' => '548db07a0f9cd12946000000'
                          }, 'MongoDB::OID' ),
            'DeviceArch' => 'x86_64'
          };

 , $VAR1 = \{
            'DeviceArch' => 'x86_64',
            '_id' => bless( {
                            'value' => '548dae9c2d09dc9845000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 21:07:00 2014',
            'userName' => 'testPerlUser67',
            'OS' => 'Fedora 20 Linux',
            'Score' => 108
          };

 , $VAR1 = \{
            'Score' => 191,
            'OS' => 'Fedora 20 Linux',
            'userName' => 'testPerlUser75',
            '_id' => bless( {
                            'value' => '548db2c5cb53ab1b47000000'
                          }, 'MongoDB::OID' ),
            'LastLogin' => 'Sun Dec 14 21:24:45 2014',
            'DeviceArch' => 'x86_64'
          };

 INFO: Done. Quit

Assuming that you know basic of perl, I hope the mongoDB_test.pl code + comments is self explanatory.

Note: I have shown my database host, user name and password. So you can just copy paste and run the code. Please Do not abuse the credentials.

Let me know via comments if you have any questions.

Refer:

1. https://metacpan.org/pod/distribution/MongoDB/lib/MongoDB/Tutorial.pod

2. For more query examples -> https://metacpan.org/pod/distribution/MongoDB/lib/MongoDB/Examples.pod
 
Christmas is very near, Get ready for the festival and holidays... Merry Christmas... 

No comments:

Post a Comment