Google News
logo
Neo4J Interview Questions
Neo4j is an open source NOSQL graph database, implemented in Java. It saves data structured in graphs rather than in tables.

It is one of the most popular open-source free NOSQL  Graph DBMS (database management system) developed by Neo4j, Inc. It written in Java and Scala..The development of Neo4j was started in 2003, it has been publicly available since 2007. The source code and issue tracking of Neo4j is available on GitHub, with support readily available on Stack Overflow and the Neo4j Google group.
Neo4J is called graph database because it stores data structure in graph instead of in tables.
The first version of Neo4J was Neo4j 1.0 and it was released in Feb, 2010.
Neo4J is mainly used for :
 
* Real time data analysis
* Knowledge graph
* Network and IT operations
* Real-time recommendation engines
* Data management
* Identity and Access management
* Social Network
* Privacy and Risk management
Neo4j is widely used for :
 
* Recommendation- ( e-coomerce)
* Path Finding
* Data First Schema (bottom-up)
* Schema Evolution
* A* (Least Cost Path)
* Highly connected data – Social Network
Neo4j :
* It consists of vertices and edges. Each vertex or node represent a key value or attribute
* It is possible to store dynamic content like images, videos, audio,
* It has the capability for deep search into the database without affecting the performance along with efficient timing
* We can relate any two objects in neo4j by the mean of making relationship between any two nodes
 
MySQL :
* In relational databases, attributes are appended in plain table format
* In relational databases, such as MySQL, it’s difficult to store videos, audios, images,
* It takes longer time for database search and also inconvenient compared to neo4j
* It lacks relationship and difficult to use them for connected graphs and data
RDBMS Graph Database
Tables Graphs
Rows Nodes
Columns and Data Properties and its Values
Constraints Relationships
Joins Traversal


Roles of building blocks :
 
* Nodes : They are entities equivalent to rows in table.

* Relationship :
It connects entities and structure domain.

* Properties :
It contains meta-data and attributes.

* Labels :
It groups nodes by role.
Neo4J supports UNIQUE constraints :
 
* Neo4J uses Native graph storage with Native GPE(Graph Processing Engine).

*
Neo4J supports exporting of query data to JSON and XLS format.

*
Neo4J provides REST API to be accessed by any Programming Language like Java, Spring, Scala etc.

*
Neo4J provides Java Script to be accessed by any UI MVC Framework like Node JS.

*
Neo4J supports two kinds of Java API: Cypher API and Native Java API to develop Java applications.
There are two different types of object caches in Neo4j
 
* Reference Caches : With this cache, Neo4j will use as much as allocated JVM heap memory as it can hold nodes and relationships

* High-performance Caches : It get assigned a certain maximum amount of space on the JVM heap and will delete objects whenever it grows bigger than that.
Relationship and Nodes are added to the object cache as soon as they are accessed
Neo4j uses Cypher query language, which is unique to Neo4j. Traversing the graph requires to know where you want to begin (Start), the rules that allow traversal (Match) and what data you are expecting back (Return).

The basic query consists of :
 
