Showing posts with label spring data. Show all posts
Showing posts with label spring data. Show all posts

Sunday, December 7, 2014

Perform CRUD Operations Using Spring-Data-Neo4j.

Introduction:

In this post, we are trying to use Spring-Data-Neo4j with web application to persist the data in the graph form. Spring-Data-Neo4j is the huge wrapper on Neo4j, which is famous graph database community. Neo4j implementation is done by Java , but have support for connectivity with multiple programming languages. Neo4j have two types, one is opensource and other is licensed. 
There are so many graph database engines are available, but still the Neo4j is good product in all open source and have good community support. 

Graph-Database engines are designed in two ways as follow:
  1. Some database engines are native engines, means they store the data in the way of graphs internally. These types of engines are fast and efficient like Neo4j, OrientDB etc 
  2. Some database engines are store the graphs and nodes internally in RDBMS, Object Oriented Database and some other general databases. The example of these graph-database is FoundationDB. These are also called graph layer database. 
In the Graph Database, we create the nodes for store the data and these nodes are connected with each others. Graph Database is the part of No-SQL (Not-Only SQL) but provide ACID operations for store data. For more information go to this link

Graph-Database Storage For RDBMS Developers:

  1. In the table storage, the data is store in the form of records. One table have multiple records. In Graph database one node represent to one record.
  2. In the table storage the one table represent to one entity like User, Address etc and one table have multiple records. In graph storage nodes have labels and we identify multiple records represent to one entity through labels.
  3. In table storage we are use SQL for query the data from tables. In graph storage the query language is depends on graph-db engine like neo4j use cypher query language and orientdb use sql. 
  4. In table storage the relations between tables are manage through primary-key foreign-key relationship for (1:1) and (1:n) or (n:1). If the relation is (m:m) we maintain join tables. In graph-db the relation is by-directional and we also maintain as uni-directional. In these relationship we also set the attributes about relationship, that's why these relationship are called first class relationship. In graph-db relations are individual represent as entity and in graph we easily traverse from different nodes relationship without any headache of sql joins.

Example:

In the example, we are creating a flow to create, delete, read and maintain the relationship between nodes. Our example Technoloy stack as follow: 
  1. Spring-Data-Neo4j
  2. Thymeleaf
  3. Junit with Hamcrest
  4. JDK 8
  5. Google Guava
  6. Project Lombok
NOTE: For run the example, firstly configure project lombok with eclipse. For more information go to this link

Download the example code from github as below:
https://github.com/harmeetsingh0013/Spring-Data-Neo4j-Example

After downloading the code, you need to follow some steps:

  1. Before Import project into eclipse, configure project lombok
  2. Change the database path according to your system in "db.properties" file.
  3. Configure neo4j-community application for check nodes as a graphic layout. Download from this link. After installation set the path of db, that is used by example application. 
  4. At one time, the one instance is used Neo4j database like our application or neo4j-community application. Otherwise you get an exception related for lock the database.
  5. When run the JUNIT test, it run successfully, but nodes are not store permanently. When the actual application is run and save the node, the node persist permanently successfully.
  6. After launch an sample application, click on following URL: http://localhost:8080/neo4j-spring-example-web/save-person


Please give you suggestion in comments. 


Saturday, August 17, 2013

Perform CRUD Operation's MongoDB with Java.


MongoDB :

Data in MongoDB has a flexible schemaCollections do not enforce document structure. This means that:

  • documents in the same collection do not need to have the same set of fields or structure, and
  • common fields in a collection’s documents may hold different types of data.

Each document only needs to contain relevant fields to the entity or object that the document represents. In practice, most documents in a collection share a similar structure. Schema flexibility means that you can model your documents in MongoDB so that they can closely resemble and reflect application-level objects. For more information Click on that link.

Requirements : 

  1. Download MongoDB from here.
  2. Install MongoDB and set the PATH variable . 
  3. Install JDK 7 (The following code is Tested On JDK 7.

Note: Before Running the code, Need to start MongoDB server .


Following is the CODE to perform CRUD operation's of MongoDB with JAVA.

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.QueryBuilder;
import com.mongodb.ServerAddress;

public class MongoDBCRUDOperationInJava {
 public static void main(String[] args) throws UnknownHostException {
  MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
  DB courseDB = client.getDB("students");
  DBCollection collection = courseDB.getCollection("grades");
  //collection.drop();
  //insertDocument(collection);
  //findOne(collection);
  //findAll(collection);
  //count(collection);
  //findWithCriteriaUsingDBObject(collection);
  findWithCriteriaUsingQueryBuilder(collection);
}

public static void insertDocument(DBCollection collection) {
 DBObject doc = new BasicDBObject("_id", new ObjectId());
 doc.put("name", "Harmeet");
 doc.put("age", 24);
 doc.put("birthday", new Date(12312312313l));
 doc.put("programmer", true);
 doc.put("language", Arrays.asList("Java", "Scala", "Python"));
 doc.put("address", new BasicDBObject("street", "5").append("area", "moga"));
 DBObject doc2 = new BasicDBObject().append("name", "Jimmy");
System.out.println("Before Inserting the Document in Collection : "+doc);
 collection.insert(Arrays.asList(doc, doc2));
 System.out.println("After Inserting the Document in Collection : "+doc);
}
public static void findOne(DBCollection collection) {
 DBObject doc = collection.findOne();
 System.out.println(doc);
}
public static void findAll(DBCollection collection) {
 DBCursor cur = collection.find();
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
 cur.close();
 }
}
public static void count(DBCollection collection) {
 System.out.println("COunt of collection Documents : "+collection.count());
}
public static void findWithCriteriaUsingDBObject(DBCollection collection) {
 DBObject criteria = new BasicDBObject("type", "homework").append("student_id", 9);
 DBCursor cur = collection.find(criteria);
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
  cur.close();
 }
}
public static void findWithCriteriaUsingQueryBuilder(DBCollection collection) {
 QueryBuilder builer =  QueryBuilder.start("type").is("homework").and("student_id").is(9).and("score").greaterThan(60);
 DBCursor cur = collection.find(builer.get(), new  BasicDBObject("student_id" , true).append("_id", false));
 try{
   while(cur.hasNext()){
   DBObject doc = cur.next();
   System.out.println(doc);
  }
 }finally{
  cur.close();
 }
}
}

Download the CODE from here.