map
과 reduce
함수를 기반으로 한다.SELECT date_trunc('month', observation_timestamp) AS observation_month,
sum(num_animals) AS total_animals
FROM observations
WHERE faily = 'Sharks'
GROUP BY obervation_month;
db.observations.mapReduce(
function map() { // 2
var year = this.observationTimestamp.getFullYear();
var month = this.observationTimestamp.getMonth() + 1;
emit(year + "-" month, this.numAnimals); // 3
},
function reduce(key, values) { // 4
return Array.sum(values); // 5
},
{
query: { family: "Sharks" }, // 1
out: "monthlySharkReport"
}
)
map
은 자바스크립트 함수다.
this
는 문서 객체로 설정map
함수는 키(년 - 월)와 값(관측치에 있는 동물 수)을 방출한다.map
이 방출한 키-값 쌍은 키로 그룹화된다.
reduce
함수를 한 번씩 호출한다.reduce
함수는 특정 월의 모든 동물 수를 합친다.monthlySharkReport
컬렉션에 기록한다.map
과 reduce
는 순수 함수여야 한다.
CREATE TABLE vertices (
vertex_id integer PRIMARY KEY,
properties json
);
CREATE TABLE edges (
edge_id integer PRIMARY KEY,
tail_vertex integer REFERENCES vertices (vertex_id),
head_vertex integer REFERENCES vertices (vertex_id),
label text,
properties json
);
CREATE INDEX edges_tails ON edges (tail_vertex);
CREATE INDEX edges_heads ON edges (head_vertex);
person
은 어떤 정점을 향하는 BORN_IN
유출 간선을 가진다.name
속성이 “United States”인 Location
유형 정점에 도달할 때까지 WITHIN
유출 간선을 따라간다.person
정점은 LIVES_IN
유출 간선도 가지는데 이 간선과 WITHIN
유출 간선을 따라가면 name
이 “Europe”인 Location
유형의 정점에 도달하게 된다.MATCH
(person) -[:BORN_IN]-> () -[:WITHIN*0..]-> (us:Location {name:'United States'}),
(person) -[:LIVES_IN]-> () -[:WITHIN*0..]-> (eu:Location {name:'Europe'})
RETURN person.name
WITH RECURSIVE
-- 미국 출생자 찾기
born_in_us AS (
-- 기본 케이스: 직접 미국에서 태어난 사람
SELECT p.person_id, p.name
FROM Person p
JOIN Location l1 ON p.birth_location_id = l1.id
WHERE l1.name = 'United States'
UNION ALL
-- 재귀 케이스: 하위 지역에서 태어난 사람
SELECT p.person_id, p.name
FROM Person p
JOIN Location l ON p.birth_location_id = l.id
JOIN Location parent ON l.parent_id = parent.id
JOIN born_in_us b ON parent.id = b.person_id
),
-- 유럽 거주자 찾기
lives_in_eu AS (
-- 기본 케이스: 직접 유럽에 사는 사람
SELECT p.person_id, p.name
FROM Person p
JOIN Location l2 ON p.current_location_id = l2.id
WHERE l2.name = 'Europe'
UNION ALL
-- 재귀 케이스: 하위 지역에 사는 사람
SELECT p.person_id, p.name
FROM Person p
JOIN Location l ON p.current_location_id = l.id
JOIN Location parent ON l.parent_id = parent.id
JOIN lives_in_eu e ON parent.id = e.person_id
)
-- 두 조건을 모두 만족하는 사람 찾기
SELECT DISTINCT us.name
FROM born_in_us us
JOIN lives_in_eu eu ON us.person_id = eu.person_id[1][2];
(루시, 나이, 33)
= lucy 정점(주어)의 나이(키)는 33(값)(루시, 결혼하다, 알랭)
에서 루시와 알랭은 정점, 결혼하다는 간선<em>바나나</em>는 <em>노란색</em>이다
라는 HTML 코드에서 단순히 강조 표시만 인식할 뿐, “바나나”와 “노란색”의 관계를 이해하지 못한다.PREFIX : <run:example:>
SELECT ?personName WHERE {
?person :name ?personName.
?person :bornIn / :within* / :name "United States".
?person :livesIn / :within* / :name "Europe".
}