如何将Python节点dicts导入neo4j?

时间:2016-12-27 23:44:03

标签: python json neo4j py2neo

我在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

2 个答案:

答案 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