an introduction to tinkerpop
DESCRIPTION
TRANSCRIPT
An Introductionto Tinkerpop
@doryokujin
GraphDB Meet-Up Japan #1
・Takahiro Inoue(age 26)
・twitter: doryokujin
・Majored in Math (Statistics & Graph Algorithm)
・Data Scientist
・Leader of MongoDB JP
・Interest: DataProcessing, GraphDB
About Me
Open source software development group focused on technologies in the emerging graph database ecosystem
・ : A Property Graph Model Interface
・ : A Dataflow Framework using Process Graphs
・ : A Graph Traversal Language
・ : A RESTful Graph Shell
TinkerPop
TinkerPop
・Must read to understand a philosophy of Tinkerpop“Applying Graph Analysis and Manipulation to Data Stores.”
TinkerPop
Blueprints: provide a common API for many GraphDBs
・Ex. Neo4j, OrientDB, DEX, InfiniteGraph, Sones, HyperGraphDB...
・The Goal is to avoid vendor lock-in
・Any tool written to the Blueprints API can work over various graph database providers
・Analogous to the JDBC, but for GraphDB
・Free to use under a liberal BSD license
・[Sail Ouplementation] GraphDB --> RDF store
・[Sail Implementation] RDF store --> GraphDB- Sail is an RDF triple/quad store interface developed by OpenRDF
・[JUNG Ouplementation] GraphDB --> JUNG graph- JUNG(Java Universal Network/Graph Framework) is a library that was developed for in-memory graph structures
・[GraphML Reader and Writer Library] Support for GraphML reading and writing
- GraphML is a comprehensive and easy-to-use file format for graphs based on XML
・TinkerGraph: Lightweight, POJO based, in-memory property graph
・Handy use with GraphML reader and writer library// An Exapmle of TinkerGraph
Graph graph = new TinkerGraph();
Vertex a = graph.addVertex(null);
Vertex b = graph.addVertex(null);
a.setProperty("name", "marko");
b.setProperty("name", "peter");
Edge e = graph.addEdge(null, a, b, "knows");
// marko--knows-->peter
https://github.com/tinkerpop/blueprints/wiki/Code-Examples
public void testIteratingGraph() {
Graph graph = TinkerGraphFactory.createTinkerGraph();
System.out.println("Vertices of " + graph);
for (Vertex vertex : graph.getVertices()) {
System.out.println(vertex);
}
System.out.println("Edges of " + graph);
for (Edge edge : graph.getEdges()) {
System.out.println(edge);
}
}Vertices of tinkergraph[vertices:6 edges:6]
v[3]
v[2]
...
Edges of tinkergraph[vertices:6 edges:6]
e[10][4-created->5]
e[7][1-knows->2]
... https://github.com/tinkerpop/blueprints/wiki/Code-Examples
・Rexster: Multi-faceted graph server using Jersey (deployable through Tomcat)
- Provides flexible methods not only GET, POST, DELETE but also Gremlin
・Extentions support of standard traversal goals- such as search, score, rank, and, in concert, recommendation
・The Dog House provides browser based interface - Vertex and Edge Browser: View vertices and edges for a selected graph
- Gremlin Console: Simulates a Gremlin console session against a selected graph
・Rexster Console is a REPL similar to the REPL available from Gremlin.
The Dog House
・Mapping a URI to JSON[input] Parametrized URI request
[output] JSON formatted response{
"a" : 1,
"b" : {
"a" : "marko",
"b" : true,
"c" : {
"a" : "peter"
}
}
"c" : ["marko","povel"]
}
http://localhost:8182/graphs/toygraph/toy-
traversal?
a=1&b.a=marko&b.b=true&b.c.a=peter&c=[mark
o,povel]
↑ Input (URI)→ Output (Json)
curl -sX GET "http://aHost:8182/graphs/neo4jsample/indices/vertices?key=ID&value=52"
{
"version": "0.4-SNAPSHOT",
"results": [
{
"_id": 149,
"_type": "vertex",
"Name": "King",
"Type": "Card",
"ID": "52"
}
],
"totalSize": 1,
"queryTime": 3.876749
}
https://github.com/martinhbramwell/Rexster-Stuff
Basic Rest API[GET]
[POST]...
...
https://github.com/tinkerpop/rexster/wiki/Basic-REST-API
~$ gremlin
\,,,/
(o o)
-----oOOo-(_)-oOOo-----
gremlin>
・Gremlin: Graph Traversal Language
・Property Graph
・Using Groovy and Pipes to perform complex graph traversals.
・Gremlin console like a file system prompt
・Connectors, provided by Blueprints
- TinkerGraph, Neo4j, OrientDB, DEX, Rexster, Sesame 2.0
https://github.com/tinkerpop/gremlin/wiki/Gremlin-Steps
Gremlin Steps
https://github.com/tinkerpop/gremlin/wiki/Gremlin-Methods
Gremlin Methods
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> v = g.v(1)
==>v[1]
gremlin> v.outE
==>e[7][1-knows->2]
==>e[9][1-created->3]
==>e[8][1-knows->4]
gremlin> v.outE.inV
==>v[2]
==>v[3]
==>v[4]
Code Exapmles
https://github.com/tinkerpop/gremlin/wiki/Basic-Graph-Traversals
# vertex jump
gremlin> v.outE.inV.outE.inV
==>v[5]
==>v[3]
# shortcut
gremlin> v.out.out
==>v[5]
==>v[3]
# using filter
gremlin> v.outE.filter{it.label=='knows'}.inV.filter{it.age >
30}.name
==>josh
# backtracking and an in-line regular expression
gremlin> v.out('knows').filter{it.age >
21}.name.filter{it.matches('jo.{2}|JO.{2}')}.back(3).age
==>32 https://github.com/tinkerpop/gremlin/wiki/Basic-Graph-Traversals
# return path
gremlin> g.v(1).outE.inV.name.paths
==>[v[1], e[7][1-knows->2], v[2], vadas]
==>[v[1], e[9][1-created->3], v[3], lop]
==>[v[1], e[8][1-knows->4], v[4], josh]
gremlin> g.v(1).outE.inV.paths{it.name}{it.weight}{it.name}
==>[marko, 0.5, vadas]
==>[marko, 0.4, lop]
==>[marko, 1.0, josh]https://github.com/tinkerpop/gremlin/wiki/Basic-Graph-Traversals
# loop
gremlin> g.v(89).outE.inV.paths
==>[v[89], e[7021][89-followed_by->83], v[83]]
==>[v[89], e[7022][89-followed_by->21], v[21]]
==>[v[89], e[7006][89-followed_by->127], v[127]]
...
gremlin> g.v(89).outE.inV.loop(2){it.loops < 3}.paths
==>[v[89], e[7021][89-followed_by->83], v[83], e[1411][83-followed_by->13],
v[13]]
==>[v[89], e[7021][89-followed_by->83], v[83], e[1410][83-followed_by->12],
v[12]]
==>[v[89], e[7021][89-followed_by->83], v[83], e[1415][83-followed_by->114],
v[114]]
==>[v[89], e[7021][89-followed_by->83], v[83], e[1414][83-followed_by->15],
v[15]]
...
gremlin> g.v(89).outE.inV.loop(2){it.loops < 3} == g.v(89).outE.inV.outE.inV
==>true
https://github.com/tinkerpop/gremlin/wiki/Loop-Pattern
2 step away from node89
loop 11step 2step
・transform: Given an object of type S, emit an object of type E
・filter: Given an object of type S, emit it or not
・sideEffect: Given an object of type S, emit it, but yield some computational side-effect in the process
Pipes: Dataflow framework using process graphs
On the Nature of Pipes
[Transform Pipes]
gremlin> g.v(1).out.name
==>vadas
==>lop
==>josh
・Pipes is a lazy evaluation framework, the mappings are computing as needed
gremlin>
g.v(1).out.name.paths
==>[v[1], v[2], vadas]
==>[v[1], v[3], lop]
==>[v[1], v[4], josh]
On the Nature of Pipes
[Filter Pipes]
gremlin> g.v(1).out('knows')
==>v[2]
==>v[4]
gremlin> g.v(1).out('knows').filter{it.age < 30}
==>v[2]
gremlin> g.v(1).out('knows').filter{it.age < 30}.name
==>vadas
gremlin> g.v(1).out('knows').filter{it.age <
30}.name.transform{it.length()}
==>5 On the Nature of Pipes
[Side Effect Pipes]
gremlin> g.v(1).out('knows')
==>v[2]
==>v[4]
gremlin> g.v(1).out('knows').filter{it.age < 30}
==>v[2]
gremlin> g.v(1).out('knows').filter{it.age < 30}.name
==>vadas
gremlin> g.v(1).out('knows').filter{it.age <
30}.name.transform{it.length()}
==>5 On the Nature of Pipes
[Branch Pipes]
gremlin> g.v(1).out('knows').ifThenElse{it.age < 30}
{it.name}{it.out('created').name}
==>vadas
==>ripple
==>lop
On the Nature of Pipes
[Meta Back Pipes]
gremlin> g.v(1).out('knows').name
==>vadas
==>josh
gremlin> g.v(1).out('knows').name.filter{it[0]=='v'}
==>vadas
gremlin>
g.v(1).out('knows').name.filter{it[0]=='v'}.back(2)
==>v[2]
Meta Pipes: “wraps” another pipe
On the Nature of Pipes
gremlin>
g.v(1).out('knows').name.filter{it[0]=='v'}.back(2)
==
g.v(1).out('knows').as('here').name.filter{it[0]=='v'}.back('here')
[Meta Back Pipes]
On the Nature of Pipes
[Meta Loop Pipes]
gremlin> g.v(1).out.loop(1){it.loops < 3}
==>v[5]
==>v[3]
loop 1 loop 2
1 step away from node1
loop condition
On the Nature of Pipes
・Tinkerpop merges graph database ecosystem
・TinkerPop is based on the low-level Blueprints API
・Neo4j, OrientDB, DEX, RDF Sail, TinkerGraph, and Rexster are supported. (Infinite Graph near future)
・HBase (GraphBase) and Redis (Blueredis) are developed by a community
・Gremlin is a data flow graph processing using pipes
Summary