logo

NLP-Neo4j

wangzf / 2022-04-05


目录

Neo4j 介绍

Neo4j 是什么

Neo4j 的特点

Neo4j 的优点

Neo4j 的缺点、限制

Neo4j 构建模块

Neo4j 概念详解

Neo4j 关系

Neo4j 图数据库遵循属性图模型来存储和管理其数据。根据属性图模型, 关系应该是定向的, 否则, Neo4j 将抛出一个错误消息。基于方向性, Neo4j 关系被分为两种类型:

1.使用新节点创建关系

CREATE (<node1-name>:<node1-label-name>)-[<relationship-name>:<relationship-label-name>]->(<node2-name>:<node2-label-name>)
CREATE (<node1-name>:<node1-label-name>)<-[<relationship-name>:<relationship-label-name>]-(<node2-name>:<node2-label-name>)
CREATE (<node1-name>:<node1-label-name>)<-[<relationship-name>:<relationship-label-name>]->(<node2-name>:<node2-label-name>)
CREATE (e:Employee)-[r:DemoRelation]->(c:Employee)
CREATE (e:Employee)<-[r:DemoRelation]-(c:Employee)
CREATE (e:Employee)<-[r:DemoRelation]->(c:Employee)

2.使用已知节点创建带属性的关系

MATCH (<node1-name>:<node1-label-name>),(<node2-name>:<node2-label-name>)
CREATE
    (<node1-name>)-[<relationship-name>:<relationship-label-name>{<define-properties-list>}]->(<node2-name>)
RETURN <relationship-name>
MATCH (cust:Customer), (cc.CreditCard)
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014", price:55000}]->(cc)
RETURN r

3.检索关系节点的详细信息

MATCH (<node1-name>)-[<relationship-name>:<relationship-label-name>]->(<node2-name>)
RETURN <node1-name>, <relationship-name>, <node2-name>
MATCH (cust)-[r:DO_SHOPPING_WITH]->(cc)
RETURN cust, cc, r

4.示例

CREATE (e:Customer{id:"1001", name:"Abc", dob:"01/10/1982"})
CREATE (cc:CreditCard{id:"5001", number:"1234567890", cvv:"888", expiredate:"20/17"})

MATCH (cust:Customer), (cc:CreditCard)
CREATE (cust)-[r1:DO_SHOPPING_WITH{shopdate:"12/12/2014", price:55000}]->(cc)
RETURN r1

MATCH (cust:Customer), (cc:CreditCard)
CREATE (e)<-[r2:ASSOCIATED_WITH]-(cc)
RETURN r2

MATCH (cc:CreditCard) RETURN cc.id, cc.number, cc.cvv, cc.expiredate

Neo4j 安装、使用

Neo4j 安装

Neo4j 使用

Neo4j CQL

CQL 简介

Cypher 是 Neo4j 的声明式图形查询语言, 允许用户不必编写图形结构的遍历代码, 就可以对图形数据进行高效的查询。Cypher 的设计目的类似 SQL, 适合于开发者以 及在数据库上做点对点模式(ad-hoc)查询的专业操作人员。其具备的能力包括:

Neo4j CQL 代表 Neo4j Cypher Query Language

类似 MySQL 一样, 在实际的生产应用中, 除了简单的查询操作会在 Neo4j 的 web 页面进行外, 一般还是使用 Python、Java 等的 driver 来在程序中实现

CQL 命令关键字

CQL命令 用法
CREATE 创建节点、关系、属性
MATCH 检索节点、关系、属性数据
RETURN 返回查询结果
WHERE 提供条件过滤检索数据
DELETE 删除节点、关系
REMOVE 删除节点、关系的属性
ORDER BY 排序检索数据
SET 添加或更新标签

CQL 函数

定制列表功能 用法
String 用于使用 String 字面量
Aggregation 用于对 CQL 查询结果执行一些聚合操作
Relationshop 用于获取关系的细节, startnode, endnode等

CQL 数据类型

CQL 数据类型 用法
boolean 用于表示布尔文字: true, false
byte 用于表示8位整数
short 用于表示16位整数
int 用于表示32位整数
long 用于表示64位整数
float I用于表示32位浮点数
double 用于表示64位浮点数
char 用于表示16位字符
String 用于表示字符串

CQL 命令

CREATE

1.创建没有属性的节点

创建 节点标签名称, 相当于 MySQL 数据库中的表名

CREATE (<node-name>:<node-label-name>)
CREATE (emp:Employee)

CREATE (dept:Dept)

2.创建具有属性的节点

创建一个具有一些属性(键-值对)的节点来存储数据

CREATE (
    <node-name>:<node-label-name> {
        <Property1-name>:<Property1-Value>,
        <Property2-name>:<Property2-Value>,
        ...,
        <Propertyn-name>:<Propertyn-Value>
    }
)
CREATE (
    emp:Employee {
        id: 123,
        name: "Lokesh",
        sal: 35000,
        deptno: 10
    }
)

CREATE (
    dept:Dept {
        deptno: 10,
        dname: "Accounting",
        location: "Hyderabad"
    }
)

3.创建多个标签的节点

CREATE (<node-name>:<node-label-name1>:<label-name2>...:<label-namen>)
CREATE (m:Movie:Cinema:Film:Picture)

CREATE…MATCH…RETURN

MATCH (<node-name>:<node-label-name>)
RETURN
    <node-name>.<property1-name>,
    <node-name>.<property1-name>,
    ...,
    <node-name>.<property1-name>
MATCH (dept:Dept) // 会报错
MATCH (dept:Dept) RETURN dept
MATCH (dept:Dept) RETURN dept.deptno, dept.dname, dept.location
MATCH (e:Employee) RETURN e
MATCH (p:Employee {id:123, name:"Lokesh"}) RETURN p
MATCH (p:Employee) WHERE p.name = "Lokesh" RETURN p

WHERE

Neo4j CQL WHERE 过滤 MATCH 查询的结果

WHERE <condition>
WHERE <property-name> <comparison-operator> <value>
WHERE <condition> <boolean-operator> <condition>

其中:

使用 WHERE 子句创建关系

MATCH (<node1-name>:<node1-label-name>),(<node2-name>:<node2-label-name>)
WHERE <condition>
CREATE (<node1-name>)-[<relationship-name>:<relationship-label-name>{<relationship-properties>}]->(<node2-name>)
MATCH (cust:Customer)
RETURN cust.id, cust.name, cust.dob

MATCH (cc:CreditCard)
RETURN cc.id, cc.number, cc.expiredate, cc.cvv

MATCH (cust:Customer), (cc.CreditCard)
WHERE cust.id = "1001" AND cc.id = "5001"
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014", price:55000}]->(cc)
RETURN r
MATCH (emp:Employee)
RETURN emp.empid, emp.name, emp.salary, emp.deptno

MATCH (emp:Employee)
WHERE emp.name = "Abc"
RETURN emp

MATCH (emp:Employee)
WHERE emp.name = 'Abc' OR emp.name = 'Xyz'
RETURN emp

MATCH (cust:Customer), (cc:CreditCard)
WHERE cust.id = '1001' AND cc.id = '5001'
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014", price:55000}]->(cc)
RETURN r

MATCH p = (m:Bot{id:123})<-[:BotRelation]->(:Bot) 
RETURN p

DELETE

Neo4j 使用 CQL DELETE 用来:

1.删除节点

DELETE <node-name-list>

DELETE <node1-name>, <node2-name>, <relationship-name>
MATCH (e:Employee) 
RETURN e

MATCH (e:Employee) 
DELETE e

MATCH (e:Employee) 
RETURN e

MATCH (cc:CreditCard)-[r]-(c:Customer)
RETURN r 

MATCH (cc:CreditCard)-[rel]-(c:Customer)
DELETE cc, c, rel

2.删除节点和关系

DELETE <node-name1>, <node-name2>, <relationship-name>
MATCH (cc:CreditCard)-[rel]-(c:Customer)
DELETE cc, c,rel

REMOVE

Neo4j CQL REMOVE 删除节点或关系的现有属性

// 删除节点/关系的属性
REMOVE <property-name-list>

REMOVE
    <node-name>.<property1-name>,
    <node-name>.<property2-name>, 
    .... 
    <node-name>.<propertyn-name> 

// 删除节点/关系的标签
REMOVE <label-name-list>

REMOVE 
    <node-name>:<label2-name>, 
    .... 
    <node-name>:<labeln-name> 
CREATE (
    book:Book {
        id:122,
        title:"Neo4j Tutorial",
        pages:340,
        price:250
    }
)

MATCH (book:Book)
RETURN book

MATCH (book {id:122})
REMOVE book.price
RETURN book

MATCH (dc:DebitCard)
RETURN dc

MATCH (dc:DebitCard)
REMOVE dc.cvv
RETURN dc
MATCH (m:Movie)
RETURN m

MATCH (m:Movie) 
REMOVE m:Picture

MATCH (m:Movie) 
RETURN m

SET

Neo4j CQL 已提供 SET 子句来执行以下操作:

SET <property-name-list>

SET
    <node-label-name>.<property1-name>,
    <node-label-name>.<property2-name>, 
    .... 
    <node-label-name>.<propertyn-name> 
MATCH (book:Book)
RETURN book

MATCH (book:Book)
SET book.title = "superstar" 
RETURN book

ORDER BY

Neo4j CQL ORDER BY 对 MATCH 查询返回的结果进行排序

ORDER BY <property-name-list> [DESC]

ORDDR BY
    <node-label-name>.<property1-name> [DESC],
    <node-label-name>.<property2-name> [DESC], 
    .... 
    <node-label-name>.<propertyn-name> [DESC]
MATCH (emp:Employee) 
RETURN emp.empid, emp.name, emp.salary, emp.deptno

MATCH (emp:Employee) 
RETURN emp.empid, emp.name, emp.salary, emp.deptno
ORDER BY emp.name

Sorting

UNION

Neo4j CQL 与 SQL 一样, 有两个语句可以将两个不同的结果合并成一组结果

UNION

MATCH Command
UNION/UNION ALL
MATCH Command
MATCH (cc.CreditCard) RETURN cc.id, cc.number
UNION/UNION ALL
MATCH (dc.DebitCard) RETURN dc.id, dc.number

MATCH (cc.CreditCard)
RETURN cc.id as id, cc.number as number, cc.name as name, cc.valid_from as valid_from, cc.valid_to as valid_to
UNION/UNION ALL
MATCH (dc.DebitCard)
RETURN dc.id as id, dc.number as number, dc.name as name, dc.valid_from as valid_from, dc.valid_to as valid_to

UNION ALL

LIMIT、SKIP

LIMIT

LIMIT <number>
MATCH (emp:Employee) 
RETURN emp

MATCH (emp:Employee) 
RETURN emp
LIMIT 2

SKIP

SKIP <number>
MATCH (emp:Employee) 
RETURN emp

MATCH (emp:Employee) 
RETURN emp
SKIP 2

MERGE

MERGE (<node-name>:<label-name> {
    <Property1-name>:<Pro<rty1-Value>
    .....
    <Propertyn-name>:<Propertyn-Value>
})

CREATE (gp1:GoogleProfile1 {Id:201401, Name:"Apple"})
CREATE (gp1:GoogleProfile1 {Id:201401, Name:"Apple"})
MATCH  (gp1:GoogleProfile1) 
RETURN gp1.Id, gp1.Name

MERGE (gp2:GoogleProfile2 {Id:201402, Name:"Nokia"})
MERGE (gp2:GoogleProfile2 {Id:201402, Name:"Nokia"})
MATCH  (gp2:GoogleProfile2) 
RETURN gp2.Id,g p2.Name

NULL

MATCH (e:Employee) 
RETURN e.id, e.name, e.sal, e.deptno

CREATE (e:Employee)

MATCH (e:Employee) 
RETURN e.id, e.name, e.sal, e.deptno

MATCH (e:Employee) 
WHERE e.id IS NOT NULL
RETURN e.id, e.name, e.sal, e.deptno

MATCH (e:Employee) 
WHERE e.id IS NULL
RETURN e.id, e.name, e.sal, e.deptno

IN

IN [<Collection-of-values>]
MATCH (e:Employee) 
RETURN e.id, e.name, e.sal, e.deptno

MATCH (e:Employee) 
WHERE e.id IN [123,124]
RETURN e.id, e.name, e.sal, e.deptno

图形字体

ID 属性

在 Neo4j 中, Id 是节点和关系的默认内部属性. 这意味着, 当我们创建一个新的节点或关系时, Neo4j 数据库服务器将为内部使用分配一个数字。它会自动递增

以相同的方式, Neo4j 数据库服务器为关系分配一个默认 Id 属性

Caption

在 Neo4j 数据中, 当我们在 Neo4j DATA 浏览器中执行 MATCH + RETURN 命令以查看 UI 视图中的数据时, 通过使用它们的 Id 属性显示节点和/或关系结果。它被称为 CAPTIONid 属性

CQL 函数

Neo4j CQL 字符串函数

Neo4J CQL 提供了一组 String 函数, 用于在 CQL 查询中获取所需的结果

Neo4j CQL AGGREGATION 聚合函数

Neo4j CQL 提供了一些在 RETURN 子句中使用的聚合函数

Neo4j CQL 关系函数

Neo4j CQL 提供了一组关系函数, 以在获取开始节点, 结束节点等细节时知道关系的细节

Admin 管理员

Neo4j 数据库备份和恢复

Neo4j CQL 索引

Neo4j CQL UNIQUE 约束

Neo4j CQL DROP UNIQUE

Neo4j 实战

本文通过一个实际的案例来一步一步教你使用 Cypher 来操作 Neo4j。 案例的节点主要包括人物和城市两类, 人物和人物之间有朋友、夫妻等关系, 人物和城市之间有出生地的关系

  1. 首先, 我们删除数据库中以往的图, 确保一个空白的环境进行操作
MATCH (n) DETACH DELETE n
  1. 创建人物节点
CREATE (n:Person {name:"John"}) RETURN n
CREATE (n:Person {name:'Sally'}) RETURN n
CREATE (n:Person {name:'Steve'}) RETURN n
CREATE (n:Person {name:'Mike'}) RETURN n
CREATE (n:Person {name:'Liz'}) RETURN n
CREATE (n:Person {name:'Shawn'}) RETURN n
  1. 创建地区节点
CREATE (n:Location {city:'Miami', state:'FL'})
CREATE (n:Location {city:'Boston', state:'MA'})
CREATE (n:Location {city:'Lynn', state:'MA'})
CREATE (n:Location {city:'Portland', state:'ME'})
CREATE (n:Location {city:'San Francisco', state:'CA'})
  1. 创建人物之间的关系
MATCH (a:Person {name:"Shawn"}), (b:Person {name:"Sally"})
MERGE (a)-[:FRIENDS {since:2001}]->(b)

MATCH (a:Person {name:'Shawn'}), (b:Person {name:'John'}) 
MERGE (a)-[:FRIENDS {since:2012}]->(b)

MATCH (a:Person {name:'Mike'}), (b:Person {name:'Shawn'}) 
MERGE (a)-[:FRIENDS {since:2006}]->(b)

MATCH (a:Person {name:'Sally'}), (b:Person {name:'Steve'}) 
MERGE (a)-[:FRIENDS {since:2006}]->(b)

MATCH (a:Person {name:'Liz'}), (b:Person {name:'John'}) 
MERGE (a)-[:MARRIED {since:1998}]->(b)
  1. 创建人物-地区之间的关系
MATCH (a:Person {name:"John"}), (b:Location {city:"Boston"})
MERGE (a)-[:BORN_IN {year:1978}]->(b)

MATCH (a:Person {name:'Liz'}), (b:Location {city:'Boston'}) 
MERGE (a)-[:BORN_IN {year:1981}]->(b)

MATCH (a:Person {name:'Mike'}), (b:Location {city:'San Francisco'}) 
MERGE (a)-[:BORN_IN {year:1960}]->(b)

MATCH (a:Person {name:'Shawn'}), (b:Location {city:'Miami'}) 
MERGE (a)-[:BORN_IN {year:1960}]->(b)

MATCH (a:Person {name:'Steve'}), (b:Location {city:'Lynn'}) 
MERGE (a)-[:BORN_IN {year:1970}]->(b)
  1. 查询-所有在 Boston 出生的人物
MATCH (a:Person)-[:BORN_IN]->(b:Location {city:"Boston"})
RETURN a, b
  1. 查询所有对外有关系的节点
MATCH (a)-->()
RETURN a
  1. 查询所有有关系的节点
MATCH (a)--() 
RETURN a
  1. 查询所有对外有关系的节点, 以及关系类型
MATCH (a)-[r]->()
RETURN a.name, type(r)
  1. 查询所有有结婚关系的节点
MATCH (n)-[:MARRIED]-()
RETURN n
  1. 创建节点的时候就建好关系
CREATE (a:Person {name:"Todd"})-[r:FRIENDS]->(b:Person {name:"Carlos"})
  1. 查询某人的朋友的朋友
MATCH (a:Person {name:"Mike"})-[r1:FRIENDS]-()-[r2:FRIENDS]-(friend_of_a_friend)
RETURN friend_of_a_friend.name AS fofName
  1. 增加、修改节点的属性
MATCH (a:Person {name:'Liz'}) SET a.age=34
MATCH (a:Person {name:'Shawn'}) SET a.age=32
MATCH (a:Person {name:'John'}) SET a.age=44
MATCH (a:Person {name:'Mike'}) SET a.age=25
  1. 删除节点的属性
MATCH (a:Person {name:"Mike"})
SET a.test = "test"

MATCH (a:Person {name:"Mike"})
REMOVE a.test
  1. 删除节点
MATCH (a:Location {city:"Portland"})
DELETE a
  1. 删除有关系的节点
MATCH (a:Person {name:"Todd"})-[rel]-(b:Person)
DELETE a, b, rel
  1. 查询所有节点、关系
MATCH (n) 
RETURN n
LIMIT 25

py2neo

安装

$ pip install --upgrade py2neo

使用

from py2neo import Graph

核心 API

py2neo.database

from py2neo import Graph

graph = Graph(password = "password")
graph.run("UNWIND range(1, 3) AS n RETURN n, n * n as n_sq").to_table()

连接

参考