I had the chance to do a project in big data using Graph databases. Then we had used Neo4j and managed to learn quite some stuff - through hack techniques. Nevertheless our short stint with Graph databases was quite enjoyable and I decided to explore Neo4j once more.
I decided to look at Neo4j using the Movie database that is provided as an example. After loading the data I decided to query the database through java.
The first thing I want to do is simply fetch all movie records. I started building a simple java application but got overwhelmed simply downloading the jars needed ! So the alternative (and smarter way ! ) was to create a Maven project.
Accordingly I added a pom file:
I decided to look at Neo4j using the Movie database that is provided as an example. After loading the data I decided to query the database through java.
The first thing I want to do is simply fetch all movie records. I started building a simple java application but got overwhelmed simply downloading the jars needed ! So the alternative (and smarter way ! ) was to create a Maven project.
Accordingly I added a pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Here for neo4j the dependencies needed is just one but the actual jars added are more than 30 jars ! The next part was to create a pojo class for Movie:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>robin.spring-bd</groupId>
<artifactId>neo4j-basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>neo4j-basic</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
publicclassMovie {Last is the actual code to access the database:
privateLong id;
privateString title;
privateint released;
privateString tagline;
public Movie() {
// Default Constructor: This is needed by Neo4j
}
//setter getters added
}
publicclass MovieDAO {To explain the code:
privatestaticfinalString DB_LOCATION = "E:\\neo4jData\\21Dec2015";
final GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
final GraphDatabaseService graphDb = dbFactory.newEmbeddedDatabase(newFile(DB_LOCATION));
publicList<Movie> getAllMovies() {
try (Transaction tx = graphDb.beginTx()) {
Result result = graphDb.execute("MATCH (m:Movie) RETURN m");
// System.out.println(result.resultAsString());
List<Movie> movies = new ArrayList<>();
while (result.hasNext()) {
Map<String, Object> row = result.next();
// Result Set contains each of the row as a Map, with each of
// the
// returned values matched against the keys
Node nodeProxy = (Node) row.get("m");
Movie movie = new Movie();
try {
movie.setTitle((String) nodeProxy.getProperty("title"));
} catch (NotFoundException e) {
}
try {
movie.setTitle((String) nodeProxy.getProperty("title"));
} catch (NotFoundException e) {
}
try {
Long releaseYear = (Long) nodeProxy.getProperty("released");
movie.setReleased(releaseYear != null ? releaseYear.intValue() : -1);
} catch (NotFoundException e) {
}
try {
movie.setTagline((String) nodeProxy.getProperty("tagline"));
} catch (NotFoundException e) {
}
/*
* Returns the unique id of this node. Ids are garbage collected over
* time so they are only guaranteed to be unique during a specific time
* span: if the node is deleted, it's likely that a new node at some
* point will get the old id. Note: this makes node ids brittle as
* public APIs.
*/
movie.setId(nodeProxy.getId());
movies.add(movie);
}
return movies;
}
}
publicstaticvoid main(String[] args) {
List<Movie> movies = new MovieDAO().getAllMovies();
movies.forEach(movie -> System.out.println(movie));
System.out.println("No of Movies ::: " + movies.size());
}
}
- First step was to create an instance of the Factory class - GraphDatabaseFactory. This provides is with the various access methods to get the services we need.
- The next step is to create a service or handle to connect to the database - GraphDatabaseService. Here we provide the path to our database location on the system.
- To access the movies we first created a transaction. In earlier versions of Neo4j, transactions were not needed for read operations, but now we need one.
- We created a Cypher query and passed it to the execute method. This returns a Result instance. As per the documentation "The result is comprised of a number of rows, potentially computed lazily, with this result object being an iterator over those rows".
- Each row is a Map with the return name as keys and value as the Node objects. Each node object represents a Neo4j node here.
- We used the getProperty method to extract the values from the node. If a value does not exists then neo4j throws a NotFoundException.
- The last code adds an Id property. As these ids are recycled, its not a good idea to make them a part of the public code.
public void cleanup() {This will Shuts down Neo4j. After this method has been invoked, it's invalid to invoke any methods in the Neo4j API and all references to this instance of GraphDatabaseService should be discarded.
graphDb.shutdown();
}