0
votes

I have a csv file with below columns and sample data provided, and I've loaded into Neo4j and got stuck when I was trying to create relationships.

**source destination miles**
a         b         5
a         c         6
a         d         20

Now I want to create a graph with source in middle and connected destinations around and label with miles between two stops.(A star graph with source in middle), so I tried below queries, it's not returning miles on the label, I'm new to Neo4j, any help is appreciated, thanks in advance.

LOAD CSV WITH HEADERS FROM "file:///sample.csv" AS line
CREATE (s:src{id: line.source}) 
CREATE (d:dst{id: line.destination}) 
CREATE (s)-[r:trips {total: [line.miles]}]->(d)
RETURN s, d, r;
1

1 Answers

1
votes

By default, LOAD CSV expects the CSV file to use comma separators, and it does not support extraneous whitespace. Try changing the content of your CSV file to this:

source,destination,miles
a,b,5
a,c,6
a,d,20

Also, you should use MERGE instead of CREATE to avoid creating duplicate nodes. And there is no evident need to store the miles value in an array, so this query stores it as a scalar value:

LOAD CSV WITH HEADERS FROM "file:///sample.csv" AS line
MERGE (s:src {id: line.source}) 
MERGE (d:dst {id: line.destination}) 
CREATE (s)-[r:trips {miles: line.miles}]->(d)
RETURN s, d, r;

The result of the above is:

╒══════════╤══════════╤══════════════╕
│"s"       │"d"       │"r"           │
╞══════════╪══════════╪══════════════╡
│{"id":"a"}│{"id":"b"}│{"miles":"5"} │
├──────────┼──────────┼──────────────┤
│{"id":"a"}│{"id":"c"}│{"miles":"6"} │
├──────────┼──────────┼──────────────┤
│{"id":"a"}│{"id":"d"}│{"miles":"20"}│
└──────────┴──────────┴──────────────┘