Neo4j的python操作库py2neo之二

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

1.Graph

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

Graph指的就是图数据库,需要传入连接的URI,初始化参数有bolt、secure、host、http_port、https_port、bolt_port、user、password。

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)需要传入的不定参数label、property_key、property_value、limit,返回符合筛选条件节点的生成器

from py2neo import Graph, Node

graph = Graph()

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

for node in nodes:

print(node)

find_one(*args, **kwargs)需要传入的不定参数label、property_key、property_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 )

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 )]

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

list(selected)

运行结果:

[(a03f6eb:Person ),

(e59993d:Person ),

(c44901e:Person ),

(b141775:Person ),

(e40244b:Person )]

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

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180323G07S2I00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

同媒体快讯

扫码关注云+社区

领取腾讯云代金券