Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Monday, October 28, 2013

OCJP 7 book review



Author's Names: S G Ganesh & Tushar Sharma

Type of Book/Genre: OCPJP 7 Certification

Description/Brief Summary: This book is for OCPJP 7 certification.

Review:
If you are studying for OCPJP 7 examination, then I find this book is very useful. You could be a professional who wants to brush-up on Java APIs, you could be a trainer for Core Java, or you could be someone who just want to pass the exam. This book is very well-organized and it covers all the topics for OCPJP 7 in required depth.

The first chapter contains OCPJP 7 examination FAQs that provides OCPJP 7 certification overview, kinds of questions that come in this certification examination, details about how to prepare for it and the most interesting things as well as very useful information for readers who is going to give this kind of certification examination for the first time, that is how to register, what to do on the examination day, etc.

The second chapter contains a pre-test; this chapter will be useful to check your current-level of knowledge and find your weakness before you start. A new learner can skip this chapter as this chapter required beginners/intermediate knowledge in Java.

Rest of the twelve chapters covers all the topics required for OCPJP 7 with examples to explain the APIs and tricks, this chapters end with a test named "Question Time!" to check reader's learning and most important thing there is a summary section at the end of each chapter which is a great help for quick recap. Apart from this there is a "Points to Remember" section to which highlight the tricky parts covered in that section.

At the end there are two OCPJP 7 mock exams which will help reader for final preparation and check actual preparedness for the examination.

I really enjoyed reading it. I recommend this book for OCPJP 7 certification and as a Core Java book.

Monday, July 1, 2013

NoSQL Overview

Today's internet age data nature, size and query response changes dynamically due to huge user load resulting into data start growing unmanageably.
Wal-Mart: 1 million transactions per hour
Twitter: 250 million tweets a day
Facebook: 1 billion messages a day


RDMS are not design to handle this volume of data since they are generally design to scale on single server, so user need to buy a bigger machine to meet this requirement and it will add huge input cost.  


Now this data is accessed from mobile devices to desktop. (Data is distributed & urgent)
$300 billion potential annual value to US health care. (Data is valued)


To meet the need of  scalability, performance and consistency required in this web era NoSQL databases are schema free, easy replication support, sharding, instead of ACID**  it follow BASE**, following CAP** theorem etc.


** ACID is Atomicity, Consistency, Isolation, Durability. BASE is Basically Available, Soft State, and Eventually Consistent. CAP is Consistency , Availability , Partition tolerance


Schema free: There is no predefined schema for NoSQL databases.


Easy Replication Support: NoSQL databases employ asynchronous replication, which allows write to complete more quickly since they don’t depend on extra network traffic.


Horizontal Scaling: NoSQL databases split tables across different server, but with only instance of the same data.


Sharding: NoSQL can perform sharding. In which records (rows) are distributed across many database server.


BASE instead of ACID: NoSQL databases emphasis on performance and availability.
Basic Availability: NoSQL use replication to reduce the likelihood of data unavailability and use sharding, or partitioning the among many different storage server, to make any remaining failure partial. The result is a system that is always available, even if subsets of the data become unavailable for short period of time.
Soft State: NoSQL systems allow data to be inconsistent and relegate designing around such inconsistencies to application developers.
Eventual Consistency: Although applications must deal with instantaneous consistency, NoSQL systems ensure that at some future point in time the data assumes a consistent state. In NoSQL it guarantees consistency only at some un-define future time.


Here is one important theorem of distributed computer system CAP theorem or Brewer’s theorem, which is followed by NoSQL databases.
Consistency: All database clients see the same data, even with concurrent updates.
Availability: All database clients are able to access some version of the data.
Partition Tolerance: The database can be split over multiple servers & at any moment data must be available even if any node(s) / communication link(s) fails.


NoSQL database come in many form to meet application requirement like key-value store, column family store, document store and graph store are the major forms.


Key-Values Store main idea is the existence of a hash table where there is a unique key and a pointer to a particular item of data. These mappings are usually accompanied by cache mechanisms to maximize performance.


Column Family Store was created to store and process very large amounts of data distributed over many machines. There are still keys but they point to multiple columns.


