-1

I want to import an excel file which is actually a relationship matrix nxn with several relationship types between the n elements.

id| name     | element1 | element2 | element3 ... n
1 | element1              1;2;3        1;5
2 | element2   1;2;3
3 | element3     1;5
...

I saved the excel as CSV, which creates a lot of null cells (empty cells).

In Neo4j I tried:

load csv with headers from 'file:///test.csv' as line
WITH line where not line.ID is null
merge (c:element{id: toInteger(line.id),name: line.name})

as a result, I receive no changes and records.

Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
RR_514
  • 23
  • 5

2 Answers2

0

Property names are case-sensitive in Neo4j Cypher.

Your file has column id but you are checking where not line.ID is null. This will skip all the rows as there is no ID column in your CSV file.

Find below the modified version of your query for readability:

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS line
WITH line 
WHERE line.id IS NOT NULL
MERGE (c:element{id: toInteger(line.id),name: line.name})
Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
0

Aside from @Raj's answer (property names are case sensitive), your CSV file needs to use the same delimiter between all values.

Your CSV file (as shown in your question) is using inconsistent delimiters. It uses spaces sometimes, and spaces and vertical bars (|) at other times. This will not work, as the LOAD CSV clause only supports a single kind of delimiter. Also, if you are not using the default delimiter, the comma, then you need to use the FIELDTERMINATOR option to specify what that delimiter is.

Here is a fixed version of your CSV file that uses the default delimiter, the comma. Notice that 2 adjacent commas are used to indicate a missing value. If a line is missing its first value, then the first comma should be at the beginning of that line. In addition, notice that there are no extraneous spaces.

id,name,element1,element2,element3
1,element1,,1;2;3,1;5
2,element2,1;2;3
3,element3,1;5
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • now I also want to import the relations, how do I automate it? The following only creates the relation for one line, how to iterate along all lines with a name? 'LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS line WITH line WHERE line.element1 IS NOT NULL Match (c:element{id: toInteger(line.id),name: line.name}) Match (d:element{name:'element1'}) Merge (d)-[:REL{type: line.element1}]-(c) return c,d' – RR_514 Jun 03 '19 at 13:54
  • I think you should pose a new question. There are a lot of potential issues with the Cypher in your comment, and solving them would overcomplicate this already complex question. – cybersam Jun 03 '19 at 18:03