1
votes

I'm new to Neo4j and I'm currently working on to build a citation network.

I have two CSV files one containing the node properties and other containing relationship properties.

PAPERS.CSV -

paperId, title, year
123, abc, 1900
234, cde, 1902
456, efg, 1904

CITES.CSV -

fromId, ToId
123, 234
234, 456

My graph should look like (123)--cites-->(234)--cites-->(456). Using these files how do I create a relationship between nodes?

1

1 Answers

4
votes

You should avoid space in the header names and in the data too. If it is out of your control then you can use trim function and backticks to reference a headername. But normally, your csv should be clean. Your files should be in the import directory of the neo4j. Otherwise you should comment out dbms.directories.import=import property in neo4j.conf.

You can create nodes like this:

   LOAD CSV WITH HEADERS FROM "file:///PAPERS.CSV" as line
   CREATE (p:Paper {paperId:trim(line.paperId), title: trim(line.` title`), year: trim(line.` year`)});

And you can create relationships like this:

   LOAD CSV WITH HEADERS FROM "file:///CITES.CSV" as line
   MATCH (p1:Paper {paperId:trim(line.fromId)})
   MATCH (p2:Paper {paperId:trim(line.` ToId`)})
   CREATE (p1)-[:CITES]->(p2);