Smurf
文章50
标签0
分类6
Graph Database

Graph Database

Graph Database

image-20211215112032232

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

image-20211215112335838

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

image-20211215113332298

image-20211215113339365

2.2.2 Properties

  • Key: property name
  • Value: property value

Value Type

  • Boolean
  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double
  • Char
  • String
  • Array

image-20211215221640627

2.2.3 Structural Relation

  • ID
  • Type
  • Map(properties)
  • ID of the start node
  • ID of the end node

image-20211215221750855

image-20211215221802938

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

image-20211215221843268

  • A path from nodeA to nodeB

image-20211215221900810

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.

image-20211215114141617

  • Note: Keywords are not case sensitive

3.2 Cypher-Create

3.2.1 Create a node

image-20211215114117836

image-20211215114209481

image-20211215222102518

image-20211215222113317

1
2
3
4
5
6
CREATE(
node:Movie
{
title:"The American President"
}
)

3.2.2 Create relationships

1
2
3
4
CREATE
(<node1-name>:<node1-label-name>{<define-properties-list>})
-[<relationship-name>:<relationship-label-name>{<define-properties-list>}]
->(<node2-name>:<node2-label-name>{<define-properties-list>})

image-20211215222303879

image-20211215222313633

Exercise

  • Create the relationship using cypher.

image-20211215222334213

1
2
3
4
CREATE
(n1:Person {name:'Oliver Stone'})
-[r:DIRECTED]
->(N2:Movie{title:"Wall Street"})

3.3 Cypher-MATCH

  • Match
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MATCH
(<node-name>:<label-name>
{
<Property1-name>:<Property1-Value>
........
<Propertyn-name>:<Propertyn-Value>
})
-[<relationship-name>:<relationship-label-name>{<define-properties-list>}]
->(<node-name>:<label-name>
{
<Property1-name>:<Property1-Value>
........
<Propertyn-name>:<Propertyn-Value>
})
RETURN ........
  • Return
    • Variable, like node-name: node
    • Specific value, like node-name.property: node.name

3.3.1 Match node

  • 查询所有节点
1
2
MATCH (n) 
RETURN n

image-20211215222822126

  • 查询所有电影title
1
2
MATCH (movie:Movie)
RETURN movie.title

image-20211215222827727

  • 名叫Oliver Stone相关联的事物标题
1
2
MATCH (director {name: 'Oliver Stone'})--(movie) 
RETURN movie.title

image-20211215223003469

image-20211215223020275

  • — : connected relationship of unknown relationship type and direction

  • 与人物Oliver Stone相关联的电影标题

1
2
MATCH (:Person {name: 'Oliver Stone'})--(movie:Movie) 
RETURN movie.title

image-20211215223434705

  • 与人物Oliver Stone相关联的关系类型
1
2
MATCH (:Person {name: 'Oliver Stone'})-[r]->(movie) 
RETURN type(r)

image-20211215223412345

  • -> / <- : directed relation, > towards the tail entity

  • 出演名为‘Wall Street’电影的演员姓名

1
2
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor) 
RETURN actor.name

image-20211215223524497

3.3.4 Match on multiple relationship types

  • 参演或导演名为Wall Street的电影的所有人物
1
2
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN|:DIRECTED]-(person:Person) 
RETURN person.name

image-20211215223626746

3.3.5 Match on relationship type and use a variable

  • 名为Wall Street的电影的中所有的角色
1
2
3
MATCH (wallstreet:Movie {title: 'Wall Street'})<-
[r:ACTED_IN]-(actor)
RETURN r.role

3.4 Match and Create

  • when nodes already exist, we can use MATCH first, then CREATE

image-20211215223827870

1
2
3
4
5
MATCH 
(charlie:Person {name: 'Charlie Sheen'}),
(rob:Person {name: 'Rob Reiner'})
CREATE
(rob)-[:TYPE INCLUDING A SPACE]->(charlie)

Exercise

  • 查询参演名为‘The American President’的所有演员姓名

image-20211215224022480

1
2
3
4
MATCH
(person:Person)-[:ACTED_IN]
->(movie:Movie{title:"The American President"})
RETURN person.name
  • 查询包含角色’Card Fox’的电影名

image-20211215224512063

1
2
3
4
MATCH
(actor)-[:ACTED{role:"Card Fox"}]
->(movie:Movie)
RETURN movie.title

3.5 Cypher-DELETE

  • Delete single node
  • Delete all nodes and relationships
  • Delete a node with all its relationships
  • Delete relationships only

image-20211215224917378

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岁的所有关系

image-20211215225242665

1
2
MATCH (person:Person {age:34})-[r]->()
DELETE 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
2
3
MATCH (n {name: 'Andy'}) 
SET n.age = toString(n.age)
RETURN n.name, n.age

image-20211215225919668

image-20211215225927663

3.6.2 multiple properties using one SET clause

1
2
MATCH (n {name: 'Andy'}) 
SET n.position = 'Developer', n.surname = 'Taylor'

3.6.3 Replace all properties using a map

1
2
3
4
MATCH (p {name: 'Peter'}) 
SET p = {name: 'Peter Smith’,
position: 'Entrepreneur'}
RETURN p.name, p.age, p.position

image-20211215230129964

Exercise

  • 更新George的年龄为28

image-20211215230148658

1
2
3
MATCH (p {name:'George'})
SET p = {name: 'George'
age: 28}

Exercise

image-20211215230420391

  • Write cypher to get the second graph
1
2
3
4
5
6
7
8
MATCH 
(actor1:Actor {name: 'Anthony Hopkins'}),
(actor2:Actor {name: 'Hitchcock'}),
(movie:Movie {title: 'Hitechcock'})
CREATE
(actor1)-[r:ACTS_IN]
->(movie)
DELETE actor2
  • Query all relationship types connected by actor Anthony Hopkins
1
2
3
MATCH
(actor:Actor {name: 'Anthony Hopkins'})-[r]-()
RETURN type(r)
本文作者:Smurf
本文链接:http://example.com/2021/08/15/knowledge%20engineering/14.%20Graph%20Database/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可