1、Schema
1、屬性
schema.propertyKey("name").asText().valueSingle().ifNotExist().create();
允許定義的約束信息包括:name,datatype,cardinality,userdata;
userdata 是用戶可以添加的附加屬性。
2、頂點
// 使用 Automatic 的 Id 策略
schema.vertexLabel("person").properties("name", "age").ifNotExist().create();
schema.vertexLabel("person").useAutomaticId().properties("name", "age").ifNotExist().create();
// 使用 Customize_String 的 Id 策略
schema.vertexLabel("person").useCustomizeStringId().properties("name", "age").ifNotExist().create();
// 使用 Customize_Number 的 Id 策略
schema.vertexLabel("person").useCustomizeNumberId().properties("name", "age").ifNotExist().create();
// 使用 PrimaryKey 的 Id 策略
schema.vertexLabel("person").properties("name", "age").primaryKeys("name").ifNotExist().create();
schema.vertexLabel("person").usePrimaryKeyId().properties("name", "age").primaryKeys("name").ifNotExist().create();
// 刪除節點
schema.vertexLabel("person").remove();
// 獲取VertexLabel對象
schema.getVertexLabel("name")
// 獲取property key屬性
schema.getVertexLabel("person").idStrategy()
schema.getVertexLabel("person").primaryKeys()
schema.getVertexLabel("person").name()
schema.getVertexLabel("person").properties()
schema.getVertexLabel("person").nullableKeys()
schema.getVertexLabel("person").userdata()
允許定義的約束信息包括:name,idStrategy,properties, primaryKeys, nullableKeys
3、邊
允許定義的約束信息包括:name,sourceLabel,targetLabel,frequency,properties,sortKeys,和 nullableKeys。
frequency 表示兩個頂點間某個關係可能出現的次數,singleTime() 或 multiTimes(),比如進程之間的連接可以爲多個。
// 創建邊
schema.edgeLabel("knows").link("person", "person").properties("date").ifNotExist().create();
schema.edgeLabel("created").multiTimes().link("person", "software")
.properties("date").sortKeys("date").ifNotExist().create();
// 刪除邊
schema.edgeLabel("knows").remove();
// 獲取EdgeLabel對象
schema.getEdgeLabel("knows")
// 獲取property key屬性
schema.getEdgeLabel("knows").frequency()
schema.getEdgeLabel("knows").sourceLabel()
schema.getEdgeLabel("knows").targetLabel()
schema.getEdgeLabel("knows").sortKeys()
schema.getEdgeLabel("knows").name()
schema.getEdgeLabel("knows").properties()
schema.getEdgeLabel("knows").nullableKeys()
schema.getEdgeLabel("knows").userdata()
4、索引
允許定義的約束信息包括:name,baseType, vaseValue, indexFields, indexType。
baseType,baseValue 配合使用,用於區分是爲 VertexLabel 還是 EdgeLabel 建立索引。
索引類型 | 說明 | 示例 |
---|---|---|
Secondray | 單個屬性支持相等查詢,聯合索引支持前綴查詢和相等查詢 | 1. g.V().has(“city”, “北京”) 查詢 city 屬性爲北京的節點 2. g.V().has(“city”, “北京”).has(“street”, “中關村街道”) |
Range | 支持數值類型的範圍查詢 | 1. g.V().has(“age”, P.gt(18)) 2. 支持 gte()、lte()、lt()、eq(),between()、inside()、outside() |
Search | 支持全文檢索的索引 | g.V().has(“address”, Text.contains(“大廈”)) |
Shard | 支持前綴匹配 + 數字範圍查詢的索引 | g.V().has (“city”, “北京”).has(“age”, P.between(18, 30)) |
Unique | 支持屬性值唯一性約束,即可以限定屬性的值不重複,允許聯合索引,但不支持查詢 | 當出現重複值時將報錯 |
// 創建索引
schema.indexLabel("personByAge").onV("person").by("age").range().ifNotExist().create();
schema.indexLabel("createdByDate").onE("created").by("date").secondary().ifNotExist().create();
schema.indexLabel("personByLived").onE("person").by("lived").search().ifNotExist().create();
schema.indexLabel("personByCityAndAge").onV("person").by("city", "age").shard().ifNotExist().create();
schema.indexLabel("personById").onV("person").by("id").unique().ifNotExist().create();
// 刪除索引
schema.indexLabel("personByAge").remove()
// 獲取IndexLabel對象
schema.getIndexLabel("personByAge")
// 獲取property key屬性
schema.getIndexLabel("personByAge").baseType()
schema.getIndexLabel("personByAge").baseValue()
schema.getIndexLabel("personByAge").indexFields()
schema.getIndexLabel("personByAge").indexType()
schema.getIndexLabel("personByAge").name()
2、Data
1、頂點
Vertex marko = graph.addVertex("label", "person", "name", "marko", "age", 29);
Vertex lop = graph.addVertex("label", "software", "name", "lop", "lang", "java", "price", 328);
- 添加頂點時參數的個數必須爲偶數,對應屬性的 (key,value) 列表;
- 第一組參數對應 vertex label 的信息;
- 對於非 nullableKeys 的屬性,必須要賦值;
- 調用 addVertex 方法後,頂點會立刻插入到存儲系統中;
2、邊
Edge knows1 = marko.addEdge("knows", vadas, "city", "Beijing");
- 源頂點和目標頂點必須符號 EdgeLabel 中 sourceLabel 和 targetLabel 的定義,不能隨意添加;
注意:當frequency爲multiple時必須要設置sortKeys對應屬性類型的值。
3、檢索
1、查找 name = marko 的 person 是否認識 name = josh 的 person
g.V().hasLabel('person').has('name','marko').out('knows')
.hasLabel('person').has('name','josh').hasNext()
如果存在,返回結果
[true]
2、
g.V().hasLabel('person').where(outE("created").count().is(P.gte(2))).count()
3、
g.V().hasLabel('person').has('name','marko').out("knows").hasLabel("person").values("age").min()
4、查找 hercules 的祖父
g.V().hasLabel('character').has('name','hercules').out('father').out('father')
或者
g.V().hasLabel('character').has('name','hercules').repeat(__.out('father')).times(2)
5、查找 hercules 父親的名稱
g.V().hasLabel('character').has('name','hercules').out('father').value('name')
6、查找年齡大於 100 歲的人物
g.V().hasLabel('character').has('age',gt(100))
7、查找與 pluto 生活在一起的人
g.V().hasLabel('character').has('name','pluto').out('lives').in('lives').values('name')
8、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')
9、pluto 的兄弟
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')
4、Gremlin
4.1 在兩個頂點之間檢索
5、Traversal
什麼是 traversal,模型?在兩個點之間遊走,起點,到終點,有四個部分組成:
- Step:Steps are chained within a traversal.
- Strategy: 攔截器方法,
- SideEffects: store global information
- Traverser
參考資料:
http://tinkerpop.apache.org/gremlin.html
https://tinkerpop.apache.org/docs/current/recipes/
http://tinkerpop.apache.org/docs/3.4.6/reference/#dsl
http://tinkerpop.apache.org/docs/current/tutorials/gremlins-anatomy/
http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#gq