neo4j-Py2neo使用

neo4j-Py2neo(一):基本库介绍使用

py2neo的文档地址:https://neo4j-contrib.github.io/py2neo/

py2neo的本质是可以采用两种方式进行操作,一种是利用cypher语句,一种是使用库提供的DataTypes,Data类的实例需要和远程的数据库中一一对应。

说明
Core Graph API 直接可以直接从py2neo引用的几个核心类。负责具体执行提交的类。
Data Types neo4j的核心,节点关系等。也就是py2neo.data,这个类下面的所有成员都可以直接从py2neo引用。
Cypher cypher语言执行后的结果如何访问,在这个类中。
Bulk 批量执行cypher语句。
Object-Graph Mapping 将图谱映射到python类中,比如整个数据库,已知节点。
from py2neo import Graph
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password")) # 不指定name, 就用默认数据库
graph.run("UNWIND range(1, 3) AS n RETURN n, n * n as n_sq")
Node

1, 创建节点 ,Node(*labels, **properties)。节点就是两个部分组成:标签、属性。

from py2neo import Node # 或者 from py2neo.data import Node
a = Node('Person', name='alice')
b = Node(*['Person', 'Man', 'Manager'], **{'name':'Joey'}) 

# 查看标签
list(a.labels)

# 是否具有标签
a.has_label()

# 添加标签
a.add_label('Woman')

# 删除标签
a.remove_label('Person')

# 删除所有标签
a.clear_labels()

# 添加多个标签
a.update_labels(['Coder', 'Mother'])


# 查看属性
a['name']
a.get('name', default='John')
# 属性赋值
a['name'] = 'Pheebe'

# 如果没有属性,就赋予属性默认值。如果有保持原属性值。
a.setdefault('name', default='john')

# 添加属性,如果存在更新属性
a.update(age=29, love='football') # 或者 a.update(**{'age':29, 'love':'football'})

# 删除属性
del a['name']

# 属性数量
len(a)

# 字典形式返回属性,以及具有字典类似的属性
dict(a)
a.items()
a.keys()
a.values()

# 删除所有属性
a.clear()

# 查看对应的数据库
a.graph # 如果时None表示a还没有和远程数据库有链接。

# 查看节点对应数据库中的唯一身份
a.identity
RelationShip

创建关系:

class py2neo.data.Relationship(start_node, type, end_node, **properties)[source]

class py2neo.data.Relationship(start_node, end_node, **properties)

class py2neo.data.Relationship(node, type, **properties)

class py2neo.data.Relationship(node, **properties)

from py2neo import Relationship, Node
# 创建三种方式
start = Node('Man', name='Ross')
end = Node('Woman', name='Richiel')

class Love(Relationship): pass # 先指定是啥关系
rl_1 = Love(start, end)

love = Relationship.type('Love') # 先制定是啥关系
rl_2 = love(start, end, )

# 一般常用的就是这个。
rl_3 = Relationship(start, 'Love', end,  **{'retain'='two season'}) # 如果不指定type,类型就是''

# 查看关系的名称
rl_1.type.__name__

# 查看关系开始结
rl_1.nodes
rl_1.start_node
rl_1.end_node

# 关系的属性,和节点一样
dict(rl_1)
rl_1.keys()
rl_1.values()
rl_1.items()
# 添加、更新
rl_1.update(**{'name':'love'})
# 删除
del rl_1['name']

# 以及其他的get, setdefault
rl_1.get('name', default=None)

rl_1.setdefault('name', default='john')
Path

Path特点是相邻必须连接。本质上就是相邻节点必须连接的几段关系。

from py2neo import Path, Relationship

a = Node('Person', name='a')
b = Node('Person', name='b')
c = Node('Person', name='c')
d = Node('Person', name='d')
e = Node('Person', name='e')


# 连接节点与节点
ab = Path(a, 'love', b)
de = Path(d, 'love', e)

# 连接节点与节点与节点。。。
abc = Path(a, 'love', b, Relationship(b, 'love', c), c)

# 相邻必须连接
abc = Path(a, 'love', b, Relationship(a, 'love', c), c) # 报错ValueError: Cannot append walkable love(Node('Person', name='a'), Node('Person', name='c')) to node Node('Person', name='b')

