CYPHER 语句(Neo4j)
Posted aongao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CYPHER 语句(Neo4j)相关的知识,希望对你有一定的参考价值。
CYPHER 语句(Neo4j)
创建电影关系图
CREATE (TheMatrix:Movie {title:\'The Matrix\', released:1999, tagline:\'Welcome to the Real World\'})
CREATE (Keanu:Person {name:\'Keanu Reeves\', born:1964})
CREATE (Carrie:Person {name:\'Carrie-Anne Moss\', born:1967})
CREATE (Laurence:Person {name:\'Laurence Fishburne\', born:1961})
CREATE (Hugo:Person {name:\'Hugo Weaving\', born:1960})
CREATE (LillyW:Person {name:\'Lilly Wachowski\', born:1967})
CREATE (LanaW:Person {name:\'Lana Wachowski\', born:1965})
CREATE (JoelS:Person {name:\'Joel Silver\', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:[\'Neo\']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:[\'Trinity\']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:[\'Morpheus\']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:[\'Agent Smith\']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix)
CREATE (Emil:Person {name:"Emil Eifrem", born:1978})
CREATE (Emil)-[:ACTED_IN {roles:["Emil"]}]->(TheMatrix)
CREATE (TheMatrixReloaded:Movie {title:\'The Matrix Reloaded\', released:2003, tagline:\'Free your mind\'})
CREATE
(Keanu)-[:ACTED_IN {roles:[\'Neo\']}]->(TheMatrixReloaded),
(Carrie)-[:ACTED_IN {roles:[\'Trinity\']}]->(TheMatrixReloaded),
(Laurence)-[:ACTED_IN {roles:[\'Morpheus\']}]->(TheMatrixReloaded),
(Hugo)-[:ACTED_IN {roles:[\'Agent Smith\']}]->(TheMatrixReloaded),
(LillyW)-[:DIRECTED]->(TheMatrixReloaded),
(LanaW)-[:DIRECTED]->(TheMatrixReloaded),
(JoelS)-[:PRODUCED]->(TheMatrixReloaded)
(Keanu)-[:ACTED_IN {roles:[\'Julian Mercer\']}]->(SomethingsGottaGive),
(NancyM)-[:DIRECTED]->(SomethingsGottaGive),
(NancyM)-[:PRODUCED]->(SomethingsGottaGive),
(NancyM)-[:WROTE]->(SomethingsGottaGive)
查找
查看节点所有属性
match (n:LOGIC_SUBJECT) unwind keys(n) as key return distinct key;
属性查找
#查找无age属性的节点
match(n:Person) WHERE n.age is not null RETURN n
#查找有age属性的节点
match(n:Person) WHERE n.age is null RETURN n
查找两个节点是否存在多个相同关系
match(m)-[r]-(n) where size((m)-[:relationship]-(n))>1 return *
模糊查找
#查找name中以"J"开头的内容
match(n:Person) where n.NAME=~ \'J.*\' return n
查询标签属性信息
match (n:场景) unwind keys(n) as key return distinct key;
新增
新增节点
#新增Jack T 和 Tom T 两个人
create (:Person{name:\'Jack T\'}),(:Person{name:\'Tom T\'}) match(m:Person{name:\'Jack T\'}),(n:Person{name:\'Tom T\'}) return n,m
新增边
#Jack T 是Tom T的朋友
match(m:Person{name:\'Jack T\'}),(n:Person{name:\'Tom T\'}) create (m)-[r:friend_to]->(n) return n,r,m
新增属性
match(n:Person) where n.name=\'Jack T\' set n+={age:15} return n
修改
修改属性
#单个节点
match(n:Person) where n.name=\'Keanu Reeves\' set n.age=16 return n
#批量节点 如果没有改属性,则会新增
match(n:Person) set n.age=16 return n
复制属性
match (m { name:\'Keanu Reeves\' }),(n { name: \'Hugo Weaving\' }) set n = m return n,m
修改节点名称
match(n:INTERFACE) set n:T_BDM_ITFS match(n:T_BDM_ITFS) remove n:INTERFACE
字符串处理
截取部分字符串
MATCH (n:LOGIC_SUBJECT) RETURN substring(n.NAME,0,4)
删除
删除属性
#单个节点删除属性
match(n:Person) where n.age=17 remove n.age
#批量删除属性
match(n:Person) remove n.age
导入
CSV文件
#import 文件夹下
load csv with headers from \'file:///fileName.csv\' as newLables create(n:LableNewName) set Lable = newLables
LOAD CSV FROM \'file:///abc.csv\' AS line
CREATE (:T_BDM_PANORAMA {name: line[2], sid:line[1]})
格式转换
LOAD CSV FROM \'https://neo4j.com/docs/developer-manual/3.3/csv/artists.csv\' AS line #字符串转换成整型 toInteger
CREATE (:Artist { name: line[1], year: toInteger(line[2])})
数据迁移
match(n:PROJECT_SG_CIM3_5) return n.name as name,id(n) as oldID; match(n:PROJECT_SG_CIM3_5)-[r]->(m:PROJECT_SG_CIM3_5) return id(startNode(r)) as startId_BUS_CLASS,id(endNode(r)) as endId_BUS_CLASS; load csv with headers from "file:///a661.csv" as row match (n:LOGIC_OBJECT{oldID:row.startId_BUS_CLASS}),(m:LOGIC_OBJ_DETAIL{oldID:row.endId_BUS_CLASS}) create (n)-[:LOG_OBJ_TO_OBJ_DET]->(m);
LOAD CSV WITH HEADERS FROM "file:///label.csv" AS line
create (:SYS{id:line.ID,name:line.NAME})
load csv with headers from "file:///relation.csv" as row match (n:SYS{id:row.first}),(m:SYS{id:row.second})
create (n)-[:LINE{name:row.NAME}]->(m);
以上是关于CYPHER 语句(Neo4j)的主要内容,如果未能解决你的问题,请参考以下文章
Cypher Neo4J - 使用 MERGE 的 CASE 表达式