Gremlin 是 Apache TinkerPop 的图遍历语言。Gremlin 是一种函数式数据流语言,使用户能够简洁地表达对其应用程序属性图的复杂遍历(或查询)。每个 Gremlin 遍历都由一系列(可能嵌套的)步骤组成。步骤对数据流执行原子操作。每个步骤都是映射步骤(转换流中的对象)、过滤步骤(从流中删除对象)或副作用步骤(计算有关流的统计信息)。Gremlin 步骤库扩展了这 3 个基本操作,为用户提供了丰富的步骤集合,他们可以编写这些步骤,以便询问他们可能对数据有任何可能的问题,因为 Gremlin 是图灵完备的。
1、安装
1)下载
Apache Download Mirrors
2)解压
apache-tinkerpop-gremlin-console-3.7.0-bin.zip
3)执行命令
$ bin/gremlin.sh
\,,,/
(o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = traversal().withEmbedded(graph)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin>
2、语法试用
以内置的Mordern图数据为数据,进行gremlin语法的hello world练习。
Mordern图的数据为:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = traversal().withEmbedded(graph)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
# 1. Get all the vertices in the Graph.
gremlin> g.V()
==>v[1]
==>v[2]
==>v[3]
==>v[4]
==>v[5]
==>v[6]
# 2. Get the vertex with the unique identifier of "1".
gremlin> g.V(1)
==>v[1]
# 3. Get the value of the name property on the vertex with the unique identifier of "1".
gremlin> g.V(1).values('name')
==>marko
# 4. Get the edges with the label "knows" for the vertex with the unique identifier of "1".
gremlin> g.V(1).outE('knows')
==>e[7][1-knows->2]
==>e[8][1-knows->4]
# 5. Get the names of the people whom the vertex with the unique identifier of "1" "knows".
gremlin> g.V(1).outE('knows').inV().values('name')
==>vadas
==>josh
#6. Note that when one uses outE().inV() as shown in the previous command, this can be shortened to just out() (similar to inE().outV() and in() for incoming edges).
gremlin> g.V(1).out('knows').values('name')
==>vadas
==>josh
#7. Get the names of the people vertex "1" knows who are over the age of 30.
gremlin> g.V(1).out('knows').has('age', gt(30)).values('name')
==>josh
gremlin>
3、创建图
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = traversal().withEmbedded(graph)
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> v1 = g.addV("person").property(id, 1).property("name", "marko").property("age", 29).next()
==>v[1]
gremlin> v2 = g.addV("software").property(id, 3).property("name", "lop").property("lang", "java").next()
==>v[3]
gremlin> g.V()
==>v[1]
==>v[3]
4、图遍历
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = traversal().withEmbedded(graph)
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko')
==>v[1]
gremlin> g.V().has('person','name','marko')
==>v[1]
gremlin> g.V().has('person','name','marko').outE('created')
==>e[9][1-created->3]
gremlin> g.V().has('person','name','marko').outE('created')
==>e[9][1-created->3]
gremlin> g.V().has('person','name','marko').out('created').values('name')
==>lop
gremlin> g.V().has('person','name',within('vadas','marko')).values('age')
==>29
==>27
gremlin> g.V().has('person','name',within('vadas','marko')).values('age').mean()
==>28.0