0
votes

I've got a small directed graph stored in a .csv as a sparse matrix. The file comprises 2 columns in the following format:

1,2
2,3
1,3
1,4
2,5
3,4
3,5
4,5

Every row is basically an adjacency relation between two nodes: 1->2, 2->3, 1->3, etc. I want to import this data into neo4j and create a graph (as a first step it can be an undirected graph).

I tried the following Cypher code:

LOAD CSV FROM 'file:///dummy.csv' AS line
CREATE((:node {`name`:line[1]})-[:`connects`]->(:node {`name`:line[2]}))

This is the furthest I've gone so far, but the results are not what I want. I'm reading the file into neo4j but I'm only getting multiple small graphs composed of two nodes with many node duplicates. My aim is to read every line as an adjacency relation and add connections to existing nodes without producing node duplications. Also, I would ideally like to display the name property of each node on top of itself (instead of node id) during graph visualisation. Your help is appreciated.

1

1 Answers

2
votes

You should use MERGE to avoid creating duplicate nodes and relationships:

LOAD CSV FROM 'file:///dummy.csv' AS line
MERGE (a:node {name:line[0]})
MERGE (b:node {name:line[1]})
MERGE (a)-[:connects]->(b);

Also, see the documentation for how to style the browser visualization.