graphdatabaseintroducon - georgia state...

37
Graph database Introduc1on

Upload: others

Post on 16-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Graph  database  Introduc1on

Page 2: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Agenda

• What  is  NoSQL?  • What  is  a  Graph,  Anyway?  • What  is  a  Graph  Database?  • Neo4J  Graph  Database  

Page 3: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

What  is  NoSQL?

•  Stands  for  Not  Only  SQL    •  Class  of  non-­‐relaAonal  data  storage  systems  •  Usually  do  not  require  a  fixed  table  schema  nor  do  they  user  the  concept  of  joins,  group  by,  order  by  and  so  on.  

•  All  NoSQL  offerings  relax  one  or  more  of  the  ACID  (Atomicity,  Consistency,  IsolaAon,  Durability)properAes  

Page 4: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

What  is  NoSQL?

• Next  generaAon  databases  • CharacterisAcs:  

•  Large  Data  Volumes  •  Non-­‐relaAonal  •  Semi-­‐structured  data  •  Distributed  •  Less  restricAve  •  Scalable  replicaAon  and  distribuAon  

Page 5: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

ACID  vs  BASE •  ACID  =  Availability,  Consistency,  IsolaAon,  Durability  •  BASE  =  Basically  Available  SoV-­‐state  services  with  Eventual-­‐consistency    

Page 6: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Popular  NoSQL  Databases

Page 7: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Who  is  using  them?

Page 8: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Why    Graphs?

•  Ideal  data  structures  to  represent  relaAonships  • Provide  visual  representaAon  of  inter-­‐connecAons  • White-­‐board  friendly  •  Semi  structured  –  represent  a  wide  range  of  data  • VariaAons  –  (non)aYributed  /  (un)directed  /  (non)weighted,  etc.,.  

Page 9: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Some  Use  Cases

Page 10: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Social  Network

Page 11: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Route  Finding

Page 12: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Recommenda1ons

Page 13: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Logis1cs

Page 14: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

What  is  a  Graph  Anyway?

Page 15: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

A  Graph

Page 16: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Property  Graph  Data  Model

Page 17: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Nodes

• Used  to  represent  enAAes.  • Can  have  properAes  

Page 18: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Rela1onships

• Used  to  represent  connecAons  between  enAAes.    • Can  be  directed  or  undirected  • Can  have  properAes  

Page 19: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Rela1onships  (con1nued)

Nodes  can  have  more  than  one  relaAonship  

Nodes  be  connected  by  more  than  one  relaAonship  

Self  relaAonships  are  allowed  

Page 20: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Labels

•  Labels  idenAfy  the  node  and  edge  types  

Page 21: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Why  Graph  Databases?

A  relaAonal  database  may  tell  you  the  average  age  of  everyone  in  this  session,  but  a  graph  database  will  tell  you  who  is  most  likely  to  buy  you  a  beer!  

Page 22: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Graphs  are  unique

• Based  on  relaAonships  •  Structure  as  important  as  the  data  itself  • Difficult  to  capture  in  a  one-­‐dimensional  space  • Most  graph  queries  have  a  Aght  coupling  between  the  structure  and  data  •  ExisAng  storage  formats  incapable  of  opAmizing  storage  to  both  structure  and  data  

Page 23: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

RDBMS  and  Rela1onships

• RelaAonal  ≠  RelaAonships  • RDBMS  not  suitable  graphs  because  relaAonships  uncovered  through  joins  • No.  of  relaAonships  traversed  =  No.  of  Joins  •  Joins  are  expensive  operaAons  • Not  pracAcal  for  large  datasets  

Page 24: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

RDBMS  and  Rela1onships

2  Joins  for  2  relaAonships!  

Page 25: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Graphs  and  Rela1onships

RelaAonships  explored  through  graph  traversals!  

Page 26: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

• Graph  Databases  allow  a  drasAc  reducAon  in  search  space  Search  

Space  

Graph  Databases  Advantage

Page 27: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Graph  Databases  Advantage

• No  joins!  –  RelaAonships  explored  though  graph  traversals  • OpAmized  for  storage  and  querying  graphs  • Querying  –  APIs  or  dedicated  languages.  BeYer  expressiveness    

Page 28: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Graph  Databases  in  the  NoSQL  world

Page 29: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Neo4J  Graph  Database  

Page 30: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Neo4J  -­‐  Introduc1on