# 连接节点与关系
ab = Relationship(a, 'love', b)
abc = Path(ab, 'love', c)

# 连接关系与关系
ca = Relationship(c, 'love', a)
abca = abcd = Path(ab, 'love', ca)

# 连接节点与Path
cde = Path(c, 'love', de)

# 连接Path与Path
abcde = Path(abc, 'love', de)

# 查看开始结束的节点
abcde.start_node
abcde.end_node

# 按照顺序查看所有节点
abcde.nodes

# 按照关系查看所有关系
abcde.relationships
Subgraph

就是图。通过逻辑的方式进行创建 | & - ^

# 节点、关系、Path、subgraph之间通过逻辑进行连接就是subgraph
sub1 = abc | abca 
sub2 = abc & abca  # 节点与关系都必须在两边都存在
sub3 = abc - abca # 节点关系在前者存在,在后者不存在
sub4 = abc ^ abca # 各自独特的部分,sub1|sub2 - sub1&sub2

# 查看所有节点,关系
sub4.nodes
sub4.relationships

# 查看图中所有关系的type
sub4.types()

# 查看图中所有节点的属性key以及标签的集合
sub4.keys()
sub4.labels()

Data Types就完事了,剩下就是增删改查事务提交等数据库通用性的东西。数据库的操作都是以事务为单位,所以先从事务执行返回的结果record看起。

class* py2neo.cypher.Record

执行语句后返回的结果是cursor,是一堆结果。record是其中一个结果,本质上就是一个字典。

cursor = graph.run('match (n) return n')
if cursor.forward():
    record = cursor.current
    
# 转换为字典
dict(record) # {'n': Node('Symtom', name='脱水')}
record.data() # {'n': Node('Symtom', name='脱水')}

# 所有键
record.keys() # ['n']

# 所有值
record.values() # [Node('Symtom', name='脱水')]

# 将record里的所有节点,关系形成一个子图
record.to_subgraph().nodes # (Node('Symtom', name='脱水'),)
py2neo.cypher.Cursor(result, hydrant=None, sample_size=3)

cursor是执行语句返回的是cursor,通过cursor来查询一个个record。

cursor = graph.run('match (n) return n')

# 基本使用就是,结合forward()以及current使用
while cursor.forward():
    record = cursor.current
    
# 一次性返回所有结果
record_list = cursor.data()

# 转换为dataframe
cursor.to_data_frame()

# 转换为ndarray	
cursor.to_ndarray()

# 转换为子图
cursor.to_subgraph()
Transaction

py2neo的用法就是分成两种,一种是使用cypher语句,一种是使用自己的数据类,这些类是要和远程的实际的节点一一对应。

cypher语句的使用遵循的规则是,利用$标志语句中的变量,例如match (n) where n.name=$name。输入变量值有两种方式,一种是name=‘biden’, 一种是**{‘name’:‘biden’}

# 建立一个事务
tx = Graph.auto() # 自动提交的事务,操作之后默认执行graph.commit(tx)
tx = Graph.begin() # 非自动提交的事务

# 结束一个事务
Graph.commit(tx) # 提交
Graph.rollback(tx) # 回滚

# 属性
tx.graph # Graph('bolt://localhost:7687', name='neo4j')
tx.readonly # False表示可以写入。

# 使用cypher语句,只用一个run就可以,evaluate就是只返回第一个结果,udpate是不用返回结果
record = tx.evaluate('match (n) return n') #  返回一个数据类型,节点,关系或者子图。

cursor = tx.run('match (n) return n') # 返回多个结果,是一个cursor

tx.update("match (n:boss) where n.name='biden' set n.name=$newname", **{'newname':'aoguanhai'})
tx.update("match (n:boss) where n.name='biden' set n.name=$newname", newname='auguanhai')


# create(subgraph) 创建一个本地,create函数会在远程创建一个同样的节点/关系/子图,并和本地对应。
a = Node('Person', **{'name'})
a.graph # None,说明
tx.create(a)
a.graph # Graph('bolt://localhost:7687', name='neo4j') ,说明和远程一一对应了。

