Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

10b. Graph Databases Lab

1,534 views

Published on

In this lecture we analyze graph oriented databases. In particular, we consider TtitanDB as graph database. We analyze how to query using gremlin and how to create edges and vertex.
Finally, we presents how to use rexster to visualize the storeg graph/

Published in: Data & Analytics
  • Login to see the comments

  • Be the first to like this

10b. Graph Databases Lab

  1. 1. Graph Oriented Databases Lab Ciao ciao Vai a fare ciao ciao Dr. Fabio Fumarola
  2. 2. TitanDB 2
  3. 3. Getting Started • As example we use the Graph of the Godsexample • This Graph stores: – As nodes: locations, god, demigod, titan, monster and humans – As relations: battled, mother, father, brother, lives, person 3
  4. 4. 4
  5. 5. Start TitanDB There are three options: 1.Download a binary version from https://github.com/thinkaurelius/titan/wiki/Downloads 2.Download a Docker image from docker hub https://registry.hub.docker.com/u/lulumialu/titandb/ 5
  6. 6. Docker Setup $ docker pull lulumialu/titandb $ docker run –it--name titan lulumialu/titandb bash $ ./bin/gremlin.sh 6
  7. 7. Start with the shell • The Gremlin terminal is a Groovy shell • Groovy is a superset of Java that has various shorthand 7
  8. 8. Loading the Graph of Gods • We use BerkeleyDB and Elasticsearch index backend 8 gremlin> g = TitanFactory.open('conf/titan-berkeleydb-es.properties') ==>titangraph[berkeleyje:../db/berkeley] gremlin> GraphOfTheGodsFactory.load(g) ==>null
  9. 9. Gremlin 9
  10. 10. Find a Starting Node • The first step is locating a node using a graph index • That entry point is an element (or set of elements) • Starting from that we can traverse the graph 10 gremlin> saturn = g.V.has('name','saturn').next() ==>v[256] gremlin> saturn.map() ==>name=saturn ==>age=10000 gremlin> saturn.in('father').in('father').name ==>hercules
  11. 11. Find a Starting Edge • We can use GeoQuery to query the place edge • We look for event happened within 50 kms from Athens 11 g.query().has('place',WITHIN,Geoshape.circle(37.97,23.72,50)).edges(). collect { it.bothV.name.next(2) } ==>[hercules, hydra] ==>[hercules, nemean]
  12. 12. Graph Traversal: Saturn’s grandchild was Hercules 12
  13. 13. Graph Traversal: Saturn’s grandchild was Hercules 13 gremlin> hercules = saturn.as('x').in('father').loop('x') {it.loops < 3}.next() ==>v[1536]
  14. 14. Graph Traversal: to prove that Hercules is demigod 14
  15. 15. Graph Traversal: to prove that Hercules is demigod 15 gremlin> hercules.out('father','mother') ==>v[1024] ==>v[1792] gremlin> hercules.out('father','mother').name ==>jupiter ==>alcmene gremlin> hercules.out('father','mother')*.getVertexLabel() ==>god ==>human gremlin> hercules.getVertexLabel() ==>demigod
  16. 16. Query: find Hercules heroic exploits 16
  17. 17. Query: find Hercules heroic exploits 17 gremlin> hercules.out('battled') ==>v[2304] ==>v[2560] ==>v[2816] gremlin> hercules.out('battled').map ==>{name=nemean} ==>{name=hydra} ==>{name=cerberus} gremlin> hercules.outE('battled').has('time',T.gt,1).inV.name ==>cerberus ==>hydra
  18. 18. Query: Cohabiters of Tartarus 18
  19. 19. Query: Cohabiters of Tartarus 19 gremlin> pluto = g.V('name','pluto').next() ==>v[2048] gremlin> // who are pluto's cohabitants? gremlin> pluto.out('lives').in('lives').name ==>pluto ==>cerberus gremlin> // pluto can't be his own cohabitant gremlin> pluto.out('lives').in('lives').except([pluto]).name ==>cerberus gremlin> pluto.as('x').out('lives').in('lives').except('x').name ==>cerberus
  20. 20. Query: Pluto’s Brothers 20
  21. 21. Query: Pluto’s Brothers 21
  22. 22. Query: Pluto’s Brothers 22 gremlin> pluto.out('brother').out('lives').name ==>sky ==>sea gremlin> // which brother lives in which place? gremlin> pluto.out('brother').as('god').out('lives').as('place').select ==>[god:v[1024], place:v[512]] ==>[god:v[1280], place:v[768]] gremlin> // what is the name of the brother and the name of the place? gremlin> pluto.out('brother').as('god').out('lives').as('place').select{it.name} ==>[god:jupiter, place:sky] ==>[god:neptune, place:sea]
  23. 23. Query: Reason Pluto lives in Tartarus 23
  24. 24. Query: Reason Pluto lives in Tartarus 24 gremlin> pluto.outE('lives').reason ==>no fear of death gremlin> g.query().has('reason',CONTAINS,'loves').edges() ==>e[6xs-sg-m51-e8][1024-lives->512] ==>e[70g-zk-m51-lc][1280-lives->768] gremlin> g.query().has('reason',CONTAINS,'loves').edges().collect{ [it.outV.name.next(),it.reason,it.inV.name.next()] } ==>[jupiter, loves fresh breezes, sky] ==>[neptune, loves waves, sea]
  25. 25. Gremlin in Depth For a complete gremlin guide check •http://s3.thinkaurelius.com/docs/titan/0.5.4/gremlin. html •http://gremlindocs.com/ •http://sql2gremlin.com/ 25
  26. 26. Schema and Data Modeling 26
  27. 27. Defining an Edge Label 1/2 • We need a unique name for the label edge • Multiplicity Settings: – MULTI: Allows multiple edges of the same label between any pair of vertices. – SIMPLE: Allows at most one edge of such label between any pair of vertices. – MANY2ONE: Allows at most one outgoing edge of such label on any vertex in the graph but places no constraint on incoming edges. 27
  28. 28. Defining an Edge Label 2/2 • We need a unique name for the label edge • Multiplicity Settings: – ONE2MANY: Allows at most one incoming edge of such label on any vertex in the graph but places no constraint on outgoing edges. – ONE2ONE: Allows at most one incoming and one outgoing edge of such label on any vertex in the graph. 28
  29. 29. Defining an Edge Label 29 mgmt = g.getManagementSystem() follow = mgmt.makeEdgeLabel('follow').multiplicity(Multiplicity.MULTI).make() mother = mgmt.makeEdgeLabel('mother').multiplicity(Multiplicity.MANY2ONE). make() mgmt.commit()
  30. 30. Defining a Property • Properties on vertices and edges are key-value pairs. • For instance, the property name='Daniel' has the key name and the value 'Daniel'. 30
  31. 31. Property Key Cardinality Are used to set a cardinality of the values of a property •Single •List •Set 31
  32. 32. Property Key Cardinality 32 mgmt = g.getManagementSystem() birthDate = mgmt.makePropertyKey('birthDate').dataType(Long.class).cardinality(C ardinality.SINGLE).make() name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(Car dinality.SET).make() sensorReading = mgmt.makePropertyKey('sensorReading').dataType(Double.class).card inality(Cardinality.LIST).make() mgmt.commit()
  33. 33. Relation Types for edges • The names of relation types must be unique in the graph 33 mgmt = g.getManagementSystem() if (mgmt.containsRelationType('name')) name = mgmt.getPropertyKey('name') mgmt.getRelationTypes(EdgeLabel.class) mgmt.commit()
  34. 34. Defining Vertex Labels • Like edges, vertices have labels. • Unlike edge labels, vertex labels are optional. 34 mgmt = g.getManagementSystem() person = mgmt.makeVertexLabel('person').make(); mgmt.commit() // Create a labeled vertex person = g.addVertexWithLabel('person') // Create an unlabeled vertex v = g.addVertex(null) g.commit()
  35. 35. Titan Server 35
  36. 36. Rexter • Titan uses the Rexster engine as the server component to process and answer client queries. • The Titan Download comes preconfigured with a script, titan.sh, which starts Cassandra, Elasticsearch, and Titan with Rexster. 36
  37. 37. Rexster Dog House Visualization 37
  38. 38. Rexter Gremlin 38
  39. 39. References • http://s3.thinkaurelius.com/docs/titan/0.5.4/index.h tml • http://www.quora.com/What-are-the-differences- between-a-Graph-database-and-a-Triple-store 39

×