Gremlin入門


Gremlin入門

一、Gremlin簡介

Gremlin是Apache ThinkerPop框架下的圖遍歷語言,Gremlin是一種函數式數據流語言,可以使用戶使用簡潔的方式表述復雜的屬性圖的遍歷或查詢。每個Gremlin遍歷由一系列步驟(可能存在嵌套)組成,每一步都在數據流(data stream)上執行一個原子操作。

Gremlin 語言包括三個基本的操作:

  • map-step:對數據流中的對象進行轉換;
  • filter-step:對數據流中的對象就行過濾;
  • sideEffect-step:對數據流進行計算統計;

Tinkerpop3 模型核心概念

  • Graph: 維護節點&邊的集合,提供訪問底層數據庫功能,如事務功能
  • Element: 維護屬性集合,和一個字符串label,表明這個element種類
  • Vertex: 繼承自Element,維護了一組入度,出度的邊集合
  • Edge: 繼承自Element,維護一組入度,出度vertex節點集合.
  • Property: kv鍵值對
  • VertexProperty: 節點的屬性,有一組健值對kv,還有額外的properties 集合。同時也繼承自element,必須有自己的id, label.
  • Cardinality: 「single, list, set」 節點屬性對應的value是單值,還是列表,或者set。

二、Gremlin查詢示例

先介紹一下圖中比較核心的幾個概念:

  • Schema:Schema是一種描述語言,這里就是指所有屬性和類型的集合,包括邊和點的屬性,邊和點的Label等;
  • 屬性類型(PropertyKey ):只邊和點可以使用的屬性類型;
  • 頂點類型(VertexLabel):頂點的類型,比如User,Car等;
  • 邊類型(EdgeLabel):邊的類型,比如know,use等;
  • 頂點(Vertex):就是圖中的頂點,代表圖中的一個節點;
  • 邊(Edge):就是圖中的邊,連接兩個節點,分為有向邊和無向邊;

創建屬性類型

graph.schema().propertyKey("name").asText().ifNotExist().create()
graph.schema().propertyKey("age").asInt().ifNotExist().create()
graph.schema().propertyKey("city").asText().ifNotExist().create()
graph.schema().propertyKey("lang").asText().ifNotExist().create()
graph.schema().propertyKey("date").asText().ifNotExist().create()
graph.schema().propertyKey("price").asInt().ifNotExist().create()

創建頂點類型

person = graph.schema().vertexLabel("person").properties("name", "age", "city").primaryKeys("name").ifNotExist().create()
software = graph.schema().vertexLabel("software").properties("name", "lang", "price").primaryKeys("name").ifNotExist().create()

創建邊類型

knows = graph.schema().edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("date").ifNotExist().create()
created = graph.schema().edgeLabel("created").sourceLabel("person").targetLabel("software").properties("date", "city").ifNotExist().create()

創建頂點和邊

marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing")
vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong")
lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328)
josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing")
ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199)
peter = graph.addVertex(T.label, "person","name", "peter", "age", 29, "city", "Shanghai")

marko.addEdge("knows", vadas, "date", "20160110")
marko.addEdge("knows", josh, "date", "20130220")
marko.addEdge("created", lop, "date", "20171210", "city", "Shanghai")
josh.addEdge("created", ripple, "date", "20151010", "city", "Beijing")
josh.addEdge("created", lop, "date", "20171210", "city", "Beijing")
peter.addEdge("created", lop, "date", "20171210", "city", "Beijing")

展示圖

g.V() //創建使用graph,查詢使用g,其實g就是graph.traversal()

查詢點

g.V().limit(5) // 查詢所有點,但限制點的返回數量為5,也可以使用range(x, y)的算子,返回區間內的點數量。
g.V().hasLabel('person') // 查詢點的label值為'person'的點。
g.V('11') // 查詢id為‘11’的點。

查詢邊

g.E() // 查詢所有邊,不推薦使用,邊數過大時,這種查詢方式不合理,一般需要添加過濾條件或限制返回數量。
g.E('55-81-5') // 查詢邊id為‘55-81-5’的邊。
g.E().hasLabel('knows') // 查詢label為‘knows’的邊。
g.V('46').outE('knows') // 查詢點id為‘46’所有label為‘knows’的邊。

查詢屬性

g.V().limit(3).valueMap() // 查詢點的所有屬性(可填參數,表示只查詢該點, 一個點所有屬性一行結果)。
g.V().limit(1).label() // 查詢點的label。
g.V().limit(10).values('name') // 查詢點的name屬性(可不填參數,表示查詢所有屬性, 一個點每個屬性一行結果,只有value,沒有key)。

刪除點

g.V('600').drop() // 刪除ID為600的點。

刪除邊

g.E('501-502-0').drop() //刪除ID為“501-502-0”的邊。

查詢二度好友和共同好友數

//查詢一度好友
g.V('1500771').out()
//查詢二度好友
g.V('1500771').out().out().dedup().not(hasId('1500771'))
//查詢共同好友數
g.V('1500771').out().out().hasId('2165197').path().simplePath().count()

此外,還有查詢,遍歷,過濾,路徑,迭代,轉換,排序,邏輯,統計,分支等語法,可以參考:http://tang.love/2018/11/15/gremlin_traversal_language/。

參考:

http://tang.love/2018/11/15/gremlin_traversal_language/

https://hugegraph.github.io/hugegraph-doc/quickstart/hugegraph-studio.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM