cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Import relationships using a csv file

11li
Node Clone

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

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:
3X_f_1_f1984ff3937753eef88fd37babd144143ee69d7f.png

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

1 ACCEPTED SOLUTION

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;

View solution in original post

26 REPLIES 26

Hello @11li and welcome to the Neo4j community

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
3X_d_9_d9281b0ae967b00bd38eaf0d9a4efbd3730eadea.png

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

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).

UNIQUE CONSTRAINTS got this error.

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;

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 !!!!!

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.

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

11li
Node Clone
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.

Thank you!
So I can't just write a line to delete all peoperty keys, right?

Yeah that's right, there is no alternative option right now:)

Documentation only support enterprise edition...

and I don't know which part of the folder can be deleted...

Is there a documentation support Community version?

Delete the neo4j folder in the databases folder and it should be good but you will have to recreate a database after.


I delete the noe4j database...and I couldn't create it back...
How to create a database in community version?

Did you shutdown the server before to delete the folder and did you restart it after?

You should open another topic for this problem.

Thank you!
Yes, I did shutdown the server before I delete the neo4j folder and restart it.
But it turns out to be the picture I sent. So I delete the whole folder and download a new community version
Now it's ok.
I will open another topic when I have to create a database after.