Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Graph data with Neo4J PHP & Neoxygen

Graph data with Neo4J PHP & Neoxygen

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

Avatar for Tim Swann

Tim Swann

April 23, 2015
Tweet

More Decks by Tim Swann

Other Decks in Technology

Transcript

  1. TRADITIONAL DB ID NAME CITY 1 Albert London 2 Bernard

    New York 3 Charlotte Paris 4 Daniel London
  2. GRAPH DB? 0 25 50 75 100 April May June

    July 0 25 50 75 100 April May June July
  3. GRAPH DB? 0 25 50 75 100 April May June

    July 0 25 50 75 100 April May June July NO….
  4. WHAT IS GRAPH DATA? Facebook Open Graph Twitter Firehose Big

    Data (buzzword alert!) Ideal for “Social” Media
  5. WHAT IS GRAPH DATA? Facebook Open Graph Twitter Firehose Big

    Data (buzzword alert!) Ideal for “Social” Media NODES and RELATIONSHIPS
  6. NODES OF THE SAME TYPE CAN STORE DIFFERENT INFORMATION Batman

    Spiderman (batman:Superhero {
 realname: Bruce Wayne
 Lives: Gotham City
 Net Worth: $100billion} ) ( spiderman: Superhero {
 realname: Peter Parker meta_human: Yes climb_walls: Yes cape: No Hobbies: Photography} )
  7. SECURITY - CONNECTION file: conf/neo4j-server.properties #  http  port  (for  all

     data,  administrative,  and  UI  access)   org.neo4j.server.webserver.port=7474   #let  the  webserver  only  listen  on  the  specified  IP.  Default   #is  localhost  (only  accept  local  connections).  Uncomment  to  allow   #any  connection.   org.neo4j.server.webserver.address=0.0.0.0   http://neo4j.com/docs/stable/security-server.html Be Security Aware: Never switch off IP filter in production
  8. SECURITY - AUTHORISATION file: data/dbms/auth neo4j:SHA-­‐256,ABCDEFGH…… http://neo4j.com/docs/stable/security-server.html #  Require  (or

     disable  the  requirement  of)  auth  to  access  Neo4j   dbms.security.auth_enabled=false file: conf/neo4j-server.properties Be Security Aware: Never switch off auth in production
  9. CREATE()…. A Node with extra properties CREATE(objname:ObjectType  {propertyname:  “Property  Value”})

    CREATE(bruce:Person  {realname:“Bruce  Wayne”,  alterego:  “Batman”}) CYPHER - THE NEO4J QUERY LANGUAGE
  10. CYPHER - THE NEO4J QUERY LANGUAGE CREATE()…. A Relationship between

    nodes CREATE(object1)-­‐[:REL_TYPE  {propertykey:PropertyVal}]-­‐>(object2)
  11. CYPHER - THE NEO4J QUERY LANGUAGE CREATE()…. A Relationship between

    nodes CREATE(object1)-­‐[:REL_TYPE  {propertykey:PropertyVal}]-­‐>(object2) CREATE(joker)-­‐[:DANCES  {where:  “In  the  pale  moonlight”}]-­‐>(the_devil)
  12. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find entities in

    the graph MATCH(object1)    RETURN  object1
  13. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find entities in

    the graph MATCH(object1)    RETURN  object1 Find everything
  14. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find entities in

    the graph MATCH(object1)    RETURN  object1 MATCH(n)  RETURN  n     Find everything
  15. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find with criteria

    MATCH(object1:Label  {criteria:Description}  )    RETURN  object1
  16. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find with criteria

    MATCH(object1:Label  {criteria:Description}  )    RETURN  object1 Find everything
  17. CYPHER - THE NEO4J QUERY LANGUAGE MATCH()…. Find with criteria

    MATCH(object1:Label  {criteria:Description}  )    RETURN  object1 MATCH(bruce:Person  {realname:”Bruce  Wayne”})  RETURN  bruce     Find everything
  18. NEOXYGEN composer.json 1  {   2      "require":  {

      3          "neoxygen/neoclient":"~2.1"   4      }   5  } https://github.com/neoxygen/neo4j-neoclient
  19.  use  Neoxygen\NeoClient\ClientBuilder;        $client  =  ClientBuilder::create()    

           -­‐>addConnection('default',  'http',  'localhost',  7474)
          -­‐>setAutoFormatResponse(true)            -­‐>build();   NEOXYGEN
  20.  //  use  the  client  to  send  a  query    

       $query  =  “MATCH(n)  RETURN  (n)”;    $client-­‐>sendCyperQuery($query);   NEOXYGEN - BASICS  //  use  the  client  to  send  a  query  &  grab  the  result        $query  =  “MATCH(n)  RETURN  (n)”;    $result  =  $client-­‐>sendCyperQuery($query)-­‐>getResult();  
  21.  //  use  the  client  to  send  a  query    

       $query  =  “MATCH(n)  RETURN  (n)”;        $client-­‐>sendCyperQuery($query)-­‐>getResult();        //  OR    $client-­‐>sendCyperQuery($query)-­‐>getRows();        //  OR    $client-­‐>sendCyperQuery($query);    $client-­‐>getResponse(); NEOXYGEN - RETURN OPTIONS
  22.  //  return  an  array  of  nodes        $query

     =  “MATCH(p:Person)  RETURN  (p)  LIMIT  10”;    $result  =  $client-­‐>sendCyperQuery($query)-­‐>getResult();    $arrayOfNodes  =  $result-­‐>getNodes();    foreach  ($arrayOfNodes  as  $node  )  {        $nodeProperties  =  $node-­‐>getProperties();    }   NEOXYGEN - Get Nodes
  23.  //  get  relationships  of  a  node        $query

     =  “MATCH(p:Person  {type:‘Superhero’})  RETURN  (p)”;    $result  =  $client-­‐>sendCyperQuery($query)-­‐>getResult();    $heros  =  $result-­‐>getNodes();    foreach  ($heros  as  $hero  )  {        $enemies  =  $node-­‐>getRelationships(‘ENEMY_OF’);    }   NEOXYGEN - Get Relationships
  24.  //  use  {parameter}  notation          $query  =

     “MATCH(p:Person  {name:  {  personname  }                                                        type:  {  persontype  }                                                        })  RETURN  (p)”;    $result  =  $client-­‐>sendCyperQuery($query,  array(                                  ‘personname’=>  ‘Bruce  Wayne’,                                  ‘persontype’  =>  ‘Superhero’                                                                        ))                        -­‐>getResult();   NEOXYGEN - Using Parameters in Queries
  25. Cypher - Delete Everything
 
 Find all nodes and relationships


    And delete them  MATCH  (n)    OPTIONAL  MATCH  (n)-­‐[r]-­‐()    DELETE  n,r