Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-18-2020 02:54 AM
Hello!! I have two labels which represent money transfers. We have the column sender, receptor, quantity, date and specific code value whis is different for each transaction:
sender;receptor;quantity;date;code
Almu;Joselu;200;17/02/2020;AJ01
Joselu;Angel;500;17/02/2020;JA01
Angel;Isai;2;17/02/2020;AI01
Joselu;Isai;66;17/02/2020;JI01
Alexo;Angel;10;17/02/2020;AA01
Almu;Alexo;600;17/02/2020;AA02
Isai;Alexo;1000;17/02/2020;IA01
Emilio;Joselu;10;17/02/2020;EJ01
I import this data with a Load statement and at the end I get my two labels sender and receptor
The import code is like this:
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
CREATE (e:receptor {receptor:c.receptor, code.code})
And similar for the sender.
My problem is that when I try to create a relationship between sender and receptor, I get conexions that are repeated.
The code that I use is:
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:son]->(b)
And it look like:
I would like not to have nodes that are repeated. Something that looks like:
Thanks in advance for your time and help!
02-18-2020 03:06 AM
Hello!
You should create a uniqness constraint to avoid duplicity. For example:
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE;
And probably use MERGE instead of CREATE in the LOAD sentence, then create the relationships.
02-18-2020 03:31 AM
Hello Pilar!
So I should write:
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})
And then for each of the labels create the constraint:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE
And for making the relation, like this:
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)
Thanks!!
02-18-2020 04:09 AM
That looks good 😉
Besides, consider setting all the information about the money transfer: quantity, date and code in the relationship.
02-18-2020 04:30 AM
You might want to do that first. That way the Merge will perform with more reasonable response times as it creates index.
02-18-2020 06:02 AM
But if I have already created the constraint, when I try to merge the rest of the info, I get an error telling me that Node(9546906) already exists with label Sender
and property sender
= 'Joselu'
My code is:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)
However, as I have said, when I try the first Load I get the error...
Thanks!
02-18-2020 06:57 AM
Try to rewrite the code in this way:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Receptor {receptor:c.receptor})
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)
02-18-2020 07:37 AM
Thanks Pilar for all your help, I really appreciate all your effort.
Your code works perfect. It created the unique nodes in the correct way. However, for creating the relationship I need to add the code of the transsaction when I am loading the properties, and it gives me error. I saw you:
code
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
code
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE
3.code
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender, code:c.code})
5.code
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)
In the 1st load with the code:c.code propertie I get:
Node(47) already exists with label AA
and property emisor
= 'Joselu'
Adding the property gives me an error...
02-18-2020 11:08 AM
Since the constraint is only on sender, It will try to treat only sender as unique. If the sender and code combination is unique, then the constraint should be on the combination.
CREATE CONSTRAINT ON (e:Sender) ASSERT (e.sender, e.code) IS NODE KEY
Can you please try that?
All the sessions of the conference are now available online