如何从csv文件导入数据

时间:2017-05-15 12:21:22

标签: csv import neo4j

我是neo4j的新手,我试图从两个csv文件导入数据,但我无法做到。 这两个csv文件具有以下结构:

master.csv:

ID    NAME    LASTNAME     COLLEGE
01    aaa     bbb          ABC
02    xxx     yyy          BCE

award.csv:

playerID    award                  year
01          best_player            2010
01          rookie_of_the_year     2011
02          best_player            2012

如何为玩家和奖项创建节​​点以及如何连接它们?

我尝试使用此代码:(但显然它不起作用)

CREATE CONSTRAINT ON (a:PLAYER) ASSERT a.id IS UNIQUE;

CREATE CONSTRAINT ON (b:AWARD) ASSERT b.award IS UNIQUE;

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "File:///master.csv" as line fieldterminator ';'
CREATE (:PLAYER {id:line.playerID, name:line.firstName, lastName:line.lastName});

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "File:///award.csv" as line fieldterminator ';'
MATCH (player:Player {id:line.playerID})
MERGE (award:AWARD {award:line.award})
CREATE (player)-[r:won]->(award)
set r += {year:line.year}

如果我还要为大学添加节点,是不是?

CREATE CONSTRAINT ON (c:COLLEGE) ASSERT c.name IS UNIQUE;

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///master.csv" as line fieldterminator ';'
WITH line
MATCH (player:PLAYER {id:line.playerID})
MERGE (college:COLLEGE {name:line.college}})
CREATE (player)-[:studied]->(college)

由于

1 个答案:

答案 0 :(得分:0)

据我所知,主要是在你的陈述中使用正确的CASE /案例/案例。根据您显示的输入,我想出了这个(为我工作):

CREATE CONSTRAINT ON (a:PLAYER) ASSERT a.id IS UNIQUE;
CREATE CONSTRAINT ON (b:AWARD) ASSERT b.award IS UNIQUE;

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///master.csv" as line fieldterminator ';'
CREATE (:PLAYER {id:line.bioID, name:line.firstName, lastName:line.lastName});

USING PERIODIC COMMIT 
LOAD CSV WITH HEADERS FROM "file:///award_players.csv" as line fieldterminator ';'
WITH line
MATCH (player:PLAYER {id:line.playerID})
MERGE (award:AWARD {award:line.award})
CREATE (player)-[r:won]->(award)
SET r += {year:line.year};

希望这有帮助,汤姆