当前位置:网站首页>neo4j图数据库基本概念
neo4j图数据库基本概念
2022-06-27 04:35:00 【思想在拧紧】
简介:本文以电影关系图为例,解释neo4j图数据库中的基本概念
电影关系图
下图中的圆圈表示三个节点(node),每个节点都有表示类别的标签(label)和属性(properties),如:name、born、title、released,节点之间用单向箭头连接表示节点间的关系(relationships),如:ACTED_IN、DIRECTED
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MyWLOPT6-1656069356588)(F:/Backup/img/graph_simple.png)]](/img/17/f0d65a62e7bf1fd4001ca76093bbb4.png)
以下Cycher语句可生成上述图结构
CREATE (:Person:Actor {name: 'Tom Hanks', born: 1956})-[:ACTED_IN {roles: ['Forrest']}]->(:Movie {title: 'Forrest Gump'})<-[:DIRECTED]-(:Person {name: 'Robert Zemeckis', born: 1951})
节点 node
电影关系图中每一个圆圈都表示一个节点(node),每个节点表示一个实体,下面具体分析第一个节点![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hui2IOn9-1656069356589)(F:/Backup/img/graph_single_node.png)]](/img/0b/3cd2455fa89681a607557cc5cbdf38.png)
节点中的Person、Actor为节点的标签(label),能将有着相同标签的节点划分到同一个类中,类似于Java中类的概念。节点可拥有多个标签,标签间的关系类似于类间的关系(继承),本节点中Actor可看做是Person的子类;
name:'Tom Hanks' born:1956为节点的属性(properties),类似于Java类中的属性
java实现代码如下
public class Person{
String name;
long born;
}
class Actor extends Person{
}
该节点可由以下Cypher语句生成
CREATE (:Person:Actor {name: 'Tom Hanks', born: 1956})
语句解释:
- 创建节点所属的标签
:Person、:Actor - 以键值对(key-value pairs)的形式声明属性,并大括号
{}括起
关系 relationship
描述相互连接的始末节点间如何关联(describes how a connection between a source node and a target node are related),关系可以将节点构成链表(list)、树(tree)、图(map)、复合实体。
特点:
- 有方向且单方向
- 必须有一个类型(type)以定义是何种关系
Must have a type (one type) to define (classify) what type of relationship it is. - 节点可以由指向自己的关系
- 关系可以有属性
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hxXMjQEd-1656069356590)(F:/Backup/img/graph_example_relationship.png)]](/img/a8/77e55e0c176384bacbd460f846131c.png)
上图关系的类型(type)是ACTED_IN,属性为roles: ['Forrest']、performance: 5
Cypher语句为:
CREATE ()-[:ACTED_IN {roles: ['Forrest'], performance: 5}]->()
()表示节点
-[:ACTED_IN {roles: ['Forrest'], performance: 5}]->为声明关系的语句,键值对同样表示属性(properties)
属性 properties
属性是以键值对的形式描述节点(node)和关系(relationships)的数据
属性可以是各种数据类型,如:number、string或boolean;可以是存储同类数据的array或list,如:strings、numbers、bollean values
CREATE (:Example {a: 1, b: 3.14}) // 属性a存储integer类型的值1,属性b存储float类型的数据
CREATE (:Example {c: 'This is an example string', d: true, e: false})
CREATE (:Example {f: [1, 2, 3], g: [2.71, 3.14], h: ['abc', 'example'], i: [true, true, false]})
其他概念
图数据库中的其他概念暂时不做详述,如:Traversals(遍历)、paths(路径)、Schema、Indexes(索引)、Constraints(约束)
补充
电影数据库为neo4j的官方样例,可使用:guide movie-graph命令查看。关于属性、标签等概念可以借助以下查询语句验证和理解。
Match (n)
return ID(n) as id, properties(n) AS properties, labels(n) AS label
neo4j中的命名规范
节点的标签、关系的类型、属性都是大小写敏感的,例如:属性name≠Name,一下为neo4j官方推荐的命名规范
| Graph entity | Recommended style | Example |
|---|---|---|
| Node label | Camel case, beginning with an upper-case character | :VehicleOwner rather than :vehice_owner |
| Relationship type | Upper case, using underscore to separate words | :OWNS_VEHICLE rather than :ownsVehicle |
| Property | Lower camel case, beginning with a lower-case character | firstName rather than first_name |
对比图论
在图论中,节点也称为顶点(vertices )或点(point)
关系也称为边(edges)、链接(links)或线(lines)
创建节点
创建节点的语法中,可将节点名命名为n。因为节点名不作为唯一标识,类似于临时变量,使用相同的节点名不会影响数据库的创建
CREATE (n:Person {name:'Sally'}) RETURN n
CREATE (n:Person {name:'Steve'}) RETURN n
CREATE (n:Person {name:'Mike'}) RETURN n
neo4j图片导出
若导出为svg,可使用vscode或notepad编辑内容,查找font-size设置字体大小,查找font-family设置字体格式,然后使用photoshop将svg导出为png或借助在线转png的网站
neo4j交互设置
点击任一节点,出现一个灰色的圈,分别表示固定节点的位置、隐藏节点、显示与该节点有关系的其他节点
右侧点击节点的标签Person,可以调整节点的颜色和大小
点击箭头,可以设置表示关系箭头的粗细和颜色
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GA4QPan5-1656069356591)(F:/Backup/img/image-20220624190647695.png)]](/img/4c/5a8997ce0381931fe7343b72260e58.png)
参考文章:
meo4j 4.4 官方文档 Graph database concepts
Neo4j图数据库中文文档—neo4j 0.1.0a documentation
基于Neo4j构建电影图谱 - 知乎 (zhihu.com)
边栏推荐
- 微服务系统设计——服务注册与发现和配置设计
- 渗透测试-目录遍历漏洞
- 2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
- Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
- 018 C语言基础:C文件读写
- Chapter 2 Introduction to key technologies
- Cultural tourism night tour | stimulate tourists' enthusiasm with immersive visual experience
- 017 basics of C language: bit field and typedef
- 如何让 EF Core 6 支持 DateOnly 类型
- 010 C语言基础:C函数
猜你喜欢

Learn crypto from Buu (Zhou Geng)

Microservice system design -- distributed cache service design

深潜Kotlin协程(十五):测试 Kotlin 协程

Quickly master asp Net authentication framework identity - reset password by mail
![[station B up dr_can learning notes] Kalman filter 2](/img/52/777f2ad2db786c38fd9cd3fe55142c.gif)
[station B up dr_can learning notes] Kalman filter 2

Matlab | drawing of three ordinate diagram based on block diagram layout

微服务系统设计——消息缓存服务设计

2022-06-26: what does the following golang code output? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { type

【C语言】关键字的补充

微服务系统设计——服务链路跟踪设计
随机推荐
【C语言】关键字的补充
Fplan power planning
[数组]BM94 接雨水问题-较难
QChart笔记2: 添加鼠标悬停显示
733. 图像渲染
系统架构设计——互联网金融的架构设计
Kotlin compose compositionlocalof and staticcompositionlocalof
【B站UP DR_CAN学习笔记】Kalman滤波3
如何让 EF Core 6 支持 DateOnly 类型
微服务系统设计——分布式缓存服务设计
快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码
012 C language foundation: C array
Microservice system design -- microservice invocation design
Fplan layout
010 C language foundation: C function
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
微服务系统设计——服务注册与发现和配置设计
Microservice system design -- microservice monitoring and system resource monitoring design
iOS开发:对于动态库共享缓存(dyld)的了解
006 C language foundation: C storage class