Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

python - py2neo doesn't update database nodes

I am having trouble updating nodes in a neo4j-database via py2neo. I am trying to extract a network graph from several text files. My script extracts the necessary information and creates the nodes and relationships and then writes them to the database. While this works fine for some nodes, I have repeatedly run into nodes in the database that don't have all properties that should be there.

When I run the script line by line for such a node, the information is extracted fine and the objects created in python, but the information is lost at some point during the transfer to neo4j. I execute .push() or tx.commit() which runs without error, but when I query the node in neo4j explorer, the information isn't there. The example below is for a node, but the same applies to relationships and their properties.

I am using

  • Windows 10
  • Anaconda, version 1.10
  • Python, version 3.7.9
  • Neo4j-Desktop, version: 1.3.11
  • Neo4j-Database, version: 3.5.25
  • py2neo, version (log: 4.2.0 | pip: 2020.1.1)
from py2neo.matching import *

graphdb=Graph(user="user",password="password")

def g_commit(*nds):
    graphdb=Graph(user="neo4j",password="aqnetwork2021")
    tx=graphdb.begin()
    c=0
    for n in nds:
        if "py2neo.data." in str(type(n)):
            tx.create(n)
            c=c+1
    if c>0:
        try:
            fdb=tx.commit()
        except Exception as e:
            if hasattr(e, 'message'):
                fdb=e.message
            else:
                fdb=e
    else:
        fdb="No Node or Relationship Objects found"
    return fdb

def checkNode(label, ident):
    check=graphdb.nodes.match(label, id=ident).first()
    if check == None:
        chckNode=Node(label,id=ident)
        chckNode.__primarylabel__=label
        chckNode.__primarykey__="id"
        g_commit(chckNode)
    else:
        chckNode=check
        chckNode.__primarylabel__=label
        chckNode.__primarykey__="id"
    return chckNode


pr_node=checkNode("pressrelease",id)
pr_node["date"]= date #string
pr_node["heading"]=heading #string
pr_node["acion"]=action #string
g_commit(pr_node)

If I check the object in Python I get

dict(pr_node)
 
{'id': 'AB/12345',
 'date': '02.10.2015',
 'heading': 'Something important',
 'acion': 'adds'}

The log created via the logging module shows the following which I interpret as successfull

2021-01-12 21:10:02,474 - Adding connection pool for profile ConnectionProfile('bolt://neo4j@localhost:7687')
2021-01-12 21:10:02,474 - Trying to acquiring connection from pool <ConnectionPool profile=ConnectionProfile('bolt://neo4j@localhost:7687') in_use=0 free=0 spare=100>
2021-01-12 21:10:02,474 - [#0000] C: (Dialing <localhost:7687>)
2021-01-12 21:10:02,476 - [#F234] S: (Accepted)
2021-01-12 21:10:02,476 - [#F234] C: <BOLT>
2021-01-12 21:10:02,476 - [#F234] C: <PROTOCOL> 4.1 | 4.0 | 3.0 | 2.0
2021-01-12 21:10:02,476 - [#F234] S: <PROTOCOL> 3.0
2021-01-12 21:10:02,476 - [#F234] C: HELLO {'user_agent': 'py2neo/4.2.0 Python/3.7.9-final-0 (win32)', 'scheme': 'basic', 'principal': 'neo4j', 'credentials': '*******'}
2021-01-12 21:10:02,476 - [#F234] C: (Sent 116 bytes)
2021-01-12 21:10:02,477 - [#F234] S: SUCCESS {'server': 'Neo4j/3.5.25', 'connection_id': 'bolt-83'}
2021-01-12 21:10:02,477 - Acquired connection <py2neo.client.bolt.Bolt3 object at 0x0000017DBFC93D88>
2021-01-12 21:10:02,477 - Releasing connection <py2neo.client.bolt.Bolt3 object at 0x0000017DBFC93D88>
2021-01-12 21:10:02,477 - Attempting to acquire connection to default database
2021-01-12 21:10:02,477 - Using connection pool <ConnectionPool profile=ConnectionProfile('bolt://neo4j@localhost:7687') in_use=0 free=1 spare=99>
2021-01-12 21:10:02,477 - Trying to acquiring connection from pool <ConnectionPool profile=ConnectionProfile('bolt://neo4j@localhost:7687') in_use=0 free=1 spare=99>
2021-01-12 21:10:02,477 - Acquired connection <py2neo.client.bolt.Bolt3 object at 0x0000017DBFC93D88>
2021-01-12 21:10:02,477 - [#F234] C: BEGIN {}
2021-01-12 21:10:02,478 - [#F234] C: (Sent 7 bytes)
2021-01-12 21:10:02,478 - [#F234] S: SUCCESS {}
2021-01-12 21:10:02,478 - [#F234] C: COMMIT
2021-01-12 21:10:02,478 - [#F234] C: (Sent 6 bytes)
2021-01-12 21:10:02,479 - [#F234] S: SUCCESS {'bookmark': 'neo4j:bookmark:v1:tx49427'}
2021-01-12 21:10:02,479 - Releasing connection <py2neo.client.bolt.Bolt3 object at 0x0000017DBFC93D88>

I check the object in neo4j however I get

MATCH(n:pressrelease) WHERE (n.id="AB/12345") RETURN n

{
  "identity": 9770,
  "labels": [
    "pressrelease"
  ],
  "properties": {
"id": "SC/12067"
  }
}

Sometimes, when I use graphdb.push() instead this works, but not reliably.

Am I missing something in my code? I am somewhat lost as to where to look for the error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...