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/
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
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
7. Start with the shell
• The Gremlin terminal is a Groovy shell
• Groovy is a superset of Java that has various
shorthand
7
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
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. 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]
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]
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. 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
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. 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
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
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. 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()
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