0

I am very new into Neo4j, and have been trying to create nodes and relations from this Pandas dataframe for a while

source target    relation
John  California  lives in
Peter Utah.       born in
Oscar Canada.     returns to

I am trying to connect source and target nodes according to their relation using Python, but so far no luck. I tried the following approach:

 MATCH (s:source {property:$source})
          MERGE (s)-[r:R_TYPE]->(t:target {property:$target})'''

but no luck. Would be grateful for some guidance regarding this. This is the full code:


def add_nodes_and_relations(rows, batch_size=10000):
    # Adds author nodes to the Neo4j graph as a batch job.
    query = '''
            UNWIND $rows AS row
            MERGE (:target {target: row.target})
            MATCH (s:source {property:$source})
            MERGE (s)-[r:R_TYPE]->(t:target {property:$target})

            '''
    return insert_data(add_nodes_and_relations, rows, batch_size)


def insert_data(query, rows, batch_size = 10000):
    # Function to handle the updating the Neo4j database in batch mode.
    
    total = 0
    batch = 0
    result = None
    
    while batch * batch_size < len(rows):

        res = conn.query(query, 
                         parameters = {'rows': rows[batch*batch_size:(batch+1)*batch_size].to_dict('records')})
        total += res[0]['total']
        batch += 1
        result = {"batches":batch}
            
                 
        print(result)
        
    return result
user840
  • 134
  • 1
  • 13

1 Answers1

0

Your statement is incorrect, use this instead. You don't have a $target parameter, your parameter name is $rows

// turn $rows list into a row variable for cypher to consume
UNWIND $rows AS row
// for each row {source, target}
// create source node
MERGE (s:source {property:row.source})
// create target node
MERGE (t:target {target: row.target})
// create relationship
MERGE (s)-[r:R_TYPE]->(t)
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80