I have a test .csv file, and want to create a graph that has unique nodes for each year but also unique nodes for the Numbers values
My cypher is:
LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS row MERGE (y:Year {name: row.Year}) WITH y, row UNWIND split (row.Numbers, ',') AS X MERGE (n: Numbers {name: X}) MERGE (y)-[r:LINK]->(n) RETURN y, r, n
which produces duplicate Numbers nodes ("3" in this case).
which produces duplicate Numbers nodes ("3" in this case).
Is there a way to create uniqueness with the Numbers node values as well (only have 1, 2, 3)? In other words I only want 2016 to be associated with three unique nodes (no duplicates in the Numbers nodes).
Thank you for looking at this. Interesting! I'm guessing that it could be the space between the ',' and digit 3. When I get rid of that space in the .csv file it works the way you saw. Wonder if in one case is sees '3' and another ' 3'. Thank you!
That is correct. I created a file without spaces. The split is parsing on β,β and is not designed to compensate for a space after the comma. You can use the βtrimβ function if you want to avoid this in the future.
In those cases where you would use the value of 'X' in multiple places and don't want to wrap each usage with 'trim', you can eliminate the extra spaces upfront with list comprehension.
LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS row
MERGE (y:Year {name: row.Year})
WITH y, row
UNWIND [i in split (row.Numbers, ',') | trim(i)] AS X
MERGE (n: Numbers {name: X})
MERGE (y)-[r:LINK]->(n)
RETURN y, r, n