使用Java进行查询hugegraph_HugeGraph Examples
HugeGraph Examples
1 概述
本示例將TitanDB Getting Started 為模板來演示HugeGraph的使用方法。通過對比HugeGraph和TitanDB,了解HugeGraph和TitanDB的差異。
1.1 HugeGraph與TitanDB的異同
HugeGraph和TitanDB都是基于Apache TinkerPop3框架的圖數據庫,均支持Gremlin圖查詢語言,在使用方法和接口方面具有很多相似的地方。然而HugeGraph是全新設計開發的,其代碼結構清晰,功能較為豐富,接口更為友好等特點。
HugeGraph相對于TitanDB而言,其主要特點如下:
HugeGraph目前有HugeGraph-API、HugeGraph-Client、HugeGraph-Loader、HugeGraph-Studio、HugeGraph-Spark等完善的工具組件,可以完成系統集成、數據載入、圖可視化查詢、Spark 連接等功能;
HugeGraph具有Server和Client的概念,第三方系統可以通過jar引用、client、api等多種方式接入,而TitanDB僅支持jar引用方式接入。
HugeGraph的Schema需要顯式定義,所有的插入和查詢均需要通過嚴格的schema校驗,目前暫不支持schema的隱式創建。
HugeGraph充分利用后端存儲系統的特點來實現數據高效存取,而TitanDB以統一的Kv結構無視后端的差異性。
HugeGraph的更新操作可以實現按需操作(例如:更新某個屬性)性能更好。TitanDB的更新是read and update方式。
HugeGraph的VertexId和EdgeId均支持拼接,可實現自動去重,同時查詢性能更好。TitanDB的所有Id均是自動生成,查詢需要經索引。
1.2 人物關系圖譜
本示例通過Property Graph Model圖數據模型來描述希臘神話中各人物角色的關系(也被成為人物關系圖譜),具體關系詳見下圖。
其中,圓形節點代表實體(Vertex),箭頭代表關系(Edge),方框的內容為屬性。
該關系圖譜中有兩類頂點,分別是人物(character)和位置(location)如下表:
名稱
類型
屬性
character
vertex
name,age,type
location
vertex
name
有六種關系,分別是父子(father)、母子(mother)、兄弟(brother)、戰斗(battled)、居住(lives)、擁有寵物(pet) 關于關系圖譜的具體信息如下:
名稱
類型
source vertex label
target vertex label
屬性
father
edge
character
character
-
mother
edge
character
character
-
brother
edge
character
character
-
pet
edge
character
character
-
lives
edge
character
location
reason
在HugeGraph中,每個edge label只能作用于一對source vertex label和target vertex label。也就是說,如果一個圖內定義了一種關系father連接character和character,那farther就不能再連接其他的vertex labels。
因此本例子將原TitanDB中的monster, god, human, demigod均使用相同的vertex label: character來表示, 同時增加屬性type來標識人物的類型。edge label與原TitanDB保持一致。當然為了滿足edge label約束,也可以通過調整edge label的name來實現。
2 Graph Schema and Data Ingest Examples
HugeGraph需要顯示創建Schema,因此需要依次創建PropertyKey、VertexLabel、EdgeLabel,如果有需要索引還需要創建IndexLabel。
2.1 Graph Schema
schema = hugegraph.schema()
schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("age").asInt().ifNotExist().create()
schema.propertyKey("time").asInt().ifNotExist().create()
schema.propertyKey("reason").asText().ifNotExist().create()
schema.propertyKey("type").asText().ifNotExist().create()
schema.vertexLabel("character").properties("name", "age", "type").primaryKeys("name").nullableKeys("age").ifNotExist().create()
schema.vertexLabel("location").properties("name").primaryKeys("name").ifNotExist().create()
schema.edgeLabel("father").link("character", "character").ifNotExist().create()
schema.edgeLabel("mother").link("character", "character").ifNotExist().create()
schema.edgeLabel("battled").link("character", "character").properties("time").ifNotExist().create()
schema.edgeLabel("lives").link("character", "location").properties("reason").nullableKeys("reason").ifNotExist().create()
schema.edgeLabel("pet").link("character", "character").ifNotExist().create()
schema.edgeLabel("brother").link("character", "character").ifNotExist().create()
2.2 Graph Data
// add vertices
Vertex saturn = graph.addVertex(T.label, "character", "name", "saturn", "age", 10000, "type", "titan")
Vertex sky = graph.addVertex(T.label, "location", "name", "sky")
Vertex sea = graph.addVertex(T.label, "location", "name", "sea")
Vertex jupiter = graph.addVertex(T.label, "character", "name", "jupiter", "age", 5000, "type", "god")
Vertex neptune = graph.addVertex(T.label, "character", "name", "neptune", "age", 4500, "type", "god")
Vertex hercules = graph.addVertex(T.label, "character", "name", "hercules", "age", 30, "type", "demigod")
Vertex alcmene = graph.addVertex(T.label, "character", "name", "alcmene", "age", 45, "type", "human")
Vertex pluto = graph.addVertex(T.label, "character", "name", "pluto", "age", 4000, "type", "god")
Vertex nemean = graph.addVertex(T.label, "character", "name", "nemean", "type", "monster")
Vertex hydra = graph.addVertex(T.label, "character", "name", "hydra", "type", "monster")
Vertex cerberus = graph.addVertex(T.label, "character", "name", "cerberus", "type", "monster")
Vertex tartarus = graph.addVertex(T.label, "location", "name", "tartarus")
// add edges
jupiter.addEdge("father", saturn)
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes")
jupiter.addEdge("brother", neptune)
jupiter.addEdge("brother", pluto)
neptune.addEdge("lives", sea, "reason", "loves waves")
neptune.addEdge("brother", jupiter)
neptune.addEdge("brother", pluto)
hercules.addEdge("father", jupiter)
hercules.addEdge("mother", alcmene)
hercules.addEdge("battled", nemean, "time", 1)
hercules.addEdge("battled", hydra, "time", 2)
hercules.addEdge("battled", cerberus, "time", 12)
pluto.addEdge("brother", jupiter)
pluto.addEdge("brother", neptune)
pluto.addEdge("lives", tartarus, "reason", "no fear of death")
pluto.addEdge("pet", cerberus)
cerberus.addEdge("lives", tartarus)
2.3 Indices
HugeGraph默認是自動生成Id,如果用戶通過primaryKeys指定VertexLabel的primaryKeys字段列表后,VertexLabel的Id策略將會自動切換到primaryKeys策略。 啟用primaryKeys策略后,HugeGraph通過vertexLabel+primaryKeys拼接生成VertexId ,可實現自動去重,同時無需額外創建索引即可以使用primaryKeys中的屬性進行快速查詢。 例如 "character" 和 "location" 都有primaryKeys("name")屬性,因此在不額外創建索引的情況下可以通過g.V().hasLabel('character') .has('name','hercules')查詢vertex 。
3 Graph Traversal Examples
3.1 Traversal Query
1. Find the grand father of hercules
g.V().hasLabel('character').has('name','hercules').out('father').out('father')
也可以通過repeat方式:
g.V().hasLabel('character').has('name','hercules').repeat(__.out('father')).times(2)
2. Find the name of hercules's father
g.V().hasLabel('character').has('name','hercules').out('father').value('name')
3. Find the characters with age > 100
g.V().hasLabel('character').has('age',gt(100))
4. Find who are pluto's cohabitants
g.V().hasLabel('character').has('name','pluto').out('lives').in('lives').values('name')
5. Find pluto can't be his own cohabitant
pluto = g.V().hasLabel('character').has('name', 'pluto')
g.V(pluto).out('lives').in('lives').where(is(neq(pluto)).values('name')
// use 'as'
g.V().hasLabel('character').has('name', 'pluto').as('x').out('lives').in('lives').where(neq('x')).values('name')
6. Pluto's Brothers
pluto = g.V().hasLabel('character').has('name', 'pluto').next()
// where do pluto's brothers live?
g.V(pluto).out('brother').out('lives').values('name')
// which brother lives in which place?
g.V(pluto).out('brother').as('god').out('lives').as('place').select('god','place')
// what is the name of the brother and the name of the place?
g.V(pluto).out('brother').as('god').out('lives').as('place').select('god','place').by('name')
推薦使用HugeGraph-Studio 通過可視化的方式來執行上述代碼。另外也可以通過HugeGraph-Client、HugeApi、GremlinConsole和GremlinDriver等多種方式執行上述代碼。
3.2 總結
HugeGraph目前支持Gremlin的語法,用戶可以通過Gremlin語句實現各種查詢需求,但是目前HugeGraph暫不支持全文檢索功能。
總結
以上是生活随笔為你收集整理的使用Java进行查询hugegraph_HugeGraph Examples的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 鼠标拖动图形_java怎么实现
- 下一篇: java redis使用卡死_记一次找因