【图数据库实战】gremlin语法

    Gremlin 是 Apache TinkerPop 的图遍历语言。Gremlin 是一种函数式数据流语言,使用户能够简洁地表达对其应用程序属性图的复杂遍历(或查询)。每个 Gremlin 遍历都由一系列(可能嵌套的)步骤组成。步骤对数据流执行原子操作。每个步骤都是映射步骤(转换流中的对象)、过滤步骤(从流中删除对象)或副作用步骤(计算有关流的统计信息)。Gremlin 步骤库扩展了这 3 个基本操作,为用户提供了丰富的步骤集合,他们可以编写这些步骤,以便询问他们可能对数据有任何可能的问题,因为 Gremlin 是图灵完备的。
    

1、安装

  1)下载  Apache Download Mirrors
      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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农丁丁

你的认可是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值