0%

透過 janusgraph Connection Edge 來達成建立 vertex 資料時,自動建立 schema 中定義的關聯

問題:

目前在設定 schema 時,希望可以在 edge label 的 property 中記錄下 source vertex label 與 target vertex label 中的 mapping property,來達到 user 在 data explore insert 資料時,自動建立 edge

ex : 有兩個 vertex label, userblog,想要在他們之間建立一個 post 的關聯

image-20210824175548195

所以就會想要在 post 這個 edge label 中新增兩個 property 來代表 user 和 blog 之間建立關聯的規則

可是這會遇到一個問題,在 janusgraph 中,edge label 的 label name 是不能重複的,所以假設今天有另外一組 vertex label 之間的關聯也要叫做 post ,edge label 的 source 跟 target 就有可能會不同

image-20210824175600219

image-20210824175610872

Connection Edge:

後來有找到可以透過 addConnection 這個方法,定義一個 edge 跟兩個 vertex 之間的關聯

用法如下:

1
mgmt.addConnection([edge label],[out vertex label],[in vertex label])

我們就可以透過這個 connection 來定義兩個 vertex 之間的關聯

ex:現在有三個 vertex label 和一個 edge label

image-20210824175624680

透過 addConnection 的方式把 vertex label 之間的關聯建立好,並在 connection Edge 中設定 mapping property

image-20210824175640736

新增資料後自動建立 edge 的做法:

  1. 在 data explorer 中建立一則 tweet 的資料
  2. 取得 tweet 這個 label 的 mappedConnections
  3. loop 過每個 connection 並看 connection edge 的 incomingVertexLabel 與 outgoingVertex label 是否為 tweet
  4. 如果是的話,就看之前塞入的 mapping property 對應的是什麼欄位,在針對 incomingVertexLabel 與 outgoingVertex Label 去建立關聯

Code:

建立 connection :

1
2
3
4
5
6
7
8
9
10
11
// vertex label
user = mgmt.getVertexLabel('user')
tweet = mgmt.getVertexLabel('tweet')
blog = mgmt.getVertexLabel('blog')

// edge label
post = mgmt.getEdgeLabel('post')

// 建立 connection
mgmt.addConnection(post,tweet,user);
mgmt.addConnection(post,blog,user);

塞入 mapping property:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 取得 edge
post = mgmt.getedgeLabel('post')

// 取得 connection
user_post_tweet = post.mappedConnections()[0]
user_post_blog = post.mappedConnections()[1]

// 塞入 mapping property
user_post_tweet.connectionEdge.property('source_property','id')
user_post_tweet.connectionEdge.property('target_property','tweet_beling_user_id')

user_post_blog.connectionEdge.property('source_property','id')
user_post_blog.connectionEdge.property('target_property','beling_user_id')

取得 connection 中我們塞入的 mapping property

1
2
3
4
gremlin> user_post_blog.connectionEdge.properties()
==>p[~T$SchemaDefinitionDescription->org.janusgraph.gr...] // 這個是每個 connection 預設都有的 property
==>p[soure_property->id]
==>p[target_property->beling_user_id]

取得 vertex label 的 connection

// user 取不到 mapped connection 因為 user 是 incomingVertexLabel
gremlin> user.mappedConnections()

// tweet 取到一個 connection
gremlin> tweet.mappedConnections()
==>org.janusgraph.core.Connection@5a090f62
// 這個 connection 的內容如下
gremlin> tweet.mappedConnections()[0].properties
==>class=class org.janusgraph.core.Connection
==>connectionEdge=null
==>edgeLabel=post
==>incomingVertexLabel=user
==>outgoingVertexLabel=tweet

// blog 取得一個 connection
gremlin> blog.mappedConnections()
==>org.janusgraph.core.Connection@be164d8
// 這個 connection 的內容如下
gremlin> blog.mappedConnections()[0].properties
==>class=class org.janusgraph.core.Connection
==>connectionEdge=null
==>edgeLabel=post
==>incomingVertexLabel=user
==>outgoingVertexLabel=blog

// post 取得兩個 connection
gremlin> post.mappedConnections()
==>org.janusgraph.core.Connection@7e52a26b
==>org.janusgraph.core.Connection@2e56b4d

// user->post->tweetconnection 內容如下
gremlin> post.mappedConnections()[0].properties
==>class=class org.janusgraph.core.Connection
==>connectionEdge=e[1534-el-1th-st][525-~T$SchemaRelated->1037]
==>edgeLabel=post
==>incomingVertexLabel=user
==>outgoingVertexLabel=tweet

// user->post->tweet 中設定的 mapping property 在這邊
post.mappedConnections()[1].connectionEdge.properties()
==>p[~T$SchemaDefinitionDescription->org.janusgraph.gr...]
==>p[source_property->id]
==>p[target_property->tweet_beling_user_id]

// user->post->blogconnection 內容如下
gremlin> post.mappedConnections()[1].properties
==>class=class org.janusgraph.core.Connection
==>connectionEdge=e[15vk-171-1th-st][1549-~T$SchemaRelated->1037]
==>edgeLabel=post
==>incomingVertexLabel=user
==>outgoingVertexLabel=blog

// user->post->blog 中設定的 mapping property 在這邊
post.mappedConnections()[1].connectionEdge.properties()
==>p[source_property->id]
==>p[target_property->beling_user_id]
==>p[~T$SchemaDefinitionDescription->org.janusgraph.gr...]
```-