Reputation: 401
I have written 3 queries in order to create the nodes :Comments an their properties as well as the relationships with the nodes :Users and :Posts:
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "http://neuromancer.inf.um.es:8080/es.stackoverflow/Comments.csv" AS row
CREATE(n)
SET n=row
WITH n AS node
CREATE (c:Comment {Id: node.Id, CreationDate: node.CreationDate,
Score: node.Score, Text: node.Text,
UserId: node.UserId, PostId: node.PostId});
MATCH (c:Comment), (u:User)
WHERE toInt(c.UserId) = toInt(u.Id)
CREATE (u)-[:AUTHOR_OF]->(c)
REMOVE c.UserId;
MATCH (c:Comment), (p:Post)
WHERE toInt(c.PostId) = toInt(p.Id)
CREATE (c)-[:INCLUDED_IN]->(p)
REMOVE c.PostId;
The first query creates the nodes, while the second and third create the relationships between :Comment and :User and :Comment and :Post. However I want to create one single query that creates all of this, in order to make it more efficient. Is this posible and how can I make it? I could not find a way.
Upvotes: 0
Views: 16
Reputation: 19373
First, there's no need to create a temporary node n
or temporary id properties.
Second, it might be more efficient, depending on the size of your file, to do two passes over the file, one to create comments, and the second to match nodes and create relationships. Best to try/profile and see.
Here's what it would look like with just one pass:
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "http://neuromancer.inf.um.es:8080/es.stackoverflow/Comments.csv" AS row
CREATE (c:Comment {Id: row.Id, CreationDate: row.CreationDate,Score: row.Score, Text: row.Text})
WITH c
MATCH (u:User) WHERE u.Id = toInt(row.UserId)
CREATE (u)-[:AUTHOR_OF]->(c)
WITH c
MATCH (p:Post) WHERE p.Id=toInt(c.PostId)
CREATE (c)-[:INCLUDED_IN]->(p)
With two passes,
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "http://neuromancer.inf.um.es:8080/es.stackoverflow/Comments.csv" AS row
CREATE (c:Comment {Id: row.Id, CreationDate: row.CreationDate,Score: row.Score, Text: row.Text})
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM "http://neuromancer.inf.um.es:8080/es.stackoverflow/Comments.csv" AS row
MATCH (c:Comment {Id: row.Id})
MATCH (u:User) WHERE u.Id = toInt(row.UserId)
MATCH (p:Post) WHERE p.Id=toInt(c.PostId)
CREATE (u)-[:AUTHOR_OF]->(c)
CREATE (c)-[:INCLUDED_IN]->(p)
Upvotes: 1