Cypher 和 nGQL

基本概念对比

概念名称CyphernGQL
vertex, nodenodevertex
edge, relationshiprelationshipedge
vertex typelabeltag
edge typerelationship typeedge type
vertex identifiernode id generated by defaultvid
edge identifieredge id generated by defaultsrc, dst, rank

图基本操作

操作CyphernGQL
列出所有 labels/tagsMATCH (n) RETURN distinct labels(n);
call db.labels();
SHOW TAGS
插入指定类型的点CREATE (:Person {age: 16})INSERT VERTEX <tag_name> (prop_name_list) VALUES <vid>:(prop_value_list)
插入指定类型的边CREATE (src)-[:LIKES]->(dst)
SET rel.prop = V
INSERT EDGE <edge_type> ( prop_name_list ) VALUES <src_vid> -> <dst_vid>[@<rank>]: ( prop_value_list )
删除点MATCH (n) WHERE ID(n) = vid
DETACH DELETE n
DELETE VERTEX <vid>
删除边MATCH ()-[r]->() WHERE ID(r)=edgeID
DELETE r
DELETE EDGE <edge_type> <src_vid> -> <dst_vid>[@<rank>]
更新点属性SET n.name = VUPDATE VERTEX <vid> SET <update_columns>
查询指定点的属性MATCH (n)
WHERE ID(n) = vid
RETURN properties(n)
FETCH PROP ON <tag_name> <vid>
查询指定边的属性MATCH (n)-[r]->()
WHERE ID(r)=edgeID
return properties(r)
FETCH PROP ON <edge_type> <src_vid> -> <dst_vid>[@<rank>]
查询指定点的某一类关系MATCH (n)-[r:edge_type]->() WHERE ID(n) = vidGO FROM <vid> OVER <edge_type>
指定点的某一类反向关系MATCH (n)<-[r:edge_type]-() WHERE ID(n) = vidGO FROM <vid> OVER <edge_type> REVERSELY
指定点某一类关系第 N-Hop 查询MATCH (n)-[r:edge_typeN]->()
WHERE ID(n) = vid
return r
GO N STEPS FROM <vid> OVER <edge_type>
两点路径MATCH p =(a)-[]->(b)
WHERE ID(a) = a_vid AND ID(b) = b_vid
RETURN p
FIND ALL PATH FROM <a_vid> TO <b_vid> OVER

示例查询

示例使用以下数据:

image

  • 插入数据
  1. # 插入点
  2. nebula> INSERT VERTEX character(name, age, type) VALUES hash("saturn"):("saturn", 10000, "titan"), hash("jupiter"):("jupiter", 5000, "god");
  3. # 插入边
  4. nebula> INSERT EDGE father() VALUES hash("jupiter")->hash("saturn"):();
  5. // cypher
  6. cypher> CREATE (src:character {name:"saturn", age: 10000, type:"titan"})
  7. > CREATE (dst:character {name:"jupiter", age: 5000, type:"god"})
  8. > CREATE (src)-[rel:father]->(dst)
  9. ```
  10. - 删除点
  11. ```ngql
  12. nebula> DELETE VERTEX hash("prometheus");
  13. cypher> MATCH (n:character {name:"prometheus"})
  14. > DETACH DELETE n
  • 更新点的属性
  1. nebula> UPDATE VERTEX hash("jesus") SET character.type = 'titan';
  2. cypher> MATCH (n:character {name:"jesus"})
  3. > SET n.type = 'titan'
  • 查看点的属性
  1. nebula> FETCH PROP ON character hash("saturn");
  2. ===================================================
  3. | character.name | character.age | character.type |
  4. ===================================================
  5. | saturn | 10000 | titan |
  6. ---------------------------------------------------
  7. cypher> MATCH (n:character {name:"saturn"})
  8. > RETURN properties(n)
  9. ╒════════════════════════════════════════════╕
  10. "properties(n)"
  11. ╞════════════════════════════════════════════╡
  12. │{"name":"saturn","type":"titan","age":10000}│
  13. └────────────────────────────────────────────┘
  • 查询 hercules 祖父的姓名
  1. nebula> GO 2 STEPS FROM hash("hercules") OVER father YIELD $$.character.name;
  2. =====================
  3. | $$.character.name |
  4. =====================
  5. | saturn |
  6. ---------------------
  7. cypher> MATCH (src:character{name:"hercules"})-[r:father*2]->(dst:character)
  8. > RETURN dst.name;
  9. ╒══════════╕
  10. "dst.name"
  11. ╞══════════╡
  12. "satun"
  13. └──────────┘
  • 查询 hercules 父亲的姓名
  1. nebula> GO FROM hash("hercules") OVER father YIELD $$.character.name;
  2. =====================
  3. | $$.character.name |
  4. =====================
  5. | jupiter |
  6. ---------------------
  7. cypher> MATCH (src:character{name:"hercules"})-[r:father]->(dst:character)
  8. > RETURN dst.name
  9. ╒══════════╕
  10. "dst.name"
  11. ╞══════════╡
  12. "jupiter"
  13. └──────────┘
  • 查询百岁老人的姓名
  1. nebula> # coming soon
  2. cypher> MATCH (src:character)
  3. > WHERE src.age > 100
  4. > RETURN src.name
  5. ╒═══════════╕
  6. "src.name"
  7. ╞═══════════╡
  8. "saturn"
  9. ├───────────┤
  10. "jupiter"
  11. ├───────────┤
  12. "neptune"
  13. │───────────│
  14. "pluto"
  15. └───────────┘
  • 找出 pluto 和谁住
  1. nebula> GO FROM hash("pluto") OVER lives YIELD lives._dst AS place | GO FROM $-.place OVER lives REVERSELY WHERE \
  2. > $$.character.name != "pluto" YIELD $$.character.name AS cohabitants;
  3. ===============
  4. | cohabitants |
  5. ===============
  6. | cerberus |
  7. ---------------
  8. cypher> MATCH (src:character{name:"pluto"})-[r1:lives]->()<-[r2:lives]-(dst:character)
  9. > RETURN dst.name
  10. ╒══════════╕
  11. "dst.name"
  12. ╞══════════╡
  13. "cerberus"
  14. └──────────┘
  • 查询 Pluto 的兄弟们以及他们的居住地
  1. nebula> GO FROM hash("pluto") OVER brother YIELD brother._dst AS god | \
  2. > GO FROM $-.god OVER lives YIELD $^.character.name AS Brother, $$.location.name AS Habitations;
  3. =========================
  4. | Brother | Habitations |
  5. =========================
  6. | jupiter | sky |
  7. -------------------------
  8. | neptune | sea |
  9. -------------------------
  10. cypher> MATCH (src:Character{name:"pluto"})-[r1:brother]->(bro:Character)-[r2:lives]->(dst)
  11. > RETURN bro.name, dst.name
  12. ╒═════════════════════════╕
  13. "bro.name" "dst.name"
  14. ╞═════════════════════════╡
  15. "jupiter" "sky"
  16. ├─────────────────────────┤
  17. "neptune" "sea"
  18. └─────────────────────────┘