Neo4j 如何在 python 中访问节点的属性

Gia*_*uca 3 python neo4j cypher

我可以像这样查询图形数据库

from neo4j import GraphDatabase

#establish connection
graphdp = GraphDatabase.driver(uri="bolt://localhost:7687", auth=("neo4j","Python"))

session = graphdp.session()

q1="MATCH (n {id:0}) return n"
nodes = session.run(q1)

for node in nodes:
    print(node)
Run Code Online (Sandbox Code Playgroud)

结果是:

<Record n=<Node id=5 labels={'Ubuntu1604'} properties={'host_image': 'qsrf-56fh-3db5-xd4t', 'id': 0}>>
<Record n=<Node id=6 labels={'Ubuntu1804'} properties={'host_image': 'qsrf-56fh-3dd4-44ty', 'id': 0}>>
<Record n=<Node id=7 labels={'Network'} properties={'start': '', 'capability': 'connection', 'cidr': '', 'end': '', 'nameservers': '[10.0.71.254, 8.8.4.4, 8.8.8.8]', 'id': 0}>>
<Record n=<Node id=8 labels={'Port'} properties={'port_ip': '', 'requirement': '["container","connection"]', 'id': 0}>>
<Record n=<Node id=13 labels={'GuestLinuxUser'} properties={'id': 0, 'playbook': 'createLinuxUser'}>>
<Record n=<Node id=16 labels={'GuestWindowsUser'} properties={'id': 0, 'playbook': 'createWindowsUser'}>>

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)

如何访问每个节点属性?

the*_*thv 8

您可以保存 BoltStatmentResult 对象数据,然后通过 Node.get() 方法访问节点属性:

q1="MATCH (n {id:0}) return n"
nodes = session.run(q1)
results = [record for record in nodes.data()]

# Now you can access the Node using the key 'n' (defined in the return statement):
res[0]['n'].get('host_image')
Run Code Online (Sandbox Code Playgroud)

我在 nodes.data() 迭代中将元素命名为“record”,因为如果您的 RETURN 返回了多个项目,则记录 != node。它是 RETURN 中项目的字典。

然后您可以访问节点数据类型的任何方法,这是文档参考

例如:

node = res[0]['n']
labels = list(node.labels)
Run Code Online (Sandbox Code Playgroud)