• Robust  and  high  performance  naAve  graph  database  • OpAmized  for  connecAons  between  records  •  True  ACID  transacAons  • High  availability  •  Scales  to  billions  of  nodes  and  relaAonships  • High  speed  querying  through  traversals  

Page 31: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Neo4J  -­‐  Introduc1on

• Uses  the  property  graph  model  • Graph  queried  through  traversals  •  Traversal  

•  VisiAng  the  nodes  of  the  graph,  following  the  edges  according  to  specific  rules  •  Depth  first  /  Breadth  first  

• Paths  •  Set  of  nodes  with  connecAng  relaAonships  •  Fundamental  units  for  graph  queries  and  query  results  

Page 32: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Querying  Method-­‐  Cypher

• Cypher  •  DeclaraAve,  SQL-­‐like  •  Emphasizes  on  the  “WHAT”  instead  of  the  “HOW”  •  Major  constructs:  

•  START  •  MATCH  •  WHERE  •  RETURN  •  CREATE  •  SET  •  DELETE  •  FOREACH    

 

Page 33: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

CYPHER  –  Code  examples

• CREATE  •  Creates  a  graph  object  of  a  given  type  and  given  aYributes    •  Eg:  CREATE  (node1:Person{name:”John”,  age:20})    

•  This  statement  creates  a  node  node1  of  type  Person  with  aYributes  name  as  ”John”  and  age  as  20.  

•  Naming  the  created  object  (node1  in  this  case)  is  opAonal.  

• RETURN  •  Returns  a  specific  graph  object  •  Used  in  conjuncAon  with  CREATE/MATCH/DELETE/SET  •  Eg:  CREATE  (node1:Person{name:”John”,  age:20})  RETURN  (node1)  

•  This  statement  returns  the  node1  object  that  was  created  

 

Page 34: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

CYPHER  –  Code  examples

• MATCH  •  Specifies  the  graph  paYern  to  match.  Forms  the  graph  query.  •  Eg:  MATCH  (node2:Person{name:”Mary”,  age:23})  

•  This  statement  matches  the  graph  object  with  name  Mary,  of  age  23  and  belonging  to  the  type  “Person”  

•  Complex  path  queries  can  also  be  given  the  MATCH  clause.  •  Eg:  MATCH  (node2:Person{name:”Mary”,  age:23})-­‐[rel1:knows]-­‐>(node3:Person{city:”NY”})  

•  This  statement  matches  all  paths  in  the  graph  such  that  a  node  of  type  “Person”  with  name  “Mary”  and  age  23  is  connected  to  another  node  of  type  “Person”  with  aYribute  city  as  “NY”  through  a  knows  relaAonship.  

•  Naming  and  returning  the  objects  is  opAonal  and  is  generally  used  for  further  querying  

Page 35: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Try  out  at  hVp://console.neo4j.org/  

Page 36: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

Querying  Method  –  as  an  embedded  database  in  JAVA  applica1on GraphDatabaseService  graphDB  =  new  EmbeddedGraphDatabase(“var/neo4j”);  //  start  the  database  TransacAon  tx  =  graphDb.beginTx();  //  Neo4j  supports  transacAons.  include  all  accesses  inside  a  transacAon  try{                //  create  nodes                Node  node1  =  graphDb.createNode();  

             Node  node2  =  graphDb.createNode();                //  set  properAes  for  nodes                node1.setProperty(“name”,”john”);                node2.setProperty(“name”,”mary”);                //  create  relaAonships.  Reltypes  is  an  enum  of  the  relaAonship  types  allowed.  This  needs  to  be  predefined  

             RelaAonship  rel1  =  node1.createRelaAonshipTo(node2,  Reltypes.KNOWS);                rel1.setProperty(“date”,today);  //  set  properAes  for  the  relaAonship                System.out.printl(“node  1  name  =“+node1.getProperty(“name”)+”  “+                                                (“node  2  name  =“+node2.getProperty(“name”));  //  print  the  results                tx.success();  

 }          finally{                tx.finish();  //  close  the  transacAo    }  

Page 37: GraphdatabaseIntroducon - Georgia State Universitytinman.cs.gsu.edu/~raj/8711/sp15/GraphDatabases.pdf · 2018-05-17 · Agenda • Whatis%NoSQL?% • Whatis%aGraph,%Anyway?% • Whatis%aGraph%Database?%

More  code  examples  and  tutorials  at  hVp://neo4j.com/docs/stable/introduc1on.html