[Neo4j系列五]Neo4j的python操作库py2neo之二

neo4j 3月前 ⋅ 434 阅读

   上一篇文章主要介绍python中适配Neo4j的库py2neo的关于节点、关系、子图基本内容,本文继续介绍py2neoGraph、事务、嵌入Cypher语句以及NodeSelector等。图片

1.Graph

  • class py2neo.database.Graph(*uris, **settings)

Graph指的就是图数据库,需要传入连接的 URI,初始化参数有boltsecurehosthttp_porthttps_portbolt_portuserpassword

图片

 

from py2neo import Graph

graph_1 = Graph()

graph_2 = Graph(host="localhost")

graph_3 = Graph("http://localhost:7474/db/data/")

 

  • begin(autocommit=False) 开始一个事务

  • create(subgraph) 事务操作创建一个子图

  • exists(subgraph) 是否存在节点、关系或子图

from py2neo import Graph, Node, Relationship

g = Graph()

tx = g.begin()

a = Node("Person", name="Alice")

tx.create(a)

b = Node("Person", name="Bob")

ab = Relationship(a, "KNOWS", b)

tx.create(ab)

tx.commit()

print(g.exists(ab))

返回结果:True

 

  • data(statement, parameters=None, **kwparameters)

利用data()获得查询结果,返回含有字典元素的列表

from py2neo import Graph

graph = Graph()

data=graph.data("MATCH (a:Person) RETURN a.name, a.born LIMIT 4")

print(data)

运行结果:

[{'a.born': 1964, 'a.name': 'Keanu Reeves'},

 {'a.born': 1967, 'a.name': 'Carrie-Anne Moss'},

 {'a.born': 1961, 'a.name': 'Laurence Fishburne'},

 {'a.born': 1960, 'a.name': 'Hugo Weaving'}]

还可以通过pandas.DataFrame使结果转化为表格的形式

from py2neo import Graph

from pandas import DataFrame

graph = Graph()

data=DataFrame(graph.data("MATCH (a:Person) RETURN a.name, a.born LIMIT 4"))

print(data)

运行结果: 

  a.born              a.name

0    1964        Keanu Reeves

1    1967    Carrie-Anne Moss

2    1961  Laurence Fishburne

3    1960        Hugo Weaving

 

  • degree(subgraph) 返回度数(度就是指一个节点相关联边的数目)

from py2neo import Graph

graph = Graph()

node=graph.find_one(label='Resource',property_key='name',property_value='β-羟丁酸')

data=graph.degree(node)

print(data)

运行结果:

2

 

  • delete(subgraph) 删除节点、关系或子图

  • delete_all() 删除数据库所有的节点和关系

  • evaluate(statement, parameters=None, **kwparameters) 返回第一条查询结果

from py2neo import Graph

graph = Graph()

data=graph.evaluate("MATCH (a:Person) RETURN a.name")

print(data)

运行结果:

Keanu Reeves

 

  • find(*args, **kwargs) 需要传入的不定参数labelproperty_keyproperty_valuelimit,返回符合筛选条件节点的生成器

 

from py2neo import Graph, Node

graph = Graph()

nodes=graph.find(label='Resource')

for node in nodes:

    print(node)

 

  • find_one(*args, **kwargs)需要传入的不定参数labelproperty_keyproperty_value,返回符合筛选条件一个节点,即使多个都满足,也会返回唯一节点。

  • match(start_node=None, rel_type=None, end_node=None, bidirectional=False, limit=None) 匹配并返回所有满足条件的关系

for rel in graph.match(start_node=alice, rel_type="FRIEND"):

    print(rel.end_node()["name"])

 

  • match_one(start_node=None, rel_type=None, end_node=None, bidirectional=False)匹配并返回所有满足条件的一条关系

  • push(subgraph) 更新节点、关系或子图

from py2neo import Graph, Node

graph = Graph()

a = Node('Person', name='xiaoming')

node = graph.find_one(label='Person')

node['age'] = 18

graph.push(node)

print(graph.find_one(label='Person'))

运行结果:

(a80d634:Person {age:18,name:"xiaoming"})

 

  • run(statement, parameters=None, **kwparameters)运行一个cypher查询,返回cursors

from py2neo import Graph, Node

graph = Graph()

data=graph.run('MATCH (n:Person) RETURN n.name')

print(list(data))

运行结果:

[('n.name': 'xiaoming'),('n.name': 'xiaohong'),('n.name': 'xiaoliang')]

 

2.NodeSelector

NodeSelector是为更好的查询节点,支持更多的查询条件,比graph更友好

  • select(*labels, **properties)返回NodeSelection

from py2neo import Graph, NodeSelector

graph = Graph()

selector = NodeSelector(graph)

selected = selector.select("Person", name="Keanu Reeves")

list(selected)

运行结果:

[(f9726ea:Person {born:1964,name:"Keanu Reeves"})]

 

NodeSelection还有以下方法

  • first()返回单个节点

  • limit(amount)返回底部节点的限值条数

  • skip(amount)返回顶部节点的限值条数

  • order_by(*fields)排序

selection.order_by("_.name", "max(_.a, _.b)")

  • where(*conditions, **properties)筛选条件

 

from py2neo import Graph, Node

graph = Graph()

selected = selector.select("Person").where("_.name =~ 'J.*'", "1960 <= _.born < 1970")

list(selected)

运行结果:

[(a03f6eb:Person {born:1967,name:"James Marshall"}),

 (e59993d:Person {born:1966,name:"John Cusack"}),

 (c44901e:Person {born:1960,name:"John Goodman"}),

 (b141775:Person {born:1965,name:"John C. Reilly"}),

 (e40244b:Person {born:1967,name:"Julia Roberts"})]

 

参考http://py2neo.org/v3/index.html

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: