图数据库查询语言 cypher 与 memgraph

Cyper

作为声明式查询语言, SQL 在计算机行业无人不晓, 无人不知. 而 Cypher 就是 Graph Database 图数据库的 SQL.

Cypher 用"圆括号"来表示节点, 用"方括号,连接线及箭头"表示关系

这样一句话 - "Sally likes Graphs. Sally is friends with John. Sally works for Neo4j."
表示为图数据库中的节点和关系

graph example

如何表示节点

圆括号表示节点, 其中节点的标签 label 可以用 "node:label" 来表示
例如:

()                  //anonymous node (no label or variable) can refer to any node in the database
(p:Person)          //using variable p and label Person
(:Technology)       //no variable, label Technology
(work:Company)      //using variable work and label Company

如何表示关系

有向图中的关系就是用箭头来表示的, Cypher 使用箭头 --><-- 来连接两个节点.
而没有箭头的连接线 -- 表示节点之间的关系是双向的

创建和查询 "Person" 与 "Technology" 之间关系的语句如下

//data stored with this direction 创建关系
CREATE (p:Person)-[:LIKES]->(t:Technology)

//query relationship backwards will not return results 查询关系
MATCH (p:Person)<-[:LIKES]-(t:Technology)

//better to query with undirected relationship unless sure of direction
MATCH (p:Person)-[:LIKES]-(t:Technology)

Relationship types 关系类型

关系类型可以自己定义, 推荐使用动词 (verbs and actions)
例如以下的关系类型

# 莎莉喜欢图
[:LIKES] - makes sense when we put nodes on either side of the relationship (Sally LIKES Graphs)  

#  莎莉为 neo4j 工作
[:IS_FRIENDS_WITH] - makes sense when we put nodes with it (Sally IS_FRIENDS_WITH John)  

# 莎莉为 neo4j 工作
[:WORKS_FOR] - makes sense with nodes (Sally WORKS_FOR Neo4j)

Relationship variables 关系变量

为查询方便, 可以给关系命名一个变量, 形如 [r][rel]

Node or relationship properties 节点与关系的属性

节点和关系的属性都可在节点的括号或关系的括号内使用花括号。然后,属性的名称和值放在花括号内。

例如

# 节点属性: p 是节点名, Person 是标签, 属性名是 name, 属性值是 Sally
Node property: (p:Person {name: 'Sally'})

# 关系属性: rel 是属性名, IS_FRIENDS_WITH 是标签, 属性名是 since, 属性值是 2018
Relationship property: -[rel:IS_FRIENDS_WITH {since: 2018}]->
person graph

Cypher 中的模式

在 Cypher 中的模式可能通过以上的节点, 关系和属性放在一起来表示, 以逗号分隔.

例如我们要查询模式 "Sally likes Graph", 可以这样表示

(p:Person {name: "Sally"})-[rel:LIKES]->(g:Technology {type: "Graphs"})

通过 memgraph 为启动一个内存图数据库

  • 用 docker-compose 启动如下的 memgraph
services:
  memgraph:
    image: memgraph/memgraph-mage:latest
    container_name: memgraph-mage
    ports:
      - "7687:7687"
      - "7444:7444"
    command: ["--log-level=TRACE"]
 
  lab:
    image: memgraph/lab:latest
    container_name: memgraph-lab
    ports:
      - "3000:3000"
    depends_on:
      - memgraph
    environment:
      - QUICK_CONNECT_MG_HOST=memgraph
      - QUICK_CONNECT_MG_PORT=7687

通过 docker-compose up -d 启动 memgraph mage 和 lab, 其中

  • memgraph/memgraph-mage - 包 Memgraph 数据库, 命令行接口 mgconsole 和 MAGE 图算法库.

  • memgraph/lab - 包含一个 web 界面 Memgraph Lab 以帮助我们探索存储在 Memgraph 中的数据

打开 http://localhost:3000/ 用如下 cypher 创建节点和关系

CREATE (:Country {name: 'Germany', language: 'German', continent: 'Europe'});
CREATE (:Country {name: 'France', language: 'French', continent: 'Europe'});

MATCH (c1),(c2) WHERE c1.name= 'Germany' AND c2.name = 'France'
CREATE (c2)<-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: 'John'})-
[:LIVING_IN {date_of_start: 2014}]->(c1);

MATCH (c1),(c2) WHERE c1.name= 'Germany' AND c2.name = 'France'
CREATE (c1)<-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: 'Harry'})-[:LIVING_IN {date_of_start: 2013}]->(c2);

# 查询所有的节点和关系
MATCH (n)-[r]->(m) RETURN n,r,m;
graph

Reference

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值