用 Cypher 来查询图数据库

Table of Contents

Cyper

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

Cypher is unique because it provides a visual way of matching patterns and relationships.

Cypher was inspired by an ASCII-art type of syntax where (nodes)-[:ARE_CONNECTED_TO]->(otherNodes) using rounded brackets for circular (nodes), and -[:ARROWS]-> for relationships.

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

When you write a query, you draw a graph pattern through your data.

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

file

如何表示节点

圆括号表示节点, 其中节点的标签 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}]->

file

Patterns in Cypher

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

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

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

Practice

  • 用 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 - includes Memgraph database, command-line interface mgconsole and MAGE graph algorithms library. If tagged with cuGraph, it also includes NVIDIA cuGraph GPU-powered graph algorithms.

  • memgraph/lab - includes a web interface Memgraph Lab that helps you explore the data stored in 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;

Reference

Comments |0|

Legend *) Required fields are marked
**) You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Category: 似水流年