Graph Tools

By Andreas Kuczera

What are Graph databases?

Characteristics of graph databases

  • NoSQL
  • optimized for transactional performance: Native graph processing benefits traversal performance, but at the expense of making some queries that don’t use traversals difficult or memory intensive.
  • advantages: performance, flexibility, agility:
    • performance increase when dealing with connected data
    • schema free: extremely flexible, additive data model
    • mode of delivery aligned with today’s agile software delivery practices

The labeled property graph model

Building blocks of the property graph model

Example graph: Charlemagne, Einhard and a book

Example graph

Explanation

  1. The graph shows a node with the label “Person” and the property “Name” on top which has the value “Charlemagne”.
  2. Below, to the left, there is another node with the label “Person” by the name of Einhard.
  3. Below, to the right, there is a node with the label “Buch” (book) and the property “Titel” (title) which has the value “Vita Karoli Magni”.
  4. The edges indicate through their labels that Charlemagne knew Einhart, that Einhard wrote the book “Vita Karoli Magni” ca. 828-830 and that Charlemagne is mentioned in this book.

Software: Neo4j

  • Download: https://neo4j.com/download/
  • Or use a Sandbox
  • Start Neo4j-Desktop or use browser interface (http://localhost:7474):
    • At the top left, there is information about the structure of the database and some sample commands. The third tab contains the documentation
    • The input field for the commands is located in the area on the right; below, the window where results are shown
    • First login with neo4j/neo4j. Then assign your own password. Remember the password!
  • Query language: cypher

Practice makes perfect

cypher commands

cypher refcard

My first node

// creates a node 
CREATE (n); 

// creates a node and returns it 
CREATE (n) RETURN *; 

// creates a node with a label and returns the result
CREATE (n:Person) RETURN *; 

// creates a node with a label and an attribute and returns the result
CREATE (n:Person {name:'Andreas'}) RETURN *; 

// creates a node with a label and an attribute, if it doesn't exist yet, and returns the result
MERGE (n:Person {name:'Andreas'}) RETURN *; 

// Adds the attribute "placeOfBirth" to a node with the attribute "name:Andreas"
MATCH (n:Person {name:'Andreas'}) SET n.placeOfBirth = 'Giessen' RETURN *; 

Relation and evaluation

// Creates an edge between two nodes, if it doesn't already exist, and returns the nodes and the edge
MATCH (n1:Person {name:'Andreas'})  
MATCH (n2:Person {name:'Torsten'})   
MERGE (n1)-[:KNOWS]->(n2)  
RETURN *;
// names and ranks the places of birth of persons by frequency, order descending
MATCH (n:Person)  
RETURN n.placeOfBirth, count(*) as count, collect(n.surname) as surname  
ORDER BY count DESC;

Literature