赞
踩
Neo4j系列导航:
neo4j及简单实践
cypher语法基础
cypher插入语法
cypher插入语法
cypher查询语法
cypher通用语法
cypher函数语法
neo4j索引及调优
create, merge, set, delete, remove, create unique, load csv(import)
创建单个节点:
create(n)
# (ID:n)
创建带标签的节点:
create (n:Person)
# (ID:n, label:Person)
创建带多个标签的节点:
create (n:Person:Student:Man)
# (ID:n, label:Person,Student,Man)
创建带标签和属性的节点:
create (n:Person {name:"张三",age:18})
# (ID:n, label:Person, {name:“张三”})
返回创建的节点:
create (n:Person {name:"张三",age:18}) return n
创建多个节点:
create (n:Person {name:"张三"}), (m:Person {name:"李四"}) return n, m
创建节点和关系:
create (n:Person {name:"张三"})-[r:Friend]->(m:Person {name:"李四"})
创建节点和带属性的关系:
create (n:Person {name:"张三"})-[r:Friend {time:8}]->(m:Person {name:"李四"})
创建关系(节点已存在):
match (n:Person{name:“张三”}), (m:Person{name:“李四”}) create (n)-[r:Friend{time:1994}]->(m) retur r
创建一个完整路径:
create p =(n:Person {name:"张三"})-[:Friend]->(m:Person{name:"李四"})<-[:Friend]-(s:Person{name:"王五"}) return p
是match和create的结合,可以确保模式在graph中存在(要么已经存在要么创建)。为了性能考虑,在标签和属性中创建了索引时推荐使用merge。
merge单个节点:
merge (n:Person) return n,labels(n)
merge带属性的节点:
merge (n{name:"张三",age:18}) return n
merge带标签和属性的节点:
merge(n:Person{name:"张三", age:18}) return n.name, n.age
合并来自已存在节点的单个节点的属性:
match (person:Person{name:"张三") merge (city:City{name:person.name}) return person, city
merge (n:Person{name:"name01"}) on create set n.created=timestamp() return n
merge (n:Person{name:"张三"}) on match set person.sex="男" return n
merge (n:Person{name:"张三"}) on create set n.created = timestamp() on match set n.lastSeen = timestamp() return n
merge一个关系:
match (n:Person {name:"张三"}),(m:Person {name:"李四"}) merge (n)-[r:Friend]->(m) return r
merge多个关系:
match (n:Person {name:"张三"}),(m:Person {name:"李四"}) merge (n)-[:Watch]->(movie:Movie{name:"万达”,created:timestamp()})<-[:Watch]-(m) return movie
merge非直接关系:
match (n:Person {name:"张三"}),(m:Person {name:"李四"}) merge (n)-[r:Watch]-(m) RETURN r
创建索引:
create constraint on (n:Person) assert n.name is unique
create constraint on (n:Person) assert n.age IS unique
如果节点未找到,使用唯一性约束创建该节点:
merge (n:Person {name:"张三"}) return n
用于更新一个节点和关系的标签或属性(属性存在则更新,不存在则创建)
match (n:Person {name:"张三"}) set n.age=21 return n
match (n:Person {name:"张三"}) set n.age=null return n
Neo4j不允许属性存储空值null。如果属性的值不存在,那么节点或者关系中的属性将被删除
match (n:Person {name:"张三"}),(m:movie {created:1711093191056}) set n=m return n,m
match (n:Person {name:"张三"}) set n+={age:18, sex:"男"} return n
当用map来设置属性时,可以使用+=形式的SET来只添加属性,而不删除图元素中已存在的属性
match (n:Person {name:"张三"}) set n.age=21, n.sex='男' return n
match (n:Person{name:"张三"}) set n:Chinese return n
match (n:Person{name:"张三"}) set n:Chinese:Student return n
用于删除节点和关系
match (n:Person) delete n
match (n) where size ((n)--())=0 delete n
match (n:Person{name:"张三" })-[r]-() delete n, r
match (n {name:"张三"}) detach delete n
match p=(d{name:"张三"})--(e{ name:"王五"}) delete p
match (n) optional match (n)-[r]-() delete n,r
start n=node(*) match (n)-[r]-() delete n,r
match (n) detach delete n
用于删除图元素的属性和标签
match (n:Person{name:"张三"}) remove n.age return n
match (n:Person{name:"张三"}) remove n:Person return n
match (n:Person{name:"张三"}) remove n:Person:Student return n
match (n:Person{name:"张三"}) set n={} return n
foreach能够用于更新每一个path或者list中的元素,在foreach内可以使用createE创建节点
match p=(begin)-[*]->(end) where begin.name='A' and end.name='E' foreach (n in nodes(p)| set n.age=21)
create unique语句相当于match和create的结合体,尽可能的匹配然后创建未匹配到的
match (n{name:"name1"}) create unique (n)-[:Friend]-(e) return e
match (n{name:"name1"}) create unique (n)-[:Friend]->(e), (n)-[:Teacher]->(e) return e
用于导入csv文件,from后面接着csv的文件路径,用AS指定给变量。可以从http、https、ftp导入csv,支持gzip and deflate,其本地文件支持zip压缩, 经常和periodic commit一起使用,提交大量的数据,默认1000rows提交一次
导入数据:
load csv from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists.csv' ad line create (:Artist {name:line[1], year:toInteger(line[2])})
导入带表头的数据:
load csv with headers from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists-with-headers.csv' as line create (:Artist {name:line.Name, year:toInteger(line.Year)})
文件artists-with-headers.csv列: Id,Name,Year
导入数据并指定分隔符:
load csv from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists-fieldterminator.csv' as line fieldterminator ';' create (:Artist { name: line[1], year: toInteger(line[2])})
导入大批量数据并指定每次提交行数:
using periodic commit 500 load csv from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists.csv' as line create (:Artist { name: line[1], year: toInteger(line[2])})
using periodic commit 后面接每次提交的行数,默认为1000
获取导入数据的行数linenumber():
load csv from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists.csv' as line return linenumber() as number, line
获取导入文件的绝对路径:
load csv from 'https://neo4j.com/docs/cypher-manual/4.1/csv/artists.csv' as line return distinct file() as path
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。