JanusGraph---Advanced Schema

Static Vertices

  • 對于加載到圖中后不希望被改變的頂點,應該定義為static。
  • 將頂點label定義成static,那么具有該label的所有頂點在定義它的事務完成后,就不可以在改變。
mgmt = graph.openManagement()
tweet = mgmt.makeVertexLabel('tweet').setStatic().make()
mgmt.commit()

Edge and Vertex TTL

  • 頂點和邊可以配置time-to-live (TTL).當時間到達后就會從圖中移除。
  • 作用案例:對于一些臨時數據,可以使用TTL。
  • 支持該特性的數據庫,目前其他數據庫不支持。因為下面兩種支持Cell級的TTL。
    • Cassandra
    • HBase

Edge TTL

mgmt = graph.openManagement()
visits = mgmt.makeEdgeLabel('visits').make()
mgmt.setTTL(visits, Duration.ofDays(7))
mgmt.commit()
  • 同一個label標識的邊具有相同TTL。
  • 對邊做修改(只能修改名稱)后會重置TTL
  • TTL可以被修改
  • 修改一個邊label的TTL會影響集群中所有label,但是需要時間去傳遞修改,所以在同一時間有可能存在相同label具有不同TTL的情況。
    *需要數據庫支持Cell 級TTL

Property TTL

mgmt = graph.openManagement()
sensor = mgmt.makePropertyKey('sensor').cardinality(Cardinality.LIST).dataType(Double.class).make()
mgmt.setTTL(sensor, Duration.ofDays(21))
mgmt.commit()
  • 和Edge TTL一樣:相同key的property具有相同TTL,修改property會重置TTL,TTL修改后要想產生全局影響需要一些時間。
    *需要數據庫支持Cell 級TTL

Vertex TTL

mgmt = graph.openManagement()
tweet = mgmt.makeVertexLabel('tweet').setStatic().make()
mgmt.setTTL(tweet, Duration.ofHours(36))
mgmt.commit()
  • 相同label的頂點具有同樣的TTL。且只有static的頂點label才可以設置TTL
  • 頂點的TTL會作用到其Edge和Property,所以頂點被刪除時其Edge和Property都會被刪除。
  • 需要數據庫支持數據庫級別的TTL

Multi-Properties

  • JanusGraph中的屬性可以定義List、Set基數,所以一個頂點可以具有多個相同key的屬性。
  • JanusGraph可以為屬性提供注解屬性。
  • 案例:為作者屬性添加注解屬性以表明作者頭銜。
mgmt = graph.openManagement()
mgmt.makePropertyKey('name').dataType(String.class).cardinality(Cardinality.LIST).make()
mgmt.commit()
v = graph.addVertex()
p1 = v.property('name', 'Dan LaRocque')
p1.property('source', 'web')
p2 = v.property('name', 'dalaro')
p2.property('source', 'github')
graph.tx().commit()
v.properties('name')
==> Iterable over all name properties

單向邊

  • 占用少的存儲空間
  • 只可以從out-going方向遍歷單向邊,注意不是out-vertex。
  • 使用位置:正常的邊都是用在2個頂點上,但是單向邊out是被用在邊和屬性上,in被用在頂點上。
  • 當單向邊的in-vertexs被刪除時,單向邊并不會被刪除,
  • Note, that unidirected edges do not get automatically deleted when their in-vertices are deleted. The user must ensure that such inconsistencies do not arise or resolve them at query time by explicitly checking vertex existence in a transaction. See the discussion in Section 29.2.2, “Ghost Vertices” for more information.
mgmt = graph.openManagement()
mgmt.makeEdgeLabel('author').unidirected().make()
mgmt.commit()
user = graph.addVertex()
book = graph.addVertex()
author = graph.addVertex()
user.addE('knows', book).property('author', author)
在user到book的knows邊上加了一個單向邊author指向author節點,從而可以存儲user的相關信息。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容