我在for循环中生成以下节点和关系数据大约100万次。我们的想法是investor
个节点通过company
边连接到relationship
个节点:
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']}
如何完成以下代码以将上述内容纳入neo4j社区版?
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
答案 0 :(得分:0)
在py2neo中,节点按以下方式定义:
class Node(*labels, **properties)
每个node
都有label
,可以有多个properties
。在这种情况下,可以通过将标签投资者和节点的属性设置为 name 和 CIK 来定义Investor节点。
investor_node = Node('investor', name = owner['name'], CIK = owner['CIK'])
同样,公司节点看起来像:
company_node = Node('company', name = json['issuerName'], SIC = json['issuerSIC'])
关系定义如下:
class Relationship(start_node, type, end_node, **properties)
在这种情况下,关系可以使用以下方式定义:
investor_company_relationship = Relationship(investor_node, "is_director", company_node)
您可以找到neo4j图here的一个示例实现。
答案 1 :(得分:0)
您可以使用UNWIND
子句。像
WITH {json} AS document
UNWIND document AS company
MERGE (c:company {c_id:company.id})
SET c.sic=company.issuerSIC
如果您的某些json项目再次列出,您可以根据需要使用UNWIND:UNWIND document.list_of_some_property