* START n
* MATCH n-[r]- m
* RETURN r;
To delete/remove entire graph directory you can use command rm –rf data/* as such Neo4j is not storing anything outside that.
Neo4J allows to store and retrieve multiple complex relations. The capability of Neo4j to do complex query in real time is really helpful in identifying a brute force attack much quicker.  The most crucial thing in detecting such attacks is to capture enough information about each requests like
 
* Client real IP address and not the proxy one
* Login failure or attempt success information
* Timestamp
There was no indexing in earlier days for Neo4j, but later on it was introduced with new feature Automatic Indexes by using the command
START n=node:node_auto_index(name='abc') RETURN n
Cypher is Neo4j’s graph query language that lets you retrieve data from the graph. It is like SQL for graphs, and was inspired by SQL so it lets you focus on what data you want out of the graph (not how to go get it). It is the easist graph language to learn by far because of its similarity to other languages, and intuitiveness.

Cypher Query Language
Cypher is unique because it provides a visual way of matching patterns and relationships. Cypher uses an ASCII-art type of syntax where (nodes)-[:ARE_CONNECTED_TO]->(otherNodes) using rounded brackets for circular (nodes), and -[:ARROWS]-> for relationships. When you write a query, you draw a graph pattern through your data.
 
Neo4j users use Cypher to construct expressive and efficient queries to do any kind of create, read, update, or delete (CRUD) on their graph, and Cypher is the primary interface for Neo4j.

Source : Neo4j
Matching patterns is easy while working with nodes using Neo4J.
Example : To get cast of actors starting with S
MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)
WHERE movie.title STARTS WITH "S"
RETURN movie.title AS title, collect(actor.name) AS cast
ORDER BY title ASC LIMIT 10;
Other available graph databases in the market are :
 
* Oracle NoSQL Database
* Sqrrl Enterprise
* Graph Story
* Titan
* Velocity Graph
* InfoGrid
* Bitsy
* Teradata Aster
* Oracle Spatial and Graph
* Info Grid and many more.
There are many commands in Neo4J, of which the following are few basic commands:
 
* CREATE - To create a node or relationship.

*
MATCH - To read or retrieve all the nodes in the database.

*
MERGE - Combination of CREATE and MATCH.

*
SET - To add or update properties to new or existing nodes/relationships.

*
CREATE UNIQUE - To mention unique constraints in order to avoid redundant values.
 
Neo4j MongoDB
A primary database model is Graph DBMS. A primary database model is Document Store.
Implemented in Java and Scala.. Implemented in C++ Language.
Neo4j has an optional schema. MongoDB is schema-free.
It uses Cypher query language, Java API, Neo4j-OGM, Spring Data Neo4j, TinkerPop 3 Proprietary protocol using JSON
Uses triggers No triggers are used
Neo4j CQL use SET clause for the following purpose :
 
* Update or Add properties values

*
Add new properties to existing Relationship or Node
In Neo4J, object cache is used to store individual nodes, their relationships and their properties in a form which is optimized for fast traversal of the graph. Reading from object cache is 5 to 10 times faster than reading from the file buffer cache.
Delete commands in Neo4J :
 
Delete a single node :
MATCH (n:Person { name: 'UNKNOWN' })  
DELETE n
Delete all nodes and relationships :
MATCH (n)  
DETACH DELETE n ​
Delete a node with its relationship :
MATCH (n { name: 'Andres' })  
DETACH DELETE n
Delete relationships only :
MATCH (n { name: 'Andres' })-[r:KNOWS]->()  
DELETE r
As such Neo4j got RESTful API, you can query over the web, or you can run it locally. It runs in the Heroku or Cloud.
MATCH command is used with RETURN or UPDATE clause. It cannot be used alone otherwise it will give error.
 
Syntax :
MATCH   
(  
   <node-name>:<label-name>  
)  
The MATCH command cannot be used alone to fetch data from the database otherwise it will show invalid syntax error.
Neo4j stores primitive array in a compressed way in order to save the space on disk, to do that it uses a “bit saving” algorithm.
The Set clause in Neo4j can be used to add new properties to an existing relationship or node. It can also be used to update or add existing properties values. To set a property in a node, here is following syntax :

Syntax : 
MATCH (node:label{properties . . . . . . . . . . . . . . })
SET node.property = value 
RETURN node
Neo4j as a graph database features indexing as the preferred way to find start points for graph traversals. Over the years multiple different indexing approach have been added.

The goal of this article is to give an overview on this to avoid confusion esp. for those who just recently got started with Neo4j. A graph database using a property graph model stores its data in nodes, relationships and properties.

In Neo4j 2.0 this model was amended with labels.Neo4J supports no indexes in the beginning later it started support for manual, automatic and schema indexes.