# delete(subgraph) 删除远程对应的节点关系子图
tx.delete(a)
a.graph # None,说明远程已经删除。

# exists(subgraph) 检测是否有远程对应
tx.exists(a) # False 已经无对应

# merge(subgraph, primary_label=None, primary_key=None) 

# pull 更新本地,针对已经连接的
tx.pull(a)

# 更新远程,针对已经连接的
a['name'] = 'bob'
tx.push(a)

# separate(subgraph),删除远程中的子图中的所有关系,本地不受影响
tx = graph.begin()
a = Node('Person', name='Alice')
b = Node('Person', name='bob')
ab = Relationship(a, 'love', b)
sub =  a | b | ab
tx.create(sub) # 创建
tx.separate(sub) # 删除其中的关系
graph.commit(tx)
GraphServer
from py2neo import GraphService, Graph

url = "bolt://localhost"
auth = ('neo4j', '123456')

gs = GraphService(url, auth=auth)

# 查看所有数据库名称
list(gs)  # ['neo4j', 'system']
gs.keys() # ['neo4j', 'system']

# 建立一个Graph
graph = gs['neo4j']
graph = gs.default_graph 
graph = gs.system_graph

# 查看gs的属性值,和字典使用方法一致。
gs.config.items()
gs.config.keys()
gs.config.values()

# 查看连接地址信息
gs.connector
gs.profile
gs.uri

# 查看neo4j版本
gs.kernel_version # 
gs.product # 'Neo4j Kernel 5.14.0 (Community)'

Graph
# 建立一个Graph,也从GraphServer建立
graph = Graph(uri=url, auth=auth, name='neo4j') 

# 创建事务,在事务已经介绍过了
tx_auto = graph.auto()
tx = graph.begin()

# 提交事务
graph.commit(tx)

# create(subgraph),创建一个自动提交事务进行create,注意tx_auto并不支持create。
from py2neo import *

a = Node('person', name='ali')
b = Node('person', name='bli')
c = Node('person', name='cli')

ac = Relationship(a, 'fr', c)
bc = Relationship(b, 'fr', c)

sub = ac|bc
graph.create(sub)

# delete(subgraph),也是自动提交
graph.delete(a)

# delete_all(),删除全部
graph.delete_all()

# evaluate(cypher, parameters=None, **kwparameters),执行cypher语句,按照cypher语句规则进行就可以。
record = graph.evaluate('match (n) return n') # 返回一个record

# exists(subgraph) 查看是否存在
graph.exists(sub)

# match(nodes=None, r_type=None, limit=None),用来匹配关系。nodes是(start_node, end_node),r_type是关系类型,如果nodes=(Node, c),表示所有end_node是c的关系。
list(graph.match((None, c) ,r_type='fr')) # 查找所有以c为朋友的人
'''
[fr(Node('person', name='ali'), Node('person', name='cli')),
 fr(Node('person', name='bli'), Node('person', name='cli'))]
'''

# match_one(nodes, r_type), 值匹配一个关系。
graph.match_one((None, c) ,r_type='fr') # fr(Node('person', name='ali'), Node('person', name='cli'))


# nodes, relationships,获取所有节点关系到本地,可以使用match进行匹配。
list(graph.nodes.match("person", **{'name':'cli'})) # [Node('person', name='cli')]

list(graph.relationships.match((a, c), 'fr')) # [fr(Node('person', name='ali'), Node('person', name='cli'))]


# pull(subgraph),将已经连接的子图从远程更新
graph.pull(sub)

# push(subgraph),从本地已经连接到的子图更新到远程
graph.push(sub)

# separate(subgraph) ,从已经连接的子图删除关系
graph.separate(sub)

# cypher操作,run,update(无返回结果), query(只能查),使用方法就是使用cypher的规则
graph.run('match (n) where n.name=$name return n', name='cli')
graph.query('match (n) where n.name=$name return n', name='cli')
graph.update('match (n) where n.name=$name set n.name=$newname', name='cli', newname='ccli')



你可能感兴趣的:(neo4j,数据库)