cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Loading CSV relationships using Cypher

TorkelJ
Node

Hi smart ladies and gents,

I am sort of new to this platform as well as a newbie in regards to Cypher. So, I am trying to implement a very small subset of an analytics tool into a graph DB.

So, managed to extract a very small subset (it is several hundreds of tables, but I want to start somewhere).

This is right now in two CSV files. One containing programs and one containing relations for what program call what program. The first one containing the programs have a massive amount of information in it, but I managed to use Cypher to only extract 2 columns; Objectid and Name, where as you might imagine the Objectid is an unique identifier generated by SQL Server and Name is the Name of the programs. Worked fine after a little bit of trial and error. However, this is now loaded and I can see it graphically represented. Very cool.

NOW, here is my little problem. The other CSV file only have 2 columns. Column one is the id of the calling program and column two is the id of the program being called. I want to of course add this to my graph. Most of you are probably smiling now at my ignorance of Cypher. I have have looked at the Northwind sample instructions, but I can not really get exactly how to code this LOAD of the relationships.

So, in case anyone want to impress me a bit and get this first "real" sample working it would be very appreciated. Despite what you might think, I am not stupid. Just want one working sample and I can expand from there 🙂 This is very exciting technology and I would love to try to automate loading a complete repository eventually.

Hope you all have a great weekend,

Torkel

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Assuming your second csv file has columns 'calling_id' and 'called_id', as shown below, you can use the following cypher query.  Just modify it to your actual column names, csv file name, and relationship label. 

Screen Shot 2022-07-08 at 11.55.45 AM.png

load csv with headers from "file:///Programs.csv" as line
match(n:Program{id:line.calling_id})
match(m:Program{id:line.called_id})
merge(n)-[:CALLED]->(m)

Screen Shot 2022-07-08 at 12.02.39 PM.png 

By the way, all values from csv imports are imported as strings. If you want to interpret any as a number, you will need to use the 'to' methods, such as 'toInteger()' or 'toFloat(). 

The cypher reference card is a great resource: https://neo4j.com/docs/cypher-refcard/current/

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Assuming your second csv file has columns 'calling_id' and 'called_id', as shown below, you can use the following cypher query.  Just modify it to your actual column names, csv file name, and relationship label. 

Screen Shot 2022-07-08 at 11.55.45 AM.png

load csv with headers from "file:///Programs.csv" as line
match(n:Program{id:line.calling_id})
match(m:Program{id:line.called_id})
merge(n)-[:CALLED]->(m)

Screen Shot 2022-07-08 at 12.02.39 PM.png 

By the way, all values from csv imports are imported as strings. If you want to interpret any as a number, you will need to use the 'to' methods, such as 'toInteger()' or 'toFloat(). 

The cypher reference card is a great resource: https://neo4j.com/docs/cypher-refcard/current/

Hi,

Thanks a mill, will give this a try tomorrow. I will just use row[x] syntax, I learned that much at least as using the headers did not work very well for me (for some reason). Will do the toInteger as I used that in the initial load as well 🙂 Will let you know how it works out.

Thanks,

Torkel