Hello NoSQL World
The Architects group in Kewill Product Development have been evolving a model reference architecture for the next generation products that will offer cutting edge features to our customers. NoSQL databases are one of the key components of the new architecture; though we already use them in our existing product suite, they will have an enhanced role to play in the future. We are going with MongoDB.
Having been used to relational databases for most part of my career, the NoSQL flavor is an interesting and welcome change to application development. To get a first hand feel for MongoDB, I wrote a couple of newbie Hello-World programs each for inserting into and fetching data from MongoDB and MySQL.
I inserted a million records into MongoDB in the first program. In the second program, I fetched those million records and printed to screen. For MySQL, I inserted 20,000 records in the first program. In the second program, I fetched those 20,000 records and printed to screen.
The insert blew my mind away; MongoDB is a clear winner. It takes ages to insert data into MySQL. For the fetch, I am a bit unclear. Here are the results of the run:
MongoDB
Insert a million records ---------------------------------- 25303 ms (23.3 seconds)
Fetch & print the million records ---------------------- 267479 ms (4 minutes 27.5 seconds)
MySQL
Insert 20,000 records ------------------------------------- 806178 ms (13 minutes 26.2 seconds)
Fetch & print 20,000 records --------------------------- 4415 ms (4.5 seconds)
Platform : Dell Latitude laptop E6410 with i5 M250 processor @ 2.4 GHz & 8 GB RAM, 64-bit Windows 7 Professional, jdk 1.6.0_45, mongo v2.6, MySQL 5.6.
Here are the programs:
Having been used to relational databases for most part of my career, the NoSQL flavor is an interesting and welcome change to application development. To get a first hand feel for MongoDB, I wrote a couple of newbie Hello-World programs each for inserting into and fetching data from MongoDB and MySQL.
I inserted a million records into MongoDB in the first program. In the second program, I fetched those million records and printed to screen. For MySQL, I inserted 20,000 records in the first program. In the second program, I fetched those 20,000 records and printed to screen.
The insert blew my mind away; MongoDB is a clear winner. It takes ages to insert data into MySQL. For the fetch, I am a bit unclear. Here are the results of the run:
MongoDB
Insert a million records ---------------------------------- 25303 ms (23.3 seconds)
Fetch & print the million records ---------------------- 267479 ms (4 minutes 27.5 seconds)
MySQL
Insert 20,000 records ------------------------------------- 806178 ms (13 minutes 26.2 seconds)
Fetch & print 20,000 records --------------------------- 4415 ms (4.5 seconds)
Platform : Dell Latitude laptop E6410 with i5 M250 processor @ 2.4 GHz & 8 GB RAM, 64-bit Windows 7 Professional, jdk 1.6.0_45, mongo v2.6, MySQL 5.6.
Here are the programs:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.UnknownHostException; | |
import com.mongodb.Mongo; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DBCursor; | |
import com.mongodb.MongoException; | |
public class Mongo_Insert_Collection | |
{ | |
public static void main(String[] args) { | |
try { | |
Mongo mongo = new Mongo("localhost", 27017); | |
DB db = mongo.getDB("mh-db"); | |
DBCollection collection = db.getCollection("mhColl"); | |
BasicDBObject document; | |
System.out.println("Inserting a million records"); | |
long time1 = System.currentTimeMillis(); | |
for (int i = 0; i < 1000000; i++) { | |
document = new BasicDBObject(); | |
document.append("age"+i+1, i+1); | |
collection.insert(document); | |
} | |
long time2 = System.currentTimeMillis(); | |
System.out.println("------------------"); | |
System.out.println("Insert took " + (time2 - time1) + " ms"); | |
} | |
catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} | |
catch (MongoException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.MongoClient; | |
import com.mongodb.MongoException; | |
import com.mongodb.WriteConcern; | |
import com.mongodb.DB; | |
import com.mongodb.DBCollection; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DBObject; | |
import com.mongodb.DBCursor; | |
import com.mongodb.ServerAddress; | |
import java.util.Arrays; | |
public class Mongo_Retrieve_Collection | |
{ | |
public static void main( String args[] ) | |
{ | |
try { | |
// To connect to mongodb server | |
MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); | |
// Now connect to your databases | |
DB db = mongoClient.getDB( "mh-db" ); | |
System.out.println("Connect to database successfully"); | |
DBCollection collection = db.getCollection("mhColl"); | |
System.out.println("Collection mhColl selected successfully"); | |
System.out.println("Retrieving and printing the records"); | |
long time1 = System.currentTimeMillis(); | |
DBCursor cursor = collection.find(); | |
while (cursor.hasNext()) { | |
System.out.println(cursor.next()); | |
} | |
long time2 = System.currentTimeMillis(); | |
System.out.println("------------------"); | |
System.out.println("Retrieving and printing took " + (time2 - time1) + " ms"); | |
} | |
catch (Exception e){ | |
System.err.println( e.getClass().getName() + ": " + e.getMessage() ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//STEP 1. Import required packages | |
import java.sql.*; | |
public class MySQL_Insert | |
{ | |
// JDBC driver name and database URL | |
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | |
static final String DB_URL = "jdbc:mysql://localhost/test"; | |
// Database credentials | |
static final String USER = "root"; | |
static final String PASS = "root"; | |
public static void main(String[] args) | |
{ | |
Connection conn = null; | |
Statement stmt = null; | |
try { | |
// STEP 2: Register JDBC driver | |
Class.forName("com.mysql.jdbc.Driver"); | |
// STEP 3: Open a connection | |
System.out.println("Connecting to test database..."); | |
conn = DriverManager.getConnection(DB_URL, USER, PASS); | |
System.out.println("Connected database successfully..."); | |
// STEP 4: Execute a query | |
System.out.println("Inserting records into the table..."); | |
stmt = conn.createStatement(); | |
long time1 = System.currentTimeMillis(); | |
// Insert twenty thousand records | |
for (int i = 0; i < 20000; i++) { | |
String sql = "INSERT INTO seq " + | |
"VALUES (" + i + | |
", 'seq" +i + | |
"')"; | |
stmt.executeUpdate(sql); | |
} | |
long time2 = System.currentTimeMillis(); | |
System.out.println("------------------"); | |
System.out.println("Inserted 20000 records into the table..."); | |
System.out.println("Insert took " + (time2 - time1) + " ms"); | |
} | |
catch (SQLException se) { | |
// Handle errors for JDBC | |
se.printStackTrace(); | |
} | |
catch (Exception e) { | |
// Handle errors for Class.forName | |
e.printStackTrace(); | |
} | |
finally { | |
// finally block used to close resources | |
try { | |
if (stmt != null) | |
conn.close(); | |
} | |
catch (SQLException se) { | |
} // do nothing | |
try { | |
if (conn!=null) | |
conn.close(); | |
} | |
catch (SQLException se) { | |
se.printStackTrace(); | |
} //end finally try | |
} //end try | |
System.out.println("Goodbye!"); | |
} // end main | |
} // end MySQL_Insert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//STEP 1. Import required packages | |
import java.sql.*; | |
public class MySQL_Fetch | |
{ | |
// JDBC driver name and database URL | |
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; | |
static final String DB_URL = "jdbc:mysql://localhost/test"; | |
// Database credentials | |
static final String USER = "root"; | |
static final String PASS = "root"; | |
public static void main(String[] args) | |
{ | |
Connection conn = null; | |
Statement stmt = null; | |
try { | |
// STEP 2: Register JDBC driver | |
Class.forName("com.mysql.jdbc.Driver"); | |
// STEP 3: Open a connection | |
System.out.println("Connecting to test database..."); | |
conn = DriverManager.getConnection(DB_URL, USER, PASS); | |
System.out.println("Connected database successfully..."); | |
// STEP 4: Execute a query | |
System.out.println("Fetching records from the table..."); | |
stmt = conn.createStatement(); | |
long time1 = System.currentTimeMillis(); | |
// Fetch twenty thousand records and print them | |
String sql = "SELECT * FROM seq"; | |
ResultSet rs = stmt.executeQuery(sql); | |
// Extract data from result set | |
int id; | |
String seq; | |
while (rs.next()){ | |
// Retrieve by column name | |
id = rs.getInt("idseq"); | |
seq = rs.getString("seqcol"); | |
// Print record | |
System.out.println("id: " + id + " seq " + seq); | |
} | |
rs.close(); | |
long time2 = System.currentTimeMillis(); | |
// Print time | |
System.out.println("------------------"); | |
System.out.println("Fetched and printed 20000 records from the table..."); | |
System.out.println("Time taken = " + (time2 - time1) + " ms"); | |
} | |
catch (SQLException se) { | |
// Handle errors for JDBC | |
se.printStackTrace(); | |
} | |
catch (Exception e) { | |
// Handle errors for Class.forName | |
e.printStackTrace(); | |
} | |
finally { | |
// finally block used to close resources | |
try { | |
if (stmt != null) | |
conn.close(); | |
} | |
catch (SQLException se) { | |
} // do nothing | |
try { | |
if (conn!=null) | |
conn.close(); | |
} | |
catch (SQLException se) { | |
se.printStackTrace(); | |
} //end finally try | |
} //end try | |
System.out.println("Goodbye!"); | |
} // end main | |
} // end MySQL_Fetch |
Comments
Post a Comment