An Introduction to Tinkerpop

32,696 views
32,725 views

Published on

Published in: Technology

An Introduction to Tinkerpop

  1. 1. // An Exapmle of TinkerGraphGraph 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
  2. 2. 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]...
  3. 3. { "a" : 1,http://localhost:8182/graphs/toygraph/toy- "b" : {traversal? "a" : "marko",a=1&b.a=marko&b.b=true&b.c.a=peter&c=[mark "b" : true,o,povel] "c" : { "a" : "peter" } } "c" : ["marko","povel"] }
  4. 4. 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}
  5. 5. ~$ gremlin ,,,/ (o o)-----oOOo-(_)-oOOo-----gremlin>
  6. 6. 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]
  7. 7. # vertex jumpgremlin> v.outE.inV.outE.inV==>v[5]==>v[3]# shortcutgremlin> v.out.out==>v[5]==>v[3]# using filtergremlin> v.outE.filter{it.label==knows}.inV.filter{it.age >30}.name==>josh# backtracking and an in-line regular expressiongremlin> v.out(knows).filter{it.age >21}.name.filter{it.matches(jo.{2}|JO.{2})}.back(3).age==>32
  8. 8. # return pathgremlin> 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]
  9. 9. # loopgremlin> 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
  10. 10. gremlin> g.v(1).out.name gremlin>==>vadas g.v(1).out.name.paths==>lop ==>[v[1], v[2], vadas]==>josh ==>[v[1], v[3], lop] ==>[v[1], v[4], josh]
  11. 11. 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==>vadasgremlin> g.v(1).out(knows).filter{it.age <30}.name.transform{it.length()}==>5
  12. 12. 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==>vadasgremlin> g.v(1).out(knows).filter{it.age <30}.name.transform{it.length()}==>5
  13. 13. gremlin> g.v(1).out(knows).ifThenElse{it.age < 30}{it.name}{it.out(created).name}==>vadas==>ripple==>lop
  14. 14. gremlin> g.v(1).out(knows).name==>vadas==>joshgremlin> g.v(1).out(knows).name.filter{it[0]==v}==>vadasgremlin>g.v(1).out(knows).name.filter{it[0]==v}.back(2)==>v[2]
  15. 15. 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)
  16. 16. gremlin> g.v(1).out.loop(1){it.loops < 3}==>v[5]==>v[3]

×