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

2024-09-28 05:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

结果是:

<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

如何访问每个节点属性


Tags: imageidnodehostlabelssessionpropertiesconnection
2条回答

属性“properties”在类“Node”中是私有的(该类在“neo4j/graph/init.py”中) 我在类“Node”中添加了以下方法:

def get_properties(self):
    return self._properties

然后您可以通过您的节点的实例访问属性

您可以保存BoltStationResult对象数据,然后通过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')

我在nodes.data()迭代中将元素命名为“record”,因为如果返回的项目不止一个,那么record!=节点。这是一本关于报税表上项目的词典

然后可以访问节点数据类型的任何方法,下面是docs reference

例如:

node = res[0]['n']
labels = list(node.labels)

相关问题 更多 >

    热门问题