Search code examples
csvneo4jgraph-databasesneo4jclient

How to use a value in CSV file as relationship lable in neo4j?


I am trying to create a graph in neo4j and my data which is in CSV file looks like

node1,connection,node2
PPARA,0.5,PPARGC1A
PPARA,0.5,ENSG00000236349
PPARA,0.5,TSPA

I want connection values to use as labels of relationships in graph which I am not able to do. Following is the exact code I am using to create graph.

LOAD CSV WITH HEADERS FROM "file:///C:/Users/username/Desktop/Cytoscape-friend.csv" AS network
CREATE (:GeneRN2{sourceNode:network.node1, destNode:network.node2})
CREATE (sourceNode) -[:TO {exp:network.connection}] ->(destNode) 

My second question is that as there are multiple repeating values in my file, by default neo4j is creating multiple nodes for repeating values. How do I create single node for multiple values and connect all other relating nodes to that single node?


Solution

    1. Relationships do not have labels. They have a type.
    2. If you need to specify the type of relationship from a variable, then you need to use the procedure apoc.create.relationship from the APOC library.
    3. To avoid creating duplicate nodes, use MERGE instead of CREATE.

    So your query might look like this:

    LOAD CSV WITH HEADERS 
             FROM "file:///C:/Users/username/Desktop/Cytoscape-friend.csv" 
             AS network
    MERGE (sourceNode {id:network.node1})
    MERGE (destNode {id:network.node2})
    WITH sourceNode, 
         destNode, 
         network
    CALL apoc.create.relationship(sourceNode, network.connection, {}, destNode) yield rel
    RETURN sourceNode, 
           rel, 
           destNode