Reputation: 195
I'm able to make a query to a graph database like this
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)
The result is:
<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
How can I access each node property?
Upvotes: 9
Views: 8160
Reputation: 857
As far as I know, there is no direct way to convert Node object into a dictionary object. So you need to do it yourself. Here is a simple way:
def node_to_dict(neo4j_node):
'''
Convert a neo4j node object into dict
:param neo4j_node:
:return: node properties in a dict
'''
props = {}
for key, value in neo4j_node.items():
props[key] = value
return props
Upvotes: 1
Reputation: 1
To access the properties of a node, we need to access the "neo4j._data.Record" object that we get after running the query.
qry = "MATCH (n) RETURN n LIMIT 2"
result = session.run(qry)
for record in result:
data = record['n']['node_name']
In the above code snippet, "record" is the "neo4j._data.Record" object, and to access a property, you just need to pass the property name in place of 'node_name.
Upvotes: 0
Reputation: 21
As I can see in the neo4j package, the class Node
inherit from the class Entity
, which possess a _properties
attribute. The problem is that this attribute is not accessible from outside the class Node
scope.
To solve this problem, you can define yourself the getter :
def get_properties(self):
return self._properties
and bind this method to your Node
instance :
node.get_properties = types.MethodType(get_properties, node)
node.get_properties()
This way, you don't have to change anything in the lib.
Upvotes: 2
Reputation: 9
The attribute "properties" is private in the class "Node" (The class is inside "neo4j/graph/init.py") I have added the following method in the class "Node":
def get_properties(self):
return self._properties
Then you can access the properties by instance_of_your_node.get_properties()
Upvotes: -1
Reputation: 591
You can save BoltStatmentResult object data out and then access the node properties via the Node.get() method:
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')
I named the element 'record' in nodes.data() iteration, because if your RETURN had more than one item returned, then record != node. It's a dictionary of items in the RETURN.
You can then access any of the methods of the Node Data Type, here's the docs reference
E.g:
node = res[0]['n']
labels = list(node.labels)
Upvotes: 11