图数据库查询语言Cypher、Gremlin和SPARQL

学习:https://blog.csdn.net/wzwdcld/article/details/80783782

首先需要区分清除的概念:图数据库、图查询语言。

图数据库:以图的形式组织和存储数据
图数据库是一种用于用于语义检索的使用图结构包括节点、边和属性来表示和存储数据的数据库。其中的关键概念是图,其直接关联了被存储的数据项。这种关联关系允许数据被直接关联,在很多情况下只需要一步操作就可以抽取出来。
传统关系数据库,管理数据的时候可以不需要实现物理记录链。例如,数据之间的链接被保存在逻辑层,关联操作可以把数据组织起来。

In computing, a graph database (GDB[1]) is a database that uses graph structures for semantic queries with nodes, edges and properties to represent and store data. A key concept of the system is the graph (or edge or relationship), which directly relates data items in the store. The relationships allow data in the store to be linked together directly, and in many cases retrieved with one operation.
This contrasts with relational databases that, with the aid of relational database management systems, permit managing the data without imposing implementation aspects like physical record chains; for example, links between data are stored in the database itself at the logical level, and relational algebra operations (e.g. join) can be used to manipulate and return related data in the relevant logical format. The execution of relational queries is possible with the aid of the database management systems at the physical level (e.g. using indexes), which permits boosting performance without modifying the logical structure of the database.https://en.wikipedia.org/wiki/Graph_database

图数据库一个典型的应用场景是知识图谱,知识图谱可以简单的理解为对实体关系的描述,使用图数据库来存储比传统使用SQL要快好多倍。SQL中复杂的关联关系需要多次join操作,不够高效。

知识图谱 https://zhuanlan.zhihu.com/p/38891715

图查询语言: 和图数据库交互的查询语言,


SQL:数据以表形式存在,有比较强的schema定义,表间的数据关联以联接(join)的方式实现。这是一种事实标准,大部分人都想把其它问题也转换回SQL或类SQL的方式上来。

SPARQL:面向RDF(Resource Description Framework)的三元组数据,W3C标准,无schema,在研究中应用非常广泛。SPARQL的查询与RDF是一致的,RDF是图,SPARQL查询是子图匹配。

Gremlin:数据以属性图的形式存在,可以认为是上面两种的混合体,属性仍然在表中,但是联接关系是直接以链接(比如指针)的形式存在的。查询的本质是图遍历,擅长解决求图的直径、点到点之间的路径,比如刘德华连接奥巴马需要几度关系。

Cypher: Neoj4数据库使用的图查询语言


1、SQL的问题
这里涉及到一个问题,属性值并不总是单一值(List),而SQL表是有这个假设的(比如MySQL,PostgreSQL是有Array类型的)。针对每一个多值属性都需要进行额外拆表,这对表的管理带来了巨大挑战。查询时频繁地进行多表联接对数据库性能也是个挑战。另外,也可以直接在关系型数据库中存储三元组,但是查询效率并不高。

2、SPARQL
Free Schema的方式,在使用Gremlin时可能需要创建的很多表,造成使用不便,效率低下,可以考虑这种方式,例如知识图谱的应用场景


Gremlin使用介绍:http://tinkerpop.apache.org/(包括了其他图查询产品的信息)

1、查询gremlin的朋友的朋友的姓名,查询很直观:通过has 选择gremlin,out表示关系朋友,因为两层关系,所以有两个,values表示返回的结果属性

    // What are the names of Gremlin's friends' friends?
    g.V().has("name","gremlin").
      out("knows").out("knows").values("name")

2、由两个人创建的项目,且这两个人是朋友,可以理解为子图匹配的过程:match操作描述了子图的特征,select选择子图顶点,by选择返回的结果,as应该是类似sql的as,in表示入度,count表示个数,is判断个数为2

 // What are the names of projects that were created by two friends?
    g.V().match(
      as("a").out("knows").as("b"),
      as("a").out("created").as("c"),
      as("b").out("created").as("c"),
      as("c").in("created").count().is(2)).
        select("c").by("name")

3、gremlin到ceo,路径上的人名:has,根据name选择 gremlin,repeat util,重复查找入度关系为manage,也就是查找上级顶点,直到顶点的tile为ceo,path返回访问的路径,by选择返回的属性

// What are the names of the managers in
    //  the management chain going from Gremlin to the CEO?
    g.V().has("name","gremlin").
      repeat(in("manages")).until(has("title","ceo")).
      path().by("name")

4、Gremli的合作者的职位名称的分布:has选择gremlin,as方便下文引用,out根据create关系找到其创办的项目,in选择创办该项目的人,where neq排除a,groupCount分组计算,by指定了分组计算的属性,返回的结果应该是tile:count

  // What is the distribution of job titles amongst Gremlin's collaborators?
    g.V().has("name","gremlin").as("a").
      out("created").in("created").
        where(neq("a")).
      groupCount().by("title")

5、给出他的购买历史,找出和gremlin最相关的产品的排名:has选择gremlin,out选择买的产品节点,aggregate聚合之前购买的商品,in选择购买这些商品的用户,out选择这些用户购买的商品,where排除掉已经股买的商品,也就是刚才聚合的结果stach,然后分组计算,本地排序,根据根据values应该指的是个数??

  // Get a ranking of the most relevant products for Gremlin given his purchase history.
    g.V().has("name","gremlin").out("bought").aggregate("stash").
      in("bought").out("bought").
        where(not(within("stash"))).
      groupCount().
        order(local).by(values,desc)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值