Neo4j Tutorial
hiblue
목차
Neo4j 소개
- Neo4j is the world's leading graph database.
그래프 데이터 베이스 - Cypher, The Graph Query Language를 이용
Neo4j의 기본구조
- The Labeled Property Graph Model을 기본 데이터 구조로 가짐.
노드Nodes
- 데이터 개체
Nodes are the main data elements - 관계로 다른 노드들과 연결
Nodes are connected to other nodes via relationships - 하나 이상의 속성을 지님
Nodes can have one or more properties (i.e., attributes stored as key/value pairs) - 하나 이상의 라벨을 지님
Nodes have one or more labels that describes its role in the graph
관계Relationships
- 두 개의 노드를 연결
Relationships connect two nodes - 직접적으로 연결
Relationships are directional - 하나의 노드가 여러 개의 관계, 재귀적인 관계도 가질 수 있음
Nodes can have multiple, even recursive relationships - 관계는 하나 이상의 속성을 가진다
Relationships can have one or more properties (i.e., attributes stored as key/value pairs)
속성Properties
- 문자로 정의된 값
Properties are named values where the name (or key) is a string - 색인화되고, 제한될 수 있다.
Properties can be indexed and constrained - 다양한 속성으로 복합적인 색인을 만들 수 있다
Composite indexes can be created from multiple properties
라벨Labels
- 노드를 묶는 단위
Labels are used to group nodes into sets - 하나의 노드는 여러개의 라벨을 가질 수 있다
A node may have multiple labels - 그래프에서 노드를 더 잘 찾을 수 있도록 색인화된다
Labels are indexed to accelerate finding nodes in the graph - 기초 라벨 색인은 속도에 최적화되어 있다
Native label indexes are optimized for speed
Cypher, The Graph Query Language
Cyper의 기본 구조
MATCH <pattern> WHERE <conditions> RETURN <expressions>
Relation
-[변수:릴레이션{속성:"속성값"}]->
- A -[:Knows]-> B : Knows 관계를 가진 A와 B
- A -[*]-> B : 관계를 가진 A와 B
- A --> B : 관계를 가진 A와 B
- A -[*1..3]-> B : 1~3의 거리를 가진 A와 B
- A -[:Knows*1..3]-> B : Knows관계의 거리가 1~3인 A와 B
Create
CREATE (변수:라벨 { 속성: "속성값", 속성: "속성값" })
ex)
- CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 }) : 이름이 Emil이고 출신이 Sweden, 영향력이 99인 Person 라벨의 개체를 만들어라.
- CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }), (ir:Person { name: "Ian", from: "England", title: "author" }), (js)-[:KNOWS {since: 2001}]->(ir) : 이름이 johan이고 출신이 sweden이고 배우는 것이 surfing이고, 라벨이 person인 개체(js)와 이름이 lan이고 출신이 England이고 칭호가 작가이고 라벨이 Person인 개체(jr)을 만들어라. 그리고 js와 jr은 KNOWS라는 관계를 가지고, 그 관계의 since라는 속성은 2001이란 값을 가진다.
Cypher 실습
- Neo4j 샘플:워커힐 프로젝트
- 클래스 Actor만 보여라
match (n:Actor) return n
- 클래스가 Actor인 개체의 한글이름과 한자이름을 보여라.
match (n:Actor) return n.name, n.chname
- 프로젝트가 워커힐인 개체를 모두 보여라 (아무거나 25개만 보여라)
MATCH (n) where n.project="워커힐" RETURN n (LIMIT 25)
- 방문했다(visits)라는 관계를 개체를 모두 보여라.
MATCH p=()-[r:visits]->() RETURN p
- 이벤트의 이름과 이벤트에 참여한 행위자의 수를 내림차순으로 보여라.
match (e:Event)<-[:Creates]-(a:Actor) return e.name as eventName, count(a) as 참여인원 order by 참여인원 desc
- 이벤트와 그 이벤트가 열린 장소를 모두 보여라.
match (n:Place)<-[:isHeldAt]-(o) return n,o
- 과거 프로젝트 : 강세윤과 두단계까지만 연결된 인물들을 보여라
match (a1:Actor{name:"강세윤"}) -[*1..2]- (a2:Actor) where a1.project="과거" and a2.project="과거" return a1, a2
- 포미닛과 외화벌이(개념)을 연결하는 가장 빠른 길은?
match (포미닛{name:"포미닛"}), (외화벌이{name:"외화벌이"}), 빠른길= shortestPath((포미닛)-[*]-(외화벌이)) return 포미닛, 외화벌이, 빠른길
- 워커힐 프로젝트의 actor를 group 속성으로 분류하고, 각 속성에 속하는 개체의 이름을 보여라.
match (n:Actor) where n.project="워커힐" return n.group, count(n) as 분류, collect(n.name)