问题
I produce the following node and relationship data in a for loop about 1 million times. The idea is that investor
nodes connect to company
nodes by relationship
edges:
investor = {'name': owner['name'],
'CIK': owner['CIK']}
relationship = {'isDirector': owner['isDirector'],
'isOfficer': owner['isOfficer'],
'isOther': owner['isOther'],
'isTenPercentOwner': owner['isTenPercentOwner'],
'title': owner['title']}
company = {'Name': json['issuerName'],
'SIC': json['issuerSIC'],
'Ticker Symbol': json['issuerTradingSymbol'],
'CIK': json['issuerCIK'],
'EIN': json['issuerEIN']}
How do I complete the following code to get the dicts above into neo4j community edition?
from py2neo import Graph, authenticate
authenticate("localhost:7474", "neo4j", "neo")
graph = Graph()
for json in long_list_of_dicts:
investor = {...}
company = {...}
relationship = {...}
# Code to import investor, company, relationship data into neo4j
回答1:
In py2neo a Node is defined in following manner:
class Node(*labels, **properties)
Each node
has a label
and can have many properties
. In this case, Investor node can de defined by setting the label investor and properties of the node to be name and CIK.
investor_node = Node('investor', name = owner['name'], CIK = owner['CIK'])
Similarly, company node would look like:
company_node = Node('company', name = json['issuerName'], SIC = json['issuerSIC'])
Relationship are defined in following manner :
class Relationship(start_node, type, end_node, **properties)
In this case Relationship can be defined using:
investor_company_relationship = Relationship(investor_node, "is_director", company_node)
You can find one sample implementation of neo4j graph here.
回答2:
You could use UNWIND
clause. Something like
WITH {json} AS document
UNWIND document AS company
MERGE (c:company {c_id:company.id})
SET c.sic=company.issuerSIC
If some of your json items is list again, you can use UNWIND as much as you like: UNWIND document.list_of_some_property
来源:https://stackoverflow.com/questions/41353578/how-do-i-import-python-node-dicts-into-neo4j