Neo4j Graph Database Seminar 5 of NoSQL Databases (PA195) David Novak & Vlastislav Dohnal Faculty of Informatics, Masaryk University, Brno Agenda ● Graph Databases ● Neo4j ○ Basic information ○ Data model ○ Cypher query language ■ Structure and examples ■ Other interfaces: Experience with Web UI ○ Java API (embedded database) ○ Traversal of the graph ■ Traversal framework ■ Examples 2 Graph Databases: Example source: Sadalage & Fowler: NoSQL Distilled, 2012 3 Neo4j: Basic Info 6 Source: neo4j.com ● Open source graph database ● Initial release: 2007 ○ Current version 4.2 ● Written in: Java ● OS: cross-platform ● Full transactions (ACID) ● Partitioning: supported by queries ● since 4.0, by Neo4j Fabric ● Replication: Master-slave ○ Eventual consistency Data Model: Nodes http://db-engines.com/en/system/Neo4j ● Fundamental unit: node ● Nodes have properties ○ Key-value pairs ○ null is not a valid property value ■ nulls can be modelled by the absence of a key ● Nodes have labels ○ labels typically express "type of node" 7 Data Model: Relationships ● Directed relationships (edges) ○ Incoming and outgoing edge ■ Equally efficient traversal in both directions ■ Direction can be ignored if not needed by the application ○ Always a start and an end node ■ Can be recursive 9 Use of Neo4j ● Two ways to use Neo4j: ○ Embedded: Used directly within a Java application ○ Self-standing server + connections ● Various types of connections ○ Neo4j Cypher Shell ○ HTTP API ■ uses Cypher query language ○ Web GUI ■ using Cypher query language ○ Standard Java API ○ Gremlin graph traversal language (plugin), etc. 10 Neo4j in Server mode ● Virtual machine http://stratus.fi.muni.cz ○ Template “PA195 - Neo4j”, ID 255 $ ssh root@... -L 7474:localhost:7474 -L 7687:localhost:7687 # neo4j-community-3.1.4/bin/neo4j start ● or Install on your own: ○ download from https://neo4j.com/ to /var/tmp ○ tar xvzf neo4j-community-*.tar.gz ■ module add jdk ○ ./bin/neo4j start 11 stratus Neo4j Command-line Querying ● Cypher shell ○ ./bin/cypher-shell ○ can also be installed separately, but shipped with the server 12 HTTP API ● Query/update operations using HTTP protocol ○ GET, POST methods ○ data sent/received in JSON ● Fully transactional in the latest version ● Example: create node with “name” property curl -i -X POST http://localhost:7474/db/neo4j/tx/commit -H "Content-Type: application/json; charset=UTF-8" --user "neo4j" -d '{ "statements": [ { "statement": "CREATE (n $props) RETURN n", "parameters": { "props": { "name": "John Doe" } } } ] }' https://neo4j.com/docs/http-api/current/ 13 Neo4j Web Interface ● By default, running on http://localhost:7474/ ○ default credentials: neo4j/neo4j ● Online interpreter of Cypher ● Graphical display of query results 14 Cypher Language ● Neo4j graph query language ○ For querying and updating ● Declarative – we say what we want ○ Not how to get it ○ Not necessary to express traversals ● Human-readable ● Inspired by SQL and SPARQL ● Still growing = syntax changes are often http://neo4j.com/docs/stable/cypher-query-lang.html 15 Cypher: Clauses ● MATCH: The graph pattern to match ● WHERE: Filtering criteria ● RETURN: What to return ● WITH: Divides a query into multiple parts ● CREATE: Creates nodes and relationships. ● DELETE: Remove nodes, relationships, properties ● SET: Set values to properties 16 https://neo4j.com/docs/cypher-manual/current/ Cypher: Creating Nodes (Examples) CREATE (n); (create a node, assign to var n) Created 1 node, returned 0 rows CREATE (a: Person {name : 'Jan'}) RETURN a; (create a node with label ‘Person’ and ‘name’ property Jan’) Created 1 node, set 1 property, returned 1 row 17 Task 1: Add people with names: John, Jack, Andres Cypher: Creating Relationships MATCH (a {name:'John'}), (b {name:'Jack'}) CREATE (a)-[r:FRIEND]->(b) RETURN r ; (create a relation FRIEND between John and Jack) Created 1 relationship, returned 1 row 18 Cypher: Creating Paths CREATE p = (andres: Person {name: 'Andres'}) -[:WORKS_AT]-> (neo) <-[:WORKS_AT](michael: Person {name:'Michael'}) RETURN p ; (all parts of the pattern are created) P [Node[4]{name:"Andres"},:WORKS_AT[2] {},Node[5]{},:WORKS_AT[3] {},Node[6]{name:"Michael"}] 1 row Nodes created: 3 Relationships created: 2 Properties set: 2 19 To create just a relationship, use MATCH and WHERE Cypher: Changing Properties MATCH (n: Person {name: 'Andres'}) SET n.surname = 'Taylor' RETURN n (find a node with name ‘Andres’ and set it surname ‘Taylor’) n Node[0]{name:"Andres",surname:"Taylor"} 1 row Properties set: 1 20 Task 2: Update Queries 21 ● Modify nodes by using the SET clause: ○ Set Andres’s age to 24 ○ Make a FRIEND relationship from Andres to Person ○ Change the anonymous node create in the previous slide (neo), so the label is Company and name is ‘Neo4j Corp.’ Cypher: Queries with filters MATCH (user: Person {name: 'Andres'})-[:FRIEND]->(follower) RETURN user.name, follower.name (find all ‘friends’ of 'Andres') MATCH (p: Person) WHERE p.age > 18 AND p.age < 30 RETURN p.name (return names of all adult people under 30) 22 Cypher: Queries (2) MATCH (andres: Person {name: 'Andres'})-[*1..3]-(node) RETURN andres, node ; (find all ‘nodes’ within three hops from ‘Andres’) MATCH p=shortestPath( (andres:Person {name: 'Andres'})-[*]-(david {name:'David'}) ) RETURN p ; (find the shortest connection between ‘Andres’ and ‘David’) 23 Cypher: Delete MATCH (n: Person {name: 'Andres'}) DELETE n (delete all Persons with name ‘Andres’) Cannot delete node<3>, because it still has relationships. MATCH (n: Person {name: 'Andres'}), ((n)-[r]-()) DELETE r,n (first, we must delete all relationships of node with name ‘Andres’) Nodes deleted: 1 Relationships deleted: 1 24 Task 3: Movies Database ● Go over the “Movies” demo prepared by Neo4j ○ Download the data from the course page (movies- insert.cypher) ○ Copy the file to Stratus VM ○ Import by cypher shell # cd neo4j-community-3.1.4 # bin/cypher-shell -u neo4j -p