Let's keep getting the most useful web development tips from InternetDevels developers.
Here's the first blog post about Neo4j in which you will learn the basics.
Neo4j is the most popular graph database (NoSQL). Unlike the conventional MySQL or PostgreSQL, it doesn’t store the data as a table but as a graph, thus giving you the flexibility in handling connections between the nodes, and these connections are considered the most important.
When you should use a graph database (Neo4j)
Graph databases give you an advantage when working with the data for which the connections are very important, especially if you have to work with several different levels of connections.
IT giants, such as Google, Facebook, LinkedIn and PayPal, are actively using graph databases. This allows them to fully unleash the potential of connections. Graph databases are much faster at working with highly connected data than relational and NoSQL databases, and they also have built-in support for graph algorithms.
Installing Neo4j (Ubuntu)
1) Download Neo4j Community Edition from the official site
2) Unpack
tar -xf neo4j-enterprise-2.3.1-unix.tar.gz
3) For your convenience, set an environment variable which specifies the path to the folder where we have unpacked Neo4j. To do this, we can add a line in the .bashrc file (this is optional).
export NEO4J_HOME=/opt/neo4j-community-2.3.1
where /opt/neo4j-community-2.3.1 is the folder to which we have unpacked Neo4j/
4) Run
$NEO4J_HOME/bin/neo4j console
or
sudo $NEO4J_HOME/bin/neo4j console
where $NEO4J_HOME is the variable we have set in (3). If we haven’t done that, we should specify the full path.
Neo4j can also be run as a service, with this command:
$NEO4J_HOME/bin/neo4j start
If you have done everything right, you’ll see the Neo4j admin menu at this link http://localhost:7474/browser/
Cypher
Cypher is a query language used in Neo4j.
Here are some examples of queries:
MATCH (n)RETURN n LIMIT 100 — selection
MATCH (n {name:"p1"})
RETURN n
selection by name field
CREATE (n:Person {name:"p2"})
RETURN n
creation (the screen is similar to the previous one)
MATCH (n {name:"p2"})
DELETE n
removal (nothing is returned in this query, so there is no screen)
MATCH (a:Person),(b:Person)
WHERE a.name = 'p1' AND b.name = 'p2'
CREATE (a)-[r:RELTYPE]->(b)
RETURN r
setting a connection
MATCH (n:Person)
RETURN n
select by (Person) type
MATCH (node1)-[:RELTYPE]->(node2)-[:RELTYPE]->(node3)
RETURN node3
selection by connections (two levels of hierarchy)
MATCH (node1:Person { name:'p2' })-[r]->(node2:Person)
RETURN r
selection of all connections for the given node
MATCH (node1:Person { name:'p2' })-[r]->(node2:Person { name:'p3' }) SET r.distance = 10
RETURN r
setting a value for a given connection
MATCH (from:Person {name: 'p2'})-[:RELTYPE*1..100]->(to)
RETURN distinct to
selection of nodes with which you can reach the given node through a RELTYPE connection within 1-100 steps
MATCH p =(:Person { name: "p1" })-[:RELTYPE*0..5]-(:Person { name: "p5" }) RETURN extract(n IN nodes(p)| coalesce(n.name)) AS `names`, length(p) ORDER BY length(p)
LIMIT 10;
selection of paths in which you can go from one given point to another given point within 0-5 steps
MATCH (node1:Person { name:"p1" }),(node2:Person { name:"p5" }), p = shortestPath((node1)-[rels:RELTYPE*0..5]-(node2)) WHERE ALL (r IN rels WHERE NOT r.distance = 7)
RETURN p
finding the shortest path (here the shortest path is a path with the least number of intermediate nodes) that would meet the given requirements
MATCH p=(startNode:Person { name:"p1" })-[rels:RELTYPE*1..4]->(endNode:Person { name:"p5" }) RETURN p AS shortestPath, reduce(distance=0, r in rels | distance + r.distance) AS totalDistance
LIMIT 1
finding the shortest path between the given nodes (here the shortest path is the one for which the sum of the distance value for the RELTYPE connection is the smallest).
MATCH p=(startNode:Person { name:"p1" })-[rels:RELTYPE*1..4]->(endNode) WITH p, reduce(distance=0, r in rels | distance + r.distance) AS totalDistance WHERE totalDistance = 28
RETURN p
searching for all the routes that have the given sum of the distance value for the RELTYPE connection within 1-4 steps
As you can see, Cypher is a simple and yet flexible query language, adapted for graph data displays.
Here is a guide for Neo4j queries - http://neo4j.com/developer/cypher-query-language/
In the next part, our web development services company will put the above information into practice, and create an API using Neo4j as a database, Phalconphp as a framework, Neo4jphp as PHP wrapper for Neo4j and Redis for caching.