An introduction to installing Neo4j and how to run basic queries against it. How to use Neoxygen to create a PHP connection and send Cypher queries to the database.
1. Graph DATA and PHP
NEO4J and NEOXYGEN
For PHP Belfast - April 23rd 2015
2. TRADITIONAL DB
Traditional databases conform to rigid rows & column structures.
Join data through intermediary tables
Graphs can exist - but complex and messy (and slow to query)
3. GRAPH DB?
So is a Graph DB a bar chart ;line chart or pice chart.
of course - it's not a graph in that traditional sense
4. WHAT IS GRAPH DATA?
Common examples of Graphs that we all can recognise are
Twitter and Facebooks graphs.
Graph DBs lend themselves very well to social data, because Graphs
depend on relationships.
5. START WITH A NODE
Start with a simple node - one objec with a few properties
6. ADD MORE NODES
We can add more and mroe nodes
BUT they're still just float unrelated
7. ADD RELATIONSHIPS
We need to join nodes together through relationships -
indicated by lines or arrows.
8. ADD MORE NODES AND RELATIONSHIPS
As we add more nodes and more relationships,
the visualisation gets more and more busy
9. NODES Of THE SAME TYPE CAN STORE DIFFERENT INFORMATION
A big advantage over traditional RDBS is that two nodes of the same
basic type can store castly different properties.
10. RELATIONSHIPS HAVE METADATA TOO
Even the relationships can have properties, to tell us more
about the nature of the relationship
11. Introducing Neo4J
The Most common GraphDB is Neo4J
12. Installing Neo4J
Installation is easy
visit http://neo4j.com/download/ and
download the community edition
13. Java SDK v1.7+ required
Need to install Java SDK on a Mac
NOT the Java Runtime - download from
http://oracle.com/technetwork/java/javase/downloads
14. Set the Java Path
Update your path so that terminal uses the new Java SDK
$ export JAVA_HOME = `/usr/libexec/java_home -v 'v1.7*'`
change command according to version downloaded
15. Start the Neo4J Server
Start the server form the command line
Inside the directory you unpacked neo4j in type
$ ./bin/neo4j start
16. The Neo4J local browser interface
http://localhost:7474
Lots of great examples in there
You can save you favourite queries
17. Security - connection
By default the server only listens on localhost.
You can set a specific IP address in conf/neo4j-server.properties
18. Security - Authorisation
Default username/password is neo4j/neo4j
Passwords stored in data/dbms.auth as SHA-256
You can turn off the authorisation requirement - but dont do that.
19. SO... How do we query a Graph DB?
20. Cypher is the Neo4J Query Language
CREATE() - makes a new node
CREATE(bruce:Person)
21. Create a Node with properties
CREATE (bruce:Person { name: 'Bruce Wayne' , alterego : 'Batman' })
22. CREATE() a Relationship
Relationships are shown in Square Bracket notation [].
CREATE (a)-[:LIVES_IN]->(b)
23. MATCH() is used to find nodes
MATCH(p:Person) Return p
MATCH (p:Person)-[:ENEMY_OF]->(joker) RETURN p
24. Match with Criteria
MATCH(p:Person {name: 'Bruce'}) RETURN p
MATCH (p:Person)-[:ENEMY_OF]->(joker) RETURN p
25. Using PHP to Query the Graph
26 Enter Neoxygen
Install using composer - Yay!
27. Simple instantiation
create the client builder
setAutoFormatResponse to true
28. Some Neoxygen Basics
Send a query to the server
->sendCypherQuery($query)
29. What return options are there?
$response->getResult()
$response->getRows()
$client->getResponse()
30. Get a list of nodes
$result->getNodess()
$result->getSingleNode()
$nodeProperties = $node->getProperties()
$node->getProperty();
31. Get Relationships
$node->getRelationships('ENEMY_OF')
32. Use Parameters in Queries
MATCH(p:Person {name: { name } } ) RETURN p
->sendCypherQuery($query, array('placeholder' => $value ));
33. Simple Query to delete everything
34. Some Useful Links
http://localhost:7474 - start with the examples
NEO4J Documentation
http://neo4j.com/developer/get-started/
Gists
http://gist.neo4j.org/?6029850
Cypher Cheat Sheet
http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf
35. Some Useful Books
http://neo4j.com/books/
O'Reilly - Graph Databases
PACKT - Learning Neo4j