How do I import Python node dicts into neo4j?
Asked Answered
F

2

0

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
Fricassee answered 27/12, 2016 at 23:44 Comment(0)
E
0

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.

Entero answered 28/12, 2016 at 0:26 Comment(0)
T
0

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

Tuckerbag answered 28/12, 2016 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.