 |
Neo4j 실습을 위한 샘플 예제 및 쿼리 문서입니다. 본 문서는 DH Edu 교강사 김지선이 작성하였습니다.(21-2학기 최초 작성)
|
실습 예제
【샘플 데이터1(한강이남) 시트 페이지로 이동】
【샘플 데이터2(서울전체) 시트 페이지로 이동】
[ 【샘플 데이터3(서울전체-교화/교목추가) 시트 페이지로 이동】]
샘플1
지도를 불러오는 중...
{"minzoom":false,"maxzoom":false,"mappingservice":"leaflet","width":"100%","height":"360px","centre":false,"title":"","label":"","icon":"","visitedicon":"","lines":[],"polygons":[],"circles":[],"rectangles":[],"wmsoverlay":false,"copycoords":false,"static":false,"zoom":6,"defzoom":14,"layers":["OpenStreetMap"],"overlaylayers":[],"resizable":false,"enablefullscreen":false,"scrollwheelzoom":true,"markercluster":false,"clustermaxzoom":20,"clusterzoomonclick":true,"clustermaxradius":80,"clusterspiderfy":true,"geojson":"","locations":[{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uac80\ub2e8\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\uac80\ub2e8\uc0b0\n","link":"","lat":37.517679,"lon":127.2493873,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ub0a8\ud55c\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\ub0a8\ud55c\uc0b0\n","link":"","lat":37.480586,"lon":127.2040795,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ub0a8\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \ubaa9\uba71\uc0b0, \uc885\ub0a8\uc0b0, \uc778\uacbd\uc0b0, \uc5f4\uacbd\uc0b0, \ub9c8\ubafc\n\u003C/p\u003E\u003C/div\u003E","title":"\ub0a8\uc0b0\n","link":"","lat":37.548752,"lon":126.9908734,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc0bc\uac01\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \ubd81\ud55c\uc0b0\n\u003C/p\u003E\u003C/div\u003E","title":"\uc0bc\uac01\uc0b0\n","link":"","lat":37.658711,"lon":126.97798,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ubd81\uc545\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \ubc31\uc545\uc0b0, \uba74\uc545\uc0b0, \uacf5\uadf9\uc0b0\n\u003C/p\u003E\u003C/div\u003E","title":"\ubd81\uc545\uc0b0\n","link":"","lat":37.593029,"lon":126.9737306,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ub300\ubaa8\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\ub300\ubaa8\uc0b0\n","link":"","lat":37.47492,"lon":127.0789667,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uad6c\ub8e1\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\uad6c\ub8e1\uc0b0\n","link":"","lat":37.468934,"lon":127.0615631,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uad00\uc545\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \uc18c\uae08\uac15, \uc11c\uae08\uac15\n\u003C/p\u003E\u003C/div\u003E","title":"\uad00\uc545\uc0b0\n","link":"","lat":37.444469,"lon":126.9638833,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc778\uc655\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\uc778\uc655\uc0b0\n","link":"","lat":37.584915,"lon":126.957849,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ubc31\ub450\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\ubc31\ub450\uc0b0\n","link":"","lat":41.99334,"lon":128.0786108,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc640\ub8e1\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\uc640\ub8e1\uc0b0\n","link":"","lat":37.501146,"lon":126.8206901,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc6b0\uc7a5\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003C/div\u003E","title":"\uc6b0\uc7a5\uc0b0\n","link":"","lat":37.551156,"lon":126.842302,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc218\uba85\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \ubc1c\uc0b0, \ud30c\ub824\uc0b0\n\u003C/p\u003E\u003C/div\u003E","title":"\uc218\uba85\uc0b0\n","link":"","lat":37.547919,"lon":126.8275525,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uac1c\ud654\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \uc8fc\ub8e1\uc0b0\n\u003C/p\u003E\u003C/div\u003E","title":"\uac1c\ud654\uc0b0\n","link":"","lat":37.581678,"lon":126.8061454,"icon":""},{"text":"\u003Cb\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\ub355\uc591\uc0b0\n\u003C/p\u003E\u003C/div\u003E\u003C/b\u003E\u003Chr /\u003E\u003Cdiv class=\"mw-parser-output\"\u003E\u003Cp\u003E\uc774\uce6d: \uc131\uc0b0, \ud589\uc8fc\uc0b0\n\u003C/p\u003E\u003C/div\u003E","title":"\ub355\uc591\uc0b0\n","link":"","lat":37.595606,"lon":126.8289372,"icon":""}],"imageoverlays":null}
샘플 쿼리
생성
● 단일 노드 생성〔생성된 노드 확인 하기 위해서는 return a〕
create (a:label명{property명:'property값'})
-ex) create (a:label명{gid:'gid명', class:'class명', name:'대표명칭', refurl:'외부정보링크'})
● 특정 라벨(label)의 노드 속성(properties) 및 값(value)을 생성
match (a:label명) set a. property명=“property값”
● 엣지 데이터 생성〔생성된 엣지 데이터 확인 하기 위해서는 return a, r, b〕
match (a{property명:'property값'}) match(b{property명:'property값'})
create (a) - [r:relation명] -> (b)
● 관계 속성을 지닌 엣지 데이터 생성〔생성된 엣지 데이터 확인 하기 위해서는 return a, r, b〕
match (a{property명:'property값'}) match(b{property명:'property값'})
create (a) - [r:relation명{property명:'property값'}] -> (b)
출력: 기초
● 모든 데이터를 출력
match (n) return n
= match (n) return *
● 모든 데이터를 출력하되 50개 이하로 출력
match (n) return n limit 50
● 관계(relation)를 갖고 있는 모든 데이터를 출력
match (a)-[r]-(b) return a, r, b
● 관계(relation)가 없는 외톨이 노드 출력
match (a) where not (a)-[]-() return a
● 특정 라벨(label)에 속하는 데이터를 출력
match (a:label명) return a
출력: 속성
● 특정 노드의 속성(properties) 값(value)을 기준으로 데이터를 출력
match (a) where a.property명='property값' return a
= match (a{property명:'property값'}) return a
● 특정 라벨(label)에 속하는 노드의 속성(properties) 값(value)을 기준으로 데이터를 출력
match (a:label명) where a.property명='property값' return a
= match (a:label명{property명:'property값'}) return a
○ Where 참고1: >,<,=, starts with, ends with, contains
ⓐ노드의 속성(properties) 값(value)이 'ABC'인 노드를 출력
Where a.property명='ABC':
ⓑ노드의 속성(properties) 값(value)이 A로 시작하는 노드를 출력
Where a.property명 starts with 'A'
ⓒ노드의 속성(properties) 값(value)이 C로 끝나는 노드를 출력
Where a.property명 ends with 'C'
ⓓ노드의 속성(properties) 값(value) 중 B가 들어가는 노드를 출력
Where a.property명 contains 'B'
ⓔ노드의 속성(properties) 값(value)으로 'ABC', 'DEF', 'GHI' 중 하나를 가지는 노드를 출력
Where a.property명 in ['ABC', 'DEF', 'GHI']
○ Where 참고2: not 과 함께 쓸 경우 해당 조건 결과값을 제외
match (a:label명l) where not a.property명='property값' return a
- 노드의 속성(properties) 값(value)이 A로 시작하는 노드를 출력하지 않음
Where not a.property명 starts with 'A'
○ Where 참고3: and 과 함께 쓸 경우 복수의 조건 결과값을 출력
where a.propertyA명='propertyA값' and a.propertyB명='propertyB값'
= match (a:label명{propertyA:'propertyA값', propertyB:'propertyB값'})
● 특정 라벨(label)을 기준으로 데이터의 속성을 출력
match (a:label명) return a.property명
출력: 관계
● 특정 관계(relation)를 갖고 있는 모든 데이터를 출력
match (a) -[r:relation명]- (b) return a, r, b
= match (a:label명)-[r]-(b) where type(r)="relation명" return a, r, b
● 특정 관계 속성(properties) 값(value)을 기준으로 데이터를 출력
match (a) -[r:relation명{property명:'property값'}]- (b) return a, r, b
= match (a) -[r:relation명]- (b) where r.property명='property값' return a, r, b
● 특정 관계(relation)를 가지지 않는 데이터 출력
match (a:label명)-[r]-(b) where not type(r)="relation명" return a, r, b
● 특정 관계(relation)를 하나 이상 가지고 있는 데이터 출력
match (a:label명) - [r:relation명] - (b:label명) with a, count(r) as s, collect(b) as c
where s > 1 return a, s, c
출력: 간편하게!
● match문에 입력한 변수를 *으로 받기(*: 'all'을 뜻함)
match 쿼리 return *
출력: n단계
● 특정 노드를 기준으로 1단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [r] - (b) return a, r, b
● 특정 노드와 특정 관계(1단계)에 있는 내용을 출력
match (a{property명'property값'}) -[relation명]- (b) return a, r, b
● 특정 노드와 특정 노드 사이에 1단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) -[relation명]- (b{property명'property값'}) return a, r, b
● 특정 노드를 기준으로 1~2단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [*..2] - (b) return a, b
● 특정 노드를 기준으로 1~3단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [*..3] - (b) return a, b
● 특정 노드를 기준으로 3단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [*3] - (b) return a, b
● 특정 노드를 기준으로 2~3단계 관계를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [*2..3] - (b) return a, b
● 특정 노드를 기준으로 1~3단계의 특정 관계(relation)를 맺고 있는 데이터를 모두 출력
match (a{property명'property값'}) - [relation명*..3] - (b) return a, r, b
경로 구하기
● 특정 노드 사이의 경로 구하기(※p: 'path' 의 줄임말 / N에는 원하는 숫자 넣기)
match (a{property명:'property값'}), (b{property명:'property값'}),
p = ((a)-[*..N]-(b))
return p
● 특정 노드 사이의 가장 빠른 경로 구하기(※N에는 원하는 숫자 넣기)
match (a{property명:'property값'}), (b{property명:'property값'}),
p = shortestPath((a)-[*..N]-(b))
return p
오름/내림차순
● 노드 개수를 오름/내림차순으로 보이기
-예시1-1) 교가에 언급된 산의 이름과 언급 횟수를 오름차순으로 출력하라.
match (a:SchoolSong) -[r:mentions] -> (b:Mountain)
return b.name as 산이름, count(b) as 언급횟수 order by 언급횟수
-예시1-2) 교가에 언급된 산의 이름과 언급 횟수를 내림차순으로 출력하라.
match (a:SchoolSong) -[r:mentions] -> (b:Mountain)
return b.name as 산이름, count(b) as 언급횟수 order by 언급횟수 desc
○ as 참고: as 뒤에 오는 명칭에는 (쌍)따옴표를 붙이지 않으며, 띄어쓰기 시 _(언더바)를 붙인다.
- 예) match (a:SchoolSong) -[r:mentions] -> (b:Mountain) return b.name as 산_이름, count(b) as 언급_횟수 order by 언급_횟수 desc
복합 출력
(잠시 내립니다 👩🏻💻)
삭제
- 잘 삭제되었는지 확인하고 싶다면 "return"을 사용한다.
● 특정 라벨(label)의 노드 속성(properties)을 삭제
Match (a:label명) remove a. property명 Return a
● 모든 Node 삭제
Match (n) Delete n Return n
● 모든 Node와 Edge 삭제
Match (n) Detach Delete n Return n
● 특정 라벨(label)의 모든 Node 삭제
Match (a:label명) Delete a Return a
● 특정 Edge 삭제
Match (a) – [r:relation명] – (b) Delete r
더 공부하기📚
- ● Neo4j 질의어 참고 자료
- ○ Cypher 메뉴얼: Neo4j 공식 웹사이트
- ○ Data Science 영역의 알고리즘 매뉴얼: Neo4j 공식 웹사이트