【问题标题】:Neo4j How to access node's properties in pythonNeo4j 如何在 python 中访问节点的属性
【发布时间】:2020-03-04 10:27:13
【问题描述】:

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

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

如何访问每个节点属性?

【问题讨论】:

    标签: python neo4j cypher


    【解决方案1】:

    您可以将 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')
    

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

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

    例如:

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

    【讨论】:

    • 感谢您的回复,但我仍然有同样的问题。如果我打印(results[0]),我会得到&lt;Record n=&lt;Node id=5 labels={'Ubuntu1604'} properties={'host_image': 'qsrf-56fh-3db5-xd4t', 'id': 0}&gt;&gt;。如何访问例如“host_image”值?
    • @cristina,对不起,我已经更新了我的答案以显示如何返回节点并访问属性
    • 您知道如何检索标签或节点 ID 等属性吗? .get() 方法从 properties={ } 检索,但我如何访问标签={} 或节点 ID?
    • 已更新。此外,它会回溯一段时间,但不建议使用内部 ID,因为它们可以重复使用。我建议您将自己的 GUID 属性分配为要使用的 ID。
    • 标签可以通过node.labelsneo4j.com/docs/api/python-driver/1.7/types/…访问
    【解决方案2】:

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

    def get_properties(self):
        return self._properties
    

    然后你可以通过 instance_of_your_node.get_properties() 访问属性

    【讨论】:

      【解决方案3】:

      正如我在 neo4j 包中看到的那样,Node 类继承自 Entity 类,后者具有 _properties 属性。问题是无法从类Node 范围之外访问此属性。

      要解决这个问题,你可以自己定义 getter :

      def get_properties(self):
          return self._properties
      

      并将此方法绑定到您的 Node 实例:

      node.get_properties = types.MethodType(get_properties, node)
      node.get_properties()
      

      这样,您不必更改库中的任何内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多