Loading

neo4j desktop的使用(python连接并创建一个小示例)

一、neo4j的连接方式

 内部导入数据可以使用HTTP服务(端口7474),外部访问使用浏览器(端口7687)

 

二、neo4j基本的cypher语句

match 匹配图模式。这是从图中获取数据最常见的方法

where 不是独立的语句,而是match、optinal match和with的一部分,用于给模式添加约束或者过滤传递给with的中间结果

return 定义返回的结果

create(和 delete) 创建(和删除)节点、关系

set(和remove) 使用set设置属性值和给节点添加标签,使用remove移除它们

merge 匹配已经存在的或创建新节点和模式,这对于有唯一性约束的时候非常有用

 

三、使用py2neo创建一个示例图

from py2neo import Node, Graph, Relationship, NodeMatcher


# 创建连接
graph = Graph('http://127.0.0.1:7474', name="test", password="23615")

# 创建节点
node3 = Node('animal', name='cat')
node4 = Node('animal', name='dog')
node2 = Node('Person', name='Alice')
node1 = Node('Person', name='Bob')

# 创建关系
r1 = Relationship(node2, 'know', node1)
r2 = Relationship(node1, 'know', node3)
r3 = Relationship(node2, 'has', node3)
r4 = Relationship(node4, 'has', node2)

# 将节点和关系在图中创建
graph.create(node1)
graph.create(node2)
graph.create(node3)
graph.create(node4)
graph.create(r1)
graph.create(r2)
graph.create(r3)
graph.create(r4)

# 查询某节点,一般在脚本中很少出现
matcher = NodeMatcher(graph)
print(list(matcher.match('animal')))

 

参考:

《neo4j权威指南》

posted @ 2022-03-24 14:35  国家三级保护废物  阅读(1675)  评论(0编辑  收藏  举报