Search code examples
csvneo4jrelationships

Neo4j import for Nodes and Relationships


Hi I have been attempting to set up a graph database which will be something like a social network my question is can I create nodes from a csv file called Messages.csv then create the relationships to the Users using Messages.csv?

Here is my first import Statement to create the Users:

USING PERIODIC COMMIT
LOAD CSV FROM 'file:/Users/Oliver/Desktop/Assessment_CSV/Assessment_table_Users.csv' AS csvLineB
CREATE (n:Users {
  UserID: csvLineB[0],
  Firstname: csvLineB[1],
  Surname: csvLineB[2],
  DOB: csvLineB[3],
  Email: csvLineB[4],
  Phone: csvLineB[5]});

I then import the messages using:

USING PERIODIC COMMIT
LOAD CSV FROM 'file:/Users/Oliver/Desktop/Assessment_CSV/Assessment_table_Messages.csv' AS csvLineD
CREATE (n:Message {
  MessageID :csvLineD[0],
  MessageContent:csvLineD[1]});

All the nodes appear correctly at this point.

I am then attempting to create the relationships from the additional fields in the messages csv file these are the SenderID and the RecipientID which correspond to the UserID's, however when I run the following statement it does not do anything:

USING PERIODIC COMMIT
LOAD CSV FROM 'file:/Users/Oliver/Desktop/Assessment_CSV/Assessment_table_Message.csv' AS csvLineE
MATCH (sender:Users)
WHERE sender.id = csvLineE[3]
MATCH (receiver:Users)
WHERE receiver.id = csvLineE[2]
MATCH (msg:Message)
WHERE msg.id = csvLineE[0]
MERGE(sender)-[:SENT]->(msg)<-[:RECEIVED]-(receiver);

Could anyone advise as to what I am doing wrong?

Any help is much appreciated.


Solution

  • Your third query is using the wrong property names for the ids. Try this:

    USING PERIODIC COMMIT
    LOAD CSV FROM 'file:/Users/Oliver/Desktop/Assessment_CSV/Assessment_table_Message.csv' AS csvLineE
    MATCH (sender:Users)
    WHERE sender.UserID = csvLineE[3]
    MATCH (receiver:Users)
    WHERE receiver.UserID = csvLineE[2]
    MATCH (msg:Message)
    WHERE msg.MessageID = csvLineE[0]
    MERGE(sender)-[:SENT]->(msg)<-[:RECEIVED]-(receiver);