Neo4j Tutorial

hiblue
Hiblue (토론 | 기여) 사용자의 2018년 1월 18일 (목) 16:07 판 (Cypher, The Graph Query Language)

이동: 둘러보기, 검색

Neo4j 소개

  • Neo4j is the world's leading graph database.
    그래프 데이터 베이스
  • Cypher, The Graph Query Language를 이용

Neo4j의 기본구조

  • The Labeled Property Graph Model을 기본 데이터 구조로 가짐.

Neo4j model.JPG

노드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

Cypher model.JPG

  • Cyper의 기본 구조
MATCH <pattern> WHERE <conditions> RETURN <expressions>
  • Create
CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })

이름이 Emil이고 출신이 Sweden, 영향력이 99인 Person 라벨의 개체를 만들어라.

  • Match : finding Nodes
MATCH (ee:Person) WHERE ee.name = "Emil" RETURN ee;

라벨이 Person인 개체 중에 이름 속성이 emil인 것의 그 개체 자체(모든 것)를 보여라.

  • Pattern Matching
MATCH (ee:Person)-[:KNOWS]-(friends) WHERE ee.name = "Emil" RETURN ee, friends

라벨이 Person인 개체 중에 이름이 에밀이란 개체와 KNOWS란 관계를 가진 것을 friends라 한다. 그 에밀이란 개체(ee)와 friends을 보여라.

  • Using Pattern
MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer) WHERE js.name = "Johan" AND surfer.hobby = "surfing" RETURN DISTINCT surfer

라벨이 Person인 개체 중 KNOWS와 KNOWS가 두번 연결된 관계에 있는 것을 surfer라 한다. 이름이 johan이고 surfer의 취미가 surfing인 것들 중 surfer들의 고유값을 보여줘라.

Cypher 실습

match (n:Actor) return n
  • 클래스가 Actor인 개체의 한글이름과 한자이름을 보여라.
match (n:Actor) return n.name, n.chname