Document Store model is basically versioned documents that are collections of other key-value collections. The semi-structured documents are stored in formats like XML, JSON (JavaScript Object Notation), YAML (Yet Another Markup Language)& BSON(Binary JSON).


Graph Store is built with nodes, relationships between notes and the properties of nodes. Instead of tables of rows and columns and the rigid structure of SQL, a flexible graph model is used which can scale across many machines.


Wednesday, January 9, 2013

TEST DRIVEN DEVELOPMENT = TEST FIRST DEVELOPMENT + REFACTORING



TEST DRIVEN DEVELOPMENT = TEST FIRST DEVELOPMENT + REFACTORING
Test driven development (TDD) is a software development process, in which developer write test first then start development in small incremental manner to pass this test and refactoring the code whenever required.
Now days customer expect from their software developer to write code with almost zero bug without compromising deadlines. To achieve this TDD is one of the software development processes which help in bug reduction, since this process start with testing and advocate testing after any refactoring happens which make sure that software work at least in tested scenarios if used correctly. TDD can change overall design of code for better if it is used effectively.

 
Usually developers develop the code first then write test cases. But in TDD process developer write a unit test then run that test and that test fail since developer didn’t written anything yet, now developer write code to pass that test and developer do required refactoring whenever required.

There are two type of TDD:
  1. Developer TDD: Process is same as TDD.
  2. Acceptance TDD / Behavior Driven Development (BDD): In BDD acceptance test are written then coding to pass that acceptance test.
 
How to implement TDD?
Think how new code will be implemented or refactoring of existing code happen.
Example: - Fibonacci number series 0,1,1,2,3,5,8…
Write just enough stub code, so developer can compile.   
Example:-
public int fibonacci(int x) {
return 0;
}

Add a test to for stub code.
Example:-
public void testFibonacci_whenX_isOne() {
assertEquals(0, Fibonacci(1));
}
Write a code to pass the test.
Example:-
public int fibonacci(int x) {
if(x == 0) return 0;
if(x == 1) return 1;
return 0;
}
Again run the test.
Add more tests for incremental development and refactoring code whenever possible.
Note: - In pair programming TDD is very effective. Since pair developer helps each other to keep in track.
TDD and Legacy code
The challenge is working with legacy code which didn’t have any test case. In order to introduce test into legacy code developer have to isolate the unit and refactoring the code, so that it can be tested.
Advantage of TDD
  1. TDD acts as a constant feedback that each unit is still working.
  2. TDD helps to isolate the problems in code.
  3. After the test pass and developer refactoring the code to remove duplicate code then again this test ensue development complete.
  4. Since TDD required very detail level of requirement. It forces developer to understand the requirement well before actual coding.
  5. The test suite acts as a regression safety net on bugs: If a bug is found, the developer should create a test to reveal the bug and then modify the production code so that the bug goes away and all other tests still pass. On each successive test run, all previous bug fixes are verified.
Disadvantage of TDD 
  1. TDD didn’t test with other resources like database, JMS etc. 
  2. If TDD is not used carefully, it can add cost and complexity to the project.
  3. TDD is highly reliant on refactoring and programming skill.
  4. It is difficult to detect deeper faults in code.

Even after enormous test case can create problem just like when you add this test into build for continuous integration it might take too long to complete. If developer use TDD properly, then code produced from this process is testable, well designed & developers find that they will be spending less time in debugging. But still developer make mistakes but it can be catch too early.    

Saturday, June 16, 2012

Start learning MongoDB with Java

MongoDB (named from "huMONGOus," meaning "extremely large")
MongoDB is an open source NoSQL database, which store data in form of BSON(Binary JavaScript Object Notation) documents which makes MongoDB to have dynamic schema and the integration of data in certain types of applications easier and faster.

MongoDB with Java
Yet another database, which is a NoSQL database. Okay, first you download it from http://www.mongodb.org/downloads and hey don’t forget download JDBC driver from https://github.com/mongodb/mongo-java-driver/downloads .
Here is the quick install link http://www.mongodb.org/display/DOCS/Quickstart.
Now I just tried to map MongoDB basic concepts with SQL.


