Neo4j Cypher - Creating relationship using two CSV files

六眼飞鱼酱① 提交于 2019-12-11 05:21:36

问题


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:


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);


来源:https://stackoverflow.com/questions/45755218/neo4j-cypher-creating-relationship-using-two-csv-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!