Loading CSV relationships using Cypher

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 :slightly_smiling_face: 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

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 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 :slightly_smiling_face: Will let you know how it works out.

Thanks,

Torkel

Hi
I'm having a similar issue with the OP.

I have two csv files.

  1. account.csv (node) - account_id and customer_id.
  2. transaction.csv (relationship) - account_id_source, account_id_destination and transaction_id, amount, timestamp

I can load and create the nodes however cannot create the relationships. I've attempted Gary's solution but I result in no output -> "(no changes, no records)"

load csv with headers from "file:///account.csv" as row
with row where row.trn_id is not null
match (a1:Account{acc_id:row.acc_id_src})
match (a2:Account {acc_id:row.acc_id_dst})
merge (a1)-[t:transferred_to]->(a2)
set t.createdAt:row.timestamp, t.trn_id:row.trn_id, t.amount:row.amount;

Would you be able to advise?

Thanks very much in advance!

I'm very embarrassed because I have spent some time trying to resolve this - only for the basics to hit me. Cypher import csv data as a string. I need to convert the id to integer.

1 Like