SQLMongoDB
Schema Database
Table Collection
Row Document
Column Field
Index Index
Joining Embedding & Linking

Now we will try to create a collection in database.

               
Get Started
Now try to develop some sample programming with the help of mongoDB Java driver. Now start the the mongoDB server and include mongoDB Java driver into your classpath.
Now try to get connection
It is easy to create connection using com.mongo.Mongo class, if MongoDB run on local machine.
Mongo newConn = null;
newConn =  new Mongo();
or
newConn  = new Mongo(“localhost”);
or
newConn  = new Mongo(“localhost”,27017);  // 27017 is default port
Above code by default connect to localhost in 27017 port.


Create a collection


package com.mongoDB.test;
import java.net.UnknownHostException;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoException;
import com.mongodb.WriteResult;
class Address extends BasicDBObject {
      
  public Address(){
      
  }
  public Address(String name,String street,int zipcd) {
      put("name", name);
      put("street",street);
      put("zipcd",zipcd);
  }
  
}
public class CreateCollection {
  public static void main(String[] args) {
      Mongo dbConnection = null;
      try {
          //Create a connection
          dbConnection = new Mongo();
          /*
           * dbConnection = new Mongo("localhost");
           * dbConnection = new Mongo("localhost",27017);
           * */
          
          DB db =dbConnection.getDB("mydb");
          // Creating a database object
          Address address = new Address("Testing1","Local Street",123456);
          
          // Creating a database collection (Table)
          // db.collectionExists("address") check whether this collection exist or not
          // db.getCollection("address") return collection if exist
          // db.createCollection("address", address) create a collection
          DBCollection dbCollection = db.collectionExists("address")?db.getCollection("address"):db.createCollection("address", address);
          
          // dbCollection.save(address) save the document into collection
          dbCollection.save(address);
          //select all from 'address' collection
          DBCursor dbcur = dbCollection.find();
     
                              // check any document is there or not
          while(dbcur.hasNext()){
                                              // get the document
              DBObject dbObj =  dbcur.next();
              System.out.println(“\nName = ”+dbObj.get("name")+”  Street = ”+dbObj.get("street")+” ZIP CODE = “+dbObj.get("zipcd"));
          }
          
          
      }catch (UnknownHostException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (MongoException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
}
Insert a document
We already know how to get collection object. Now insert a document into a collection.
In JSON this document represented as
{
"phoneNumber" : NumberLong("99012233123"),
"phoneNumberType" : "Mobile",
"personId" : {
              "id" : "1232325",
              "idType" : "SSN"
            },
"name" : {
           "firstName" : "Fname",
           "middleName" : "Mname",
           "LastName" : "Lname"
         },
"address" : {
              "street" : "Street",
              "city" : "City",
              "zipcd" : "123256"
             }
}


Here personId,name,address are inner document. To insert a document we can use BasicDBObject  and BasicDBObjectBuilder.


BasicDBObject This class object hold a document in key value pair. Multiple object of this class means multiple documents whether it will be a inner document it depends


BasicDBObject name = new BasicDBObject();
name.put("firstName", "Fname");
name.put("middleName", "Mname");
name.put("LastName", "Lname");


BasicDBObjectBuilder  This class is similar like StringBuilder, simply create an object using BasicDbObjectBuilder.start() and append key value pair. Here is a point to note add(String,Object) and append(String,Object) both are same add method invoke append method. So there is no difference in using either.


BasicDBObjectBuilder address = BasicDBObjectBuilder.start();
address = address.add("street", "Street");
address = address.add("city", "City");
address = address.add("zipcd", "123256");





package com.mongoDB.test;


import java.lang.reflect.UndeclaredThrowableException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;


import org.bson.BSONObject;


import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;



public class InsertDocument {


   public static void main(String[] args) {
       Mongo dbConn = null;
       
       try {
           dbConn = new Mongo("localhost",27014);
           
           DB db = dbConn.getDB("mydb1");
           char[] passwd = {'p','a','s','s','w','o','r','d'};
           // Authenticating user
           if(!db.authenticate("user", passwd)){
               return;
           }
           DBCollection dbCollection = db.getCollection("mycoll");
           
           BasicDBObject personDemographic = new BasicDBObject();
         
/ * name:{"firstName":value,
            *       "middleName":value,
            *       "LastName":value}
            * */
           BasicDBObject name = new BasicDBObject();
           name.put("firstName", "Fname");
           name.put("middleName", "Mname");
           name.put("LastName", "Lname");
           
          /* address :  {
                 * street : value,
                                 * city : value,
                                 * zipcd : value }
                                **/
         BasicDBObjectBuilder address = BasicDBObjectBuilder.start();
           address = address.add("street", "Street");
           address = address.add("city", "City");
           address = address.add("zipcd", "123256");
           
           String personIdStr = "{'id' : '1232325','idType' : 'SSN'}";


           //parse it as a JSON String
           DBObject personId = (DBObject)JSON.parse(personIdStr);
           
           BSONObject phoneNo = new BasicDBObject();
           phoneNo.put("phoneNumber", 99012233123L);
           phoneNo.put("phoneNumberType", "Mobile");
           
           Map<String,DBObject> dbObjMap = new HashMap<String,DBObject>();
           dbObjMap.put("name", name);
           dbObjMap.put("address", address.get());
           dbObjMap.put("personId", personId);
           
           personDemographic.putAll(phoneNo);
           personDemographic.putAll(dbObjMap);
           
           
           dbCollection.insert(personDemographic);
           
           DBCursor dbCurr = dbCollection.find();
           while(dbCurr.hasNext()){
               System.out.println(dbCurr.next().toString());
           }
           
           
       } catch (UnknownHostException e) {
           e.printStackTrace();
       } catch (MongoException e) {
           e.printStackTrace();
       }
      }


}




Output


{ "_id" : ObjectId("4fe1afb46361e48f9f01bf5c"), "phoneNumber" : NumberLong("99012233123"), "phoneNumberType" : "Mobile", "personId" : { "id" : "1232325", "idType" :
"SSN" }, "name" : { "firstName" : "Fname", "middleName" : "Mname", "LastName" : "Lname" }, "address" : { "street" : "Street", "city" : "City", "zipcd" : "123256" } }


Update a document
Lets update a document. We can use methods like update, updateMulti and findAndModify to update a document. Among these methods  update method  is overridden & updateMulti method internally call update method not only that even save method internally call update method.


One update method which is called every time you call any update api.
public WriteResult update( DBObject query , DBObject o , boolean upsert , boolean multi , com.mongodb.WriteConcern concern, DBEncoder encoder );


query - takes DBObject as a query
o - take DBObject which will be replaced with new object
upsert - if the database should create a new document if it is not there
multi - if all matching document will be updated or not. An object will not be inserted if it does not exist in the collection and upsert=true and multi=true. See http://www.mongodb.org/display/DOCS/Atomic+Operations
concern - the write concern
encoder - DBEncoder to be used


package com.mongoDB.test;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;


public class UpdateDocument {


       public static void main(String[] args) {
Mongo dbConn = null;
       
       try {
           dbConn = new Mongo();
           DB db = dbConn.getDB("mydb1");
           
           DBCollection dbCollection = db.getCollection("mycoll");
           // Object to be quered
           DBObject query = new BasicDBObject();
           query.put("phoneNumber", 99012233123L);
           
           // Object to be replaced
           DBObject replaced = new BasicDBObject();
           replaced.put("phoneNumber", 98012233123L);
   dbCollection.update(query,  new BasicDBObject().append("$set", replaced), false, false);
           
           // Query for find
           query.put("phoneNumber", 98012233123L);
           DBCursor dbCur = dbCollection.find(query);
           
           while(dbCur.hasNext()) {
               System.out.println(dbCur.next());
           }
  
       }catch(Exception ex) {
           ex.printStackTrace();
       }
   }


}





Output


{ "_id" : { "$oid" : "504354ebf090ca58102e6523"} , "address" : { "street" : "Street" , "city" : "City" , "zipcd" : "123256"} , "name" : { "firstName" : "Fname" , "middleName" : "Mname" , "LastName" : "Lname"} , "personId" : { "id" : "1232325" , "idType" : "SSN"} , "phoneNumber" : 98012233123 , "phoneNumberType" : "Mobile"}