当前位置: 首页 > article >正文

【图数据库实战】gremlin语法

    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


http://www.kler.cn/news/137100.html

相关文章:

  • SimpleLive 1.7.3 | TV+手机,聚合抖B虎鱼四大直播
  • 基于深度学习的声纹识别
  • 九种排序,一次满足
  • nginx编译安装配置选项详解
  • MYSQL OPTIMIZE TABLE 命令重建表和索引
  • 期货交易程序化,哪些API可供选择及如何使用?
  • c# IEnumerable--扩展方法
  • SD-WAN技术:重新定义网络连接方式
  • less相关
  • 基于STC12C5A60S2系列1T 8051单片机的模数芯片ADC0832实现模数转换应用
  • 【开发流程】持续集成、持续交付、持续部署
  • Android 13.0 Launcher3仿ios长按app图标实现抖动动画开始拖拽停止动画
  • Hibernate查询的方法
  • 维基百科文章爬虫和聚类【二】:KMeans
  • py Selenium来启动多个浏览器窗口或标签页,并操作它们
  • 回顾以前的java
  • 泗博MODBUS转PROFINET网关助力电子天平与西门子PLC无缝对接
  • 679 - Dropping Balls (UVA)
  • vue3定时器的清除
  • (论文阅读51-57)图像描述3 53
  • 【django+vue】连接数据库、登录功能
  • java中stream常用api介绍
  • 鸿蒙原生应用/元服务开发-AGC分发如何配置版本信息(上)
  • Python try except 用法
  • Linux ps -ef|grep去除 grep --color=auto信息
  • windows对话框