Search code examples
csvneo4jneo4j-apoctriples

Loading a entity relation triple csv as nodes


Suppose I have a csv file with data in the format (Subject, relation, Object).

Is it possible to load this into neo4j as a graph modeled such that the subject and object become nodes and the relation between them is the relation from the triple? Essentially while loading from the csv, I want to load the subject and object as individual nodes and the relation is the one joining them.

(subject)-[:relation]->(object)

My csv is in the format ent1,state,ent2 a,is,b . . .


Solution

  • Yes, It's possible. You need to install the APOC plugin in Neo4j and then use apoc.merge.relationship.

    Refer the following query to load the data: Add/Modify required details in the query.

    LOAD CSV FROM "file:///path-to-file" AS line
    MERGE (sub:Subject {name:line[0]})
    MERGE (obj:Object {name:line[2]})
    WITH sub, obj, line
    CALL apoc.merge.relationship(sub,line[1],{},{},obj) YIELD rel
    RETURN COUNT(*);