Import relationships using a csv file

Hi, I'm new to neo4j and Cypher language.
I have created 2 kinds of nodes using cypher:
image

load csv with headers from 'file:///trackCheckContent.csv' as line
create(:TrackCheckContent{risk:line.risk,risk_id:line.risk_id,info_source:line.source,url:line.URL,checkFrequency:line.checkFrequency})

load csv with headers from 'file:///checkAppliance.csv' as line
create(:checkAppliance{appliance:line.check_appliance,appliance_id:line.appliance_id})

Now I want to create relationship between these 2 kinds of node.
I have a csv file be like:
image

then I use cypher:

LOAD CSV WITH HEADERS FROM "file:///risk_appliance.csv" AS row 
match (from:TrackCheckContent{id:row.risk_id})
match (to:checkAppliance {id:row.appliance_id}) 
merge (from)-[r:use]->(to)

It turns out to be (no changes, no records)

And if I try this:

LOAD CSV WITH HEADERS FROM "file:///risk_appliance.csv" AS row 
match (from:TrackCheckContent{id:row.risk_id}),(to:checkAppliance {id:row.appliance_id}) 
merge (from)-[r:use]->(to)

I will get This query builds a cartesian product between disconnected patterns.

I just want to create relationships between these 2 kinds of node. plz help :cry:

Hello @11li and welcome to the Neo4j community :slight_smile:

Can you share the CSV files to create nodes and relationships?

Regards,
Cobra

Hi!
Followings are part of my files.
csv can't upload.
trackCheckContent.csv

checkAppliance.csv
image

Thank you!

Regards,
Yiyi

You can upload files here but you must replace the extension of the files by .txt.

ok! thanks!
checkAppliance.txt (417 Bytes)
risk_appliance.txt (860 Bytes)
trackCheckContent.txt (10.4 KB)

Thank you, I'm gonna try to find your issue now :slight_smile:

First, you must create UNIQUE CONSTRAINTS:

CREATE CONSTRAINT constraint_track_check_content IF NOT EXISTS FOR (n:TrackCheckContent) REQUIRE n.risk_id IS UNIQUE;
CREATE CONSTRAINT constraint_check_appliance IF NOT EXISTS FOR (n:CheckAppliance) REQUIRE n.appliance_id IS UNIQUE;

Then you load nodes:

LOAD CSV WITH HEADERS FROm 'file:///TrackCheckContent.csv' AS line
MERGE (n:TrackCheckContent {risk_id: line.risk_id}) SET n += line
LOAD CSV WITH HEADERS FROm 'file:///CheckAppliance.csv' AS line
MERGE (n:CheckAppliance {appliance_id: line.appliance_id}) SET n += line

Finally, you load relationships:

LOAD CSV WITH HEADERS FROm 'file:///RiskAppliance.csv' AS line
MATCH (a:TrackCheckContent {risk_id: line.risk_id})
MATCH (b:CheckAppliance {appliance_id: line.appliance_id})
MERGE (a)-[:USE]->(b)

Everything worked on my end and I used the latest version of Neo4j (4.4.3).

1 Like

UNIQUE CONSTRAINTS got this error.:cry:

I used Neo4j Browser version: 4.2.2

Neo4j Server version: 4.2.3 (community)

Try that:

CREATE CONSTRAINT constraint_track_check_content IF NOT EXISTS ON (n:TrackCheckContent) ASSERT n.risk_id IS UNIQUE;
CREATE CONSTRAINT constraint_check_appliance IF NOT EXISTS ON (n:CheckAppliance) ASSERT n.appliance_id IS UNIQUE;
1 Like

It worked!!! Thank you very much !

I still got one question that why can't I change the caption of this kind of node.

No problem, what do you mean by the caption?


like this

Oh, yes, you can, there is an arrow on the right on the same line, just click on it and you will see the properties.

oh!! I see!!!
thank you very much !!!!!

1 Like

Sorry to bother you again๐Ÿ˜…
Could you please tell me how can I delete all the data?


After I executed "match (n) detach delete n"
Node labels and property keys are still there. :cry:

I checked your other answers and try drop constraint


It worked!
but the property keys are still there.

If you want to clean everything:

CREATE OR REPLACE DATABASE neo4j

Sorry for the late reply. But it didn't work :cry:

Version: 4.2.3
Edition: Community
Name: neo4j
Databases: :dbs
Information: :sysinfo
Query List: :queries


still can't...

Ah, you are on Community version, that's why it's not working, you can check the documentation here.
For Community version, you will have to drop the folder of the database then recreate a database.