一、下载和初步认识
(1)下载
进入官方下载地址:Neo4j Download Center - Neo4j Graph Data Platform
(2)开始下载 & 示例数据导入
此页面有安装 Neo4j 的教程:Thanks for Downloading Neo4j - Neo4j Graph Database Platform
① 开始安装Neo4j
Windows 安装指示文档:Windows installation - Operations Manual (neo4j.com)
- 前期准备 Java 开发环境 JDK 的版本 满足 Neo4j 的要求;(neo4j-community-4.3.5 要求 JDK11/OpenJDK11的版本)
- 解压到指定目录;
- 配置Windows环境变量;
- 配置neo4j 在Windows的环境变量,类似于 JAVA_HOME 的配置方式(即 类似于 Java开发环境配置);
- 配置成功之后,使用 【neo4j console】启动 neo4j;
- 安装 neo4j 服务【neo4j install-service】;
- 可以在浏览器上访问 【http://localhost:7474/】(使用默认的用户名和密码访问 neo4j/neo4j,登入成功之后会要求修改密码,不能旧密码和新密码一样);
C:\Users\drew>neo4j
Missing required subcommand
Usage: Neo4j <COMMAND>
Neo4j database server CLI.
Commands:
console Start server in console.
start Start server as a daemon.
stop Stop the server daemon.
restart Restart the server daemon.
status Get the status of the server.
install-service Install the Windows service.
uninstall-service Uninstall the Windows service.
update-service Update the Windows service.
version, --version Print version information and exit.
help, --help Displays help information about the specified command
C:\Users\drew>neo4j console
Directories in use:
home: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5
config: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\conf
logs: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\logs
plugins: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\plugins
import: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\import
data: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\data
certificates: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\certificates
licenses: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\licenses
run: D:\neo4j\neo4j-community-4.3.5-windows\neo4j-community-4.3.5\run
Starting Neo4j.
2021-10-08 15:19:54.785+0000 INFO Starting...
2021-10-08 15:19:57.608+0000 INFO ======== Neo4j 4.3.5 ========
2021-10-08 15:19:59.115+0000 INFO Initializing system graph model for component 'security-users' with version -1 and status UNINITIALIZED
2021-10-08 15:19:59.121+0000 INFO Setting up initial user from defaults: neo4j
2021-10-08 15:19:59.121+0000 INFO Creating new user 'neo4j' (passwordChangeRequired=true, suspended=false)
2021-10-08 15:19:59.129+0000 INFO Setting version for 'security-users' to 3
2021-10-08 15:19:59.131+0000 INFO After initialization of system graph model component 'security-users' have version 3 and status CURRENT
2021-10-08 15:19:59.134+0000 INFO Performing postInitialization step for component 'security-users' with version 3 and status CURRENT
2021-10-08 15:20:00.625+0000 INFO Bolt enabled on jrebel.npegeek.com:7687.
2021-10-08 15:20:01.224+0000 INFO Remote interface available at http://localhost:7474/
2021-10-08 15:20:01.226+0000 INFO Started.
2021-10-08 15:24:44.955+0000 WARN Unsupported authentication token, missing key `principal`
2021-10-08 15:24:45.598+0000 WARN Unsupported authentication token, missing key `principal`
2021-10-08 15:24:55.396+0000 WARN The client is unauthorized due to authentication failure.
2021-10-08 15:24:56.042+0000 WARN The client is unauthorized due to authentication failure.
终止批处理操作吗(Y/N)? y
C:\Users\drew>neo4j install-service
Neo4j service installed.
C:\Users\drew>
② 打开 Neo4j 自带GUI管理工具
③ 探讨:示例数据集
④ 下载对应编程语言的程序驱动
⑤ 创建自己的数据模型
⑥ 帮助和初步使用
(3)导入数据
进入本地启动的数据之后,找到如下界面:
这里以 Movie Graph 示例,有对应的导入数据的教程。如上图显示。
① 导入官方自带示例工具(import)
方式一:导入官方的示例文件数据,每次启动neo4j需要自己手动导入一次,即临时的使用数据。
方式二:导入自己的示例数据
# 准备好 西游记.csv 文件
# 将csv拷贝到 %NEO4J_HOME%\import 目录
load csv from 'file:///西游记.csv' as line
create (:西游 {name:line[0],tail:line[1], label:line[3]})
二、初步使用
推荐入门教程: 入门教程:
- w3cschool Neo4j 教程:Neo4j - 特点和优势_w3cschool
(1)基本使用命令
C:\Users\drew>neo4j help
Usage: Neo4j <COMMAND>
Neo4j database server CLI.
Commands:
console Start server in console.
start Start server as a daemon.
stop Stop the server daemon.
restart Restart the server daemon.
status Get the status of the server.
install-service Install the Windows service.
uninstall-service Uninstall the Windows service.
update-service Update the Windows service.
version, --version Print version information and exit.
help, --help Displays help information about the specified command
说明:
$ neo4j help # 查看neo4j命令帮助
$ neo4j console # CMD命令窗口开启Neo4j服务
$ neo4j start # 以守护线程启动neo4j服务
$ neo4j stop # 关闭服务
$ neo4j restart # 重启服务
$ neo4j status # 查看neo4j服务状态
$ neo4j install-service # 安装一个Windows的neo4j服务(注意:当你想安装一个新版本的neo4j服务,需要先卸载当前版本服务 neo4j uninstall-service)
$ neo4j uninstall-service # 卸载一个Windows的neo4j服务
$ neo4j update-service # 更新一个 Windows的neo4j服务
$ neo4j version # 查看当前neo4j的版本
$ neo4j help # 或者 neo4j --help 显示指定命令的帮助信息
(2)CQL语法
Neo4j 的 Cypher语言是为处理图形数据而构建的,CQL代表 Cypher查询语言。类似于 Oracle 的 SQL 语言。
- 是 Neo4j 图形数据库的查询语言。
- 他是一种申明性模式匹配语言。
- 它遵循 SQL 语法。
- 它的语法是非常简单且人性化、可读的格式。
| CQL命令 | 用法 |
|---|---|
| create | 创建节点,关系和属性。 |
| match | 检索有关节点,关系和属性数据。 |
| return | 返回查询结果。 |
| where | 提供条件过滤检索数据。 |
| delete | 删除节点和关系。 |
| remove | 删除节点和关系的属性。 |
| order by | 排序检索数据。 |
| set | 添加或更新标签。 |
使用 cypher 语言来描述关系:
(fox)<-[:knows]-(周瑜)-[:knows]->(诸葛)-[:knows]->(fox)
(3) CQL 简单使用
- 官方使用教程:Cypher Query Language - Developer Guides (neo4j.com)
- 基本的Neo4j教程:Neo4j 备忘单_w3cschool
① 创建节点
# 创建简单节点
create(n)
# 创建多个节点
create(n),(m)
# 创建带标签和属性的节点并返回节点
create(n:person {name:'如来'}) return n
② 创建关系
neo4j 图数据库遵循属性图模型来存储和管理其数据。
根据属性图模型,关系应该是定向的。否则,neo4j 将抛出一个错误信息。
基于方向性,neo4j 关系被分为两种主要类型。
- 单向关系
- 双向关系
# 先创建节点
create(:student {name:"小明"}),(:student {name:"小红"})
# 再创建关系
match(n:student {name:"小明"}),(m:student {name: "小红"})
create (n)-[r:同学]->(m) return n.name,type(r),m.name
③ DELETE 删除操作
neo4j 使用 CQL delete 子句
- 删除节点;
- 删除节点以及相关节点和关系。
# 删除节点(前提:节点不存在关系)
match (n:person{name:'孙悟空'}) delete n
# 删除关系
match(n:person{name:"沙僧"})<-[r]-(m) delete r return type(r)
④ REMOVE 删除操作
有时候基于客户端的要求,我们需要向现有的节点或关系添加或删除属性。我们使用 neo4j CQL REMOVE 子句来删除节点或关系的现有属性。
- 删除节点或关系的标签;
- 删除节点或关系的属性;
# 删除属性
match (n:role (name:"fox")) remove n.age return n
# 创建节点
create (m:role:person {name:"fox666"})
# 删除标签
match (m:role:person {name:"fox666"}) remove m:person return m
⑤ SET 子句
有时,根据我们客户的要求,我们需要向现有节点或关系添加新属性。
- 向现有节点或关系添加新属性;
- 添加或更新属性值。
match(n:role {name:"fox"}) set n.age=32 return n
⑥ ORDER BY 排序
对结果排序,默认升序。
match(n:"西游") return id(n),n.name order by id(n) desc
⑦ LIMIT / SKIP
限制输出多少条数据LIMIT,以及跳过多少条数据SKIP
match (n:"西游") return n skip 2 limit 10
⑧ 索引 INDEX
neo4j SQL 支持节点或关系属性上的索引,以提高应用程序的性能。 我们可以为具有相同标签名的所有节点的属性创建索引。 我们可以在 match 或 where 或 in 运算上使用这些索引列来改进 CQL command 的执行。
# 常见操作:1.create index 创建索引;2.drop index 丢弃索引。
# 创建索引
create index on :"西游"(name)
# 丢弃索引
drop index on :"西游"(name)
⑨ unique 唯一约束
① 避免重复记录;② 强制执行数据完整性规则。
# 创建唯一约束 unique(在创建节点的时候,使用)
create constraint on(n:student) assert n.name is unique
# 删除唯一约束
drop constraint on (n:student) assert n.name is unique
⑩ distinct 关键字返回所有不同值
# 这个函数的用法就像 SQL 中的distinct关键字,返回的是所有不同值
match(n:"西游") return distinct(n.name)
11. 查询语句示例
match 使用手册:MATCH - Neo4j Cypher Manual
# 条件筛选(null)
match (n:student {name:"小明"}) where n.sex is not null return n
# 限制条数输出(limit)
match (n:student) return n limit 10
# in 语句
match (n:student) where n.name in["小红","小明"] return n.name
# 在 neo4j browser 浏览器控制窗口执行的
# 日期函数
return timestamp(); # 显示当前时间戳 1633843749055
return date(); # 显示当前日期年月日 "2021-10-10"
return datetime(); # 显示当前日期时间 "2021-10-10T05:28:36.521000000Z"
return time(); # 显示当前时间
# 转换函数
return toInteger("233") + 1; # 字符串转数字再累加,234
return toString(23); # 数字转字符串 “23”
return toFloat(23); # 转浮点数 23.0
return toBoolean(2); # 转布尔值,非零值为true
# 字符串函数
return toUpper("Hello"); # 全部字符大写, HELLO
return toLower("HELLO"); # 全部字符小写, hello
return reverse("hello"); # 反转字符串,olleh
return size("hello"); # 字符串长度,5
(4)常用函数
官方函数手册:Functions - Neo4j Cypher Manual
# 显示所有的neo4j 函数
show functions
| 函数 | 用法 |
|---|---|
| string字符串 | 他们用于使用 string字面量 |
| aggregation 聚合 | 用于对CQL查询结果执行一些聚合操作 |
| relationship 关系 | 用于获取关系的细节,如 startnode, endnode 等 |
字符串函数
和 SQL 一样,neo4j CQL 提供了一组 string 函数,用于查询中获取所需的结果。
| 功能 | 描述 |
|---|---|
| upper | 将所有的字母转为大写字母 |
| lower | 将所有的字母转为小写字母 |
| substring | 截取字符串 |
| replace | 替换字符串 |
# 截取e.name字符串中的前三个字符
match (e) return id(e),e.name,substring(e.name, 0, 3)
聚合函数
最大值,最小值,平均值,总数,求和等。和SQL类似。
| 聚合函数 | 描述 |
|---|---|
| count | 根据match命令返回的行数。 |
| max | 从match命令返回的一组行中返回最大值。 |
| min | 从match命令返回的一组行中返回最小值。 |
| sum | 返回由match 命令返回的所有行的求和值。 |
| avg | 返回由 match 命令返回的所有行的平均值。 |
# 统计student有多少个节点。
match (n:student) return count(n)
关系函数
neo4j CQL 提供了一组关系函数,以在获取开始节点,结束节点等细节时知道关系的细节。
| 功能 | 描述 |
|---|---|
| startnode | 获取关系的开始节点 |
| endnode | 获取关系的结束节点 |
| id | 获取关系的ID |
| type | 获取字符串表示中的一个关系的type |
# 获取a到b的关系ID和TYPE
match (a)-[r]->(b) return id(r),type(r)
(5)数据备份和恢复(neo4j-admin使用)
数据库备份
对 neo4j 数据进行备份,还原,迁移的操作时,要关闭 neo4j
cd %NEO4J_HOME%/bin
# 关闭 neo4j (注意:一定要先执行 neo4j install-service ,才会有 neo4j stop)
neo4j stop
# 备份
neo4j-admin dump --database=graph.db --to=/neo4j/backup/graph_backup.dump
数据库恢复
还原,迁移之前,要关闭 neo4j 服务。
# 数据导入
neo4j-admin load --from=/neo4j/backup/graph_backup.dump --database=graph.db --force
# 启动服务(重启服务为 neo4j restart)
neo4j start
附录
(1)相关教程地址
- Neo4j 官方网址:Graph Database Platform | Graph Database Management System | Neo4j
- w3cschool Neo4j 教程:Neo4j - 特点和优势_w3cschool
- Neo4j 官方社区版下载地址:Neo4j Download Center - Neo4j Graph Database Platform
- 博客教程:neo4j(一).初识图数据库neo4j_抓住流浪剑客的小YI巴-CSDN博客_图数据库neo4j
- Neo4j 导入数据:neo4j(二).使用neo4j-import导入数据及关系_抓住流浪剑客的小YI巴-CSDN博客