前言
學習最好的方式就是練習,以下使用 person->friends->person 的朋友關係資料來練習。
- Dave 的朋友是那些人?
- Dave 的朋友的朋友是那些人?
- Ted 要如何關連到 Denise ?
環境準備
使用 Docker 最方便,所以第一次透過 docker run
將 JanusGraph 下載下來並執行1
docker run --rm --name janusgraph-default docker.io/janusgraph/janusgraph:latest

如果要停止 janusgraph ,可執行 docker stop janusgraph-default
要再執行 janusgraph ,可執行 docker start janusgraph-default
當 JanusGraph 執行起來後,開啟 Gremlin Console1
docker exec -it janusgraph-default ./bin/gremlin.sh

連到 JanusGraph 的 Gremlin Server,並將 Gremlin Console 切換到 Remote1
2
3:remote connect tinkerpop.server conf/remote.yaml session
:remote console

新增資料
在 Gremlin Console 中,貼上以下的 groovy 程式,以新增朋友的資料1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40//Remove any existing data to allow this to be rerun
g.V().drop().iterate()
//Adds a person vertex with a name of Dave and saves it to a variable
dave = g.addV('person').property('first_name', 'Dave').next()
//Adds a person vertex with a name of Josh and saves it to a variable
josh = g.addV('person').property('first_name', 'Josh').next()
//Adds a person vertex with a name of Ted and saves it to a variable
ted = g.addV('person').property('first_name', 'Ted').next()
//Adds a person vertex with a name of Hank and saves it to a variable
hank = g.addV('person').property('first_name', 'Hank').next()
//Adds a friends edge between Dave and Ted
g.addE('friends').from(dave).to(ted).next()
//Adds a friends edge between Dave and Josh
g.addE('friends').from(dave).to(josh).next()
//Adds a friends edge between Dave and Hank
g.addE('friends').from(dave).to(hank).next()
//Adds a friends edge between Josh and Hank
g.addE('friends').from(josh).to(hank).next()
//Adds a friends edge between Ted and Josh
g.addE('friends').from(ted).to(josh).next()
//Adds a person vertex with a name of Kelly and saves it to a variable
kelly = g.addV('person').property('first_name', 'Kelly').next()
//Adds a person vertex with a name of Jim and saves it to a variable
jim = g.addV('person').property('first_name', 'Jim').next()
//Adds a person vertex with a name of Paras and saves it to a variable
paras = g.addV('person').property('first_name', 'Paras').next()
//Adds a person vertex with a name of Denise and saves it to a variable
denise = g.addV('person').property('first_name', 'Denise').next()
//Adds friends edges
g.addE('friends').from(dave).to(jim).
addE('friends').from(dave).to(kelly).
addE('friends').from(kelly).to(jim).
addE('friends').from(kelly).to(denise).
addE('friends').from(jim).to(denise).
addE('friends').from(jim).to(paras).
addE('friends').from(paras).to(denise).iterate()
- 註: 以上資料來自 图数据库实战 的《图数据库实战》源代码文件.zip
練習問題
Dave 的朋友是那些人?
1 | g.V().has('person', 'first_name', 'Dave').outE('friends').inV().values('first_name') |
- 註:
outE('friends') + inV()
可以改用out('friends')
,所以 gremlin 可以改成1
g.V().has('person', 'first_name', 'Dave').out('friends').values('first_name')

Dave 的朋友的朋友是那些人?
朋友的朋友,就是找朋友 2 次,所以可以用 times(2)
1
2
3
4g.V().has('person', 'first_name', 'Dave').
repeat(
out('friends')
).times(2).values('first_name')
- 註: 因為有重覆的人,可以使用
dedup()
去除重覆資料 Denise 就只會顯示一筆1
2
3
4
5
6g.V().has('person', 'first_name', 'Dave').
repeat(
out('friends')
).times(2).
dedup().
values('first_name')

Ted 要如何關連到 Denise ?
所以從 Ted until
到 Denise ,然後 repeat
來取出路徑,再顯示出 first_name1
2
3
4
5g.V().has('person', 'first_name', 'Ted').
until(has('person', 'first_name', 'Denise')).
repeat(
both('friends').simplePath()
).path().by(values('first_name'))

- 註: GraphDatabase 的查詢通常是從 Vertex(Node) 開始,從 Edge 開始通常會有效能問題