Graph Database
1. Definition
A database for storing and querying data in a data structure like graphs Composition
Node
- Specific entities, such as the movie “Harry Potter 2“,
- Relation
- The connection between entities.
- Label
- Abstract concepts that group similar nodes together, such as movie characters.
- Property
- Specific information about the node or relationship, such as name, age, etc.
2. Neo4j
2.1 Introduction
知识图谱存储工具:RDF4J、Jena、Neo4j
Neo4j is a robust, scalable, high-performance open source graph database
2.2 Neo4j-Important element
2.2.1 Structural Node
- ID: unique identifier
- Label: a form of pattern syntax to group nodes together
- Map(properties):
- key: property name
- value: property value
2.2.2 Properties
- Key: property name
- Value: property value
Value Type
- Boolean
- Byte
- Short
- Int
- Long
- Float
- Double
- Char
- String
- Array
2.2.3 Structural Relation
- ID
- Type
- Map(properties)
- ID of the start node
- ID of the end node
2.2.4 Path
- An sequence of nodes and relationships
- Composition
- at least one node
- connected relationships
- Often as a result of a query or traversal
- A path from nodeA to nodeB
3. Cypher
3.1 Introduction
- Cypher is a declarative graph query language that allows for expressive and efficient querying, updating and administering of the graph.
- Note: Keywords are not case sensitive
3.2 Cypher-Create
3.2.1 Create a node
1 | CREATE( |
3.2.2 Create relationships
1 | CREATE |
Exercise
- Create the relationship using cypher.
1 | CREATE |
3.3 Cypher-MATCH
- Match
1 | MATCH |
- Return
- Variable, like node-name: node
- Specific value, like node-name.property: node.name
3.3.1 Match node
- 查询所有节点
1 | MATCH (n) |
- 查询所有电影title
1 | MATCH (movie:Movie) |
3.3.2 Match related nodes
- 名叫Oliver Stone相关联的事物标题
1 | MATCH (director {name: 'Oliver Stone'})--(movie) |
— : connected relationship of unknown relationship type and direction
与人物Oliver Stone相关联的电影标题
1 | MATCH (:Person {name: 'Oliver Stone'})--(movie:Movie) |
- 与人物Oliver Stone相关联的关系类型
1 | MATCH (:Person {name: 'Oliver Stone'})-[r]->(movie) |
-> / <- : directed relation, > towards the tail entity
出演名为‘Wall Street’电影的演员姓名
1 | MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor) |
3.3.4 Match on multiple relationship types
- 参演或导演名为Wall Street的电影的所有人物
1 | MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN|:DIRECTED]-(person:Person) |
3.3.5 Match on relationship type and use a variable
- 名为Wall Street的电影的中所有的角色
1 | MATCH (wallstreet:Movie {title: 'Wall Street'})<- |
3.4 Match and Create
- when nodes already exist, we can use MATCH first, then CREATE
1 | MATCH |
Exercise
- 查询参演名为‘The American President’的所有演员姓名
1 | MATCH |
- 查询包含角色’Card Fox’的电影名
1 | MATCH |
3.5 Cypher-DELETE
- Delete single node
- Delete all nodes and relationships
- Delete a node with all its relationships
- Delete relationships only
3.5.1 Delete single node
- 删除名为UNKNOWN的节点
1 | MATCH (n:Person {name: 'UNKNOWN’}) DELETE n |
3.5.2 Delete all nodes and relationships
- 删除数据库中所有节点及与其相连的关系
1 | MATCH (n) DETACH DELETE n |
3.5.3 Delete a node with all its relationships
- 删除名为Andy的节点和与其相连的所有关系
1 | MATCH (n {name: 'Andy’}) DETACH DELETE n |
3.5.4 Delete relationships only
- 删除Andy的所有KNOWS关系
1 | MATCH (n {name: 'Andy'})-[r:KNOWS]->() DELETE r |
Exercise
- 删除人物年龄为34岁的所有关系
1 | MATCH (person:Person {age:34})-[r]->() |
3.6 Cypher-UPDATE :
3.6.1 SET
SET can be used with a map — provided as a literal, a parameter, or a node or relationship — to set properties.
Update a property
1 | MATCH (n {name: 'Andy'}) |
3.6.2 multiple properties using one SET clause
1 | MATCH (n {name: 'Andy'}) |
3.6.3 Replace all properties using a map
1 | MATCH (p {name: 'Peter'}) |
Exercise
- 更新George的年龄为28
1 | MATCH (p {name:'George'}) |
Exercise
- Write cypher to get the second graph
1 | MATCH |
- Query all relationship types connected by actor Anthony Hopkins
1 | MATCH |