文档链接: https://neo4j.com/docs/cypher-manual/4.3/.
Delete用于删除图元素(节点、关系、或路径),若要也删除与之相连的关系则用detach delete。
remove 语句用于删除图元素的属性和标签。
Order by 是紧跟RETURN或者WITH的子句。
比admin慢,比py2neo快。
from py2neo import *
from py2neo.matching import *
test_graph = Graph( # 连接Neo4j,test_graph为自己命名的,后面调用都要使用这个test_graph
"http://localhost:7474",
auth=("neo4j", "neo4j") #前为用户名,后为密码
)
其他可能用到的头文件:
from py2neo.bulk import create_nodes
from py2neo.bulk import merge_nodes
from py2neo.bulk import create_relationships
a = Node("Person", name="Alice") # 前为标签,name为属性
b = Node("Person", name="Bob")
test_graph.create(a) #放入数据库
test_graph.create(b)
a.add_label("Employee") # 添加标签,a的标签变为{'Employee', 'Person'}
print(set(a.labels)) # a结点标签的set集合
a.remove_label("Employee") # 删除标签
print(a.has_label("Person")) # a若有标签Person则返回True
a.clear_labels() # 删除该结点的所有标签
其他:
node[name] 返回名为name的属性的值。
node[name] = value 将名为name的属性的值设置为value。
del node[name] 从此节点中删除名为name的属性。len(node) 返回此节点上的属性数。
dict(node) 返回此节点中所有属性的字典。
clear( ) 从此节点中删除所有属性。
values( ) 返回所有属性值的列表。
get(名称,默认=无) 返回名为name的属性的值。
ab = Relationship(a, 'CALL', b) # 关系的类型为"CALL",两条关系都有属性count,且值为1
ab['count'] = 1 # 设置关系的值
KNOWS = Relationship.type("KNOWS") # 另一种表示的关系建立
ba = KNOWS(b, a)
test_graph.create(ab)
test_graph.create(ba)
ab['count'] += 1 # 节点/关系的属性赋值
test_graph.push(ab) # 属性值的更新
print(ba.nodes) # 打印nodes 开始节点和结束节点的 2 元组。ba.end_node表示结束结点。ba.start_node表示开始结点。
其他:
del ba[‘count’] 删除属性。
clear( )在此关系中删除所有属性。
…
nodes = NodeMatcher(test_graph) # NodeMatcher节点匹配
keanu = nodes.match("Person", name="Keanu Reeves").first() # 对于标签和属性的简单相等匹配
print(keanu)
nodes.match("Person", name=STARTS_WITH("John")).all() # 匹配名称以“John”开头的所有节点,
nodes.match("Person", born=ALL(GE(1964), LE(1966))).all() # 匹配出生于 1964 年至 1966 年(含)的所有人
nodeList = list(nodes.match().where("_.name=~‘杨.*’")) # 查找名字以“杨”开头的所有节点
for i in nodeList:
print(i)
nodeList = list(nodes.match().where("_.name=~‘杨.*’")).order_by("_.name").limit(4) # 加上“排序”“限制返回条数”
其他:
IS_NULL( ),IS_NOT_NULL( ) 空检查谓词。
EQ(值),NE(值)。排序谓词LT(值),LE(值),GT(值),GE(值) 相等谓词。
…
relation_matcher = RelationshipMatcher(test_graph)
re = relation_matcher.match((a,), r_type="KNOWS").first()
print(re)
reList = list(relation_matcher.match()) #查询所有关系
abc = Path(a, "CALL", b, Relationship(c, "KNOWS", b), c)
print(abc.nodes)
print(abc.relationships)
d, e = Node(name="Dave"), Node(name="Eve")
de = Path(d, "KNOWS", e)
abcde = Path(abc, "KNOWS", de) # 两个Path构成一个Path
for relationship in abcde.relationships: # 遍历Path上的关系
print(relationship)
其他:
types( ) 返回此路径上存在的所有关系类型的集合。
start_node。end_node。
…
子图是节点和关系的任意集合。一个子图必须至少包含一个节点,空子图应由None表示。
friends = ab | ba # 并集
test_graph.create(friends)
print(friends)
其他:
keys( )。labels( )。nodes。relationships。types( )。
subgraph | other | …:并集。
subgraph & other & …:交集。
subgraph - other - …:减。
subgraph ^ other ^ …:返回一个新的子图,该子图包含的节点和关系只出现在subgraph中或者other中,通过关系相连的节点也包含在新子图中,不管这些节点是否存在于subgraph或者other中。
可以利用run 方法来直接执行Cypher语句,返回的是游标cursors,cursors必须通过遍历来展示结果。
match_str = "match p=shortestPath((m:m的label{name:'m的name属性值'})-[r*1..4]-(n:n的label{name:'name属性值'})) return p"
p1 = test_graph.run(match_str)
for p in p1:
print(p)
import csv
with open('F:/neo4j/data/MOOCCube/subject/计算机科学技术.csv', 'r', encoding='UTF-8') as f:
next(f) # 如果文件有头行,可以让文件从第二行开始读取
reader = csv.reader(f)
for item in reader:
# print("当前行数:", reader.line_num, "当前内容:", item)
a = Node("Course", name=item[1], ID=item[0], subject='计算机科学技术')
g.create(a)
在cmd命令行里输入neo4j.bat console,加载完后在浏览器中打开网页http://localhost:7474/即可使用。
1.neo4j–Cypher语法练习(WITH、 FOREACH、Aggregation、UNWIND、UNION、CALL):
链接: https://blog.csdn.net/qq_37503890/article/details/101565515?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2defaultCTRLISTdefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2defaultCTRLISTdefault-1.no_search_link
2.Py2neo部分对node、relations、subgraph的小用法:
链接: https://blog.csdn.net/claroja/article/details/84287093
3.官方文档中一些方法的运用:
链接: https://blog.csdn.net/Elenstone/article/details/106452464