getting started with graph databases & neo4j
DESCRIPTION
The presentation gives a brief information about Graph Databases and its usage in today's scenario. Moving on the presentation talks about the popular Graph DB Neo4j and its Cypher Query Language i.e., used to query the graph.TRANSCRIPT
Getting started withGraph Databases & Neo4j
Suroor Wijdan@wotthetech
Agenda for Today
● Intro to Graph Databases● Who is using graph databases?● What is Neo4j?● MongoDB vs Neo4j?● Nodes and Relationships● Cypher Query Language● CRUD operations in CQL● Project Use Case● Questions?● Exercises
What is NoSQL?
● NoSQL stands for “Not Only SQL” ● Some questions which you can ask to database:
○ What is my Average Income?
○ What Items I have in his Shopping Cart?
○ How did I get into this session?
- Ask a RDBMS
- Ask a Key Value Store
- Ask a Graph
Intro to Graph Databases
Graph is everywhere
● Graph Databases present a new perspective on the same type of data.
● Graph is the most generic type of data structure capable of storing data in highly accessible way
● Suitable for any kind of data that is related
● Graph databases are optimized for the relations between records
● a graph containing Nodes & Relationships
● with both having Properties
● verily perfect for complex and highly connected data
Important Quote
A Quote....
“A relational database may tell you how much your customer has spent at your store”
“but”
“a graph database will tell your customers what should they buy next”
Who is using Graph Databases?
● Some big names using Graph Databases:○ Facebook - Open Graph Database○ Google - Knowledge Graph ○ Twitter - FlockDB distributed graph Database○ Adobe ○ Glassdoor○ JustDial○ CareerBuilder○ Indiatimes○ telenor○ Hellwet Packard○ T-Mobile○ Cisco
ALL THESE USE
What is Neo4j?
● Neo4j is a robust property graph database● Fully Transactional (ACID)● Highly Agile● Best suited for data which is highly
connected● Is supremely fast when it comes to querying
connected data● The most popular graph database in the
world ● Highly scalable, up to several billion
nodes/relationships/properties
What is Neo4j?
● Neo4j allows infinite depth● Uses Cypher Query Language for querying,
also has Java and REST API’s● Human Readable Queries● Data Modelling in Neo4j :
○ The whole model relies on the questions we have to ask our database
○ Very easily done, even when designing domains in SQL we tend to make graphs on whiteboards
● Neo4j helps us derive patterns from our data
MongoDB vs Neo4j
MongoDB is meant for cases where you would like to have dynamic queries on quite a size of data.- Not capable of handling relationships
Neo4j is best suitable in use cases where you have complex relationships and data which is highly connected. Neo4j can help you find routes, social patterns, etc. from your data. - Not Horizontally Scalable (as of now)
Nodes and Relationships
● A minimal graph can consist of a single node with properties defined on it.
● A Property graph has Nodes and Relationships with properties
defined on them.● node ( vertex)● relationship (edge) :- with direction ● property(attribute) :- on nodes and relationships
Lets see an example: -
Nodes and Relationships
Da Vinci Code
Dan Brown Manoj
Roni
The Lost Symbol
Gone with the Wind
Suspense
Thriller
Nodes and Relationships
Da Vinci Code
Dan Brown Manoj
Roni
The Lost Symbol
Harry Potter
Suspense
Thriller
Authored ByRead By
Friends with
Authored By
RecommendsBelongs To
Belongs ToBelongs ToRelease Date
Recommends
J.K Rowling Authored By
An Example of Book Store with Recommendations
Cypher Query Language
● Is a declarative query language for querying Neo4j● Expressive and Human readable syntax● Matches patterns of nodes and relationships to
extract/modify information in the graph
● With cypher, we can create, update, remove nodes, relationships and properties
● Has an online console at http://www.neo4j.org/console
● Has a short learning curve due to similarities with SQL query statements
Cypher Query Language
Create a Node:
CREATE (n:User { fname:"Manoj", lname:“Nama” });
● User is the Label● n is the variable for new node● {} brackets to add properties to the node
Cypher Query Language
Read Properties of a Node:
MATCH (n:User) WHERE n.fname = “Manoj”RETURN n
● User is the Label● n is the variable for node● WHERE to restrict the result to our
criteria● RETURN the properties on the node
Cypher Query Language
Update property on a Node:
MATCH (user:User)WHERE user.fname = 'Manoj'SET user.lname = ‘Mohan’RETURN user.fname, user.lname;
● User is the Label● Restricts search to the nodes under USER
label● SET adds a new property to the node● RETURN clause indicates what data to
return
Cypher Query Language
Delete a Node:
MATCH (user:User)WHERE user.fname = 'Emily'DELETE user
● User is the Label● Restricts search to the nodes under USER
label● DELETE clause deletes a node from graph● RETURN clause indicates what data to
return
Cypher Query Language
Set a new property on a Node after creation:
MATCH (user:User)WHERE user.name = 'Roni'SET user.country = ‘India’RETURN user.name, user.country;
● User is the Label● Restricts search to the nodes under USER
label● SET adds or updates a property on a node● RETURN clause indicates what data to
return
Cypher Query Language
Find a node in CQL
START n=node(*) WHERE HAS (n.name) AND n.name = "Roni"RETURN n;
● START clause to begin a query● n=node(*) to search through all nodes● WHERE clause to constrain the results● n.name indicates the name property must
exist● = "Roni" compares an existing name to the
value Roni● RETURN clause requests particular results
Steps to get a Neo4j server up & running(Linux)
Run the following commands in Terminal:
● sudo bash● wget -O - http://debian.neo4j.
org/neotechnology.gpg.key | apt-key add - ● echo 'deb http://debian.neo4j.org/repo
stable/' > /etc/apt/sources.list.d/neo4j.list
● apt-get install neo4j● start neo4j server, available at http:
//localhost:7474 of the machineneo4j start
Neo4j Web Admin
References
1. Online Manual - http://docs.neo4j.org/chunked/milestone/2. Online Cypher Console - http://console.neo4j.org3. Cypher Cheat Sheet - http://docs.neo4j.org/refcard/1.9/4. Graph image taken from - http://www.neo4j.org
THANKS!Get in touch for any queries :
twitter: @wotthetechgithub: suroorwijdan
PS: You are free to share this ppt but give due credit to the creator wherever used.