# Create relationship from CSV on existing nodes

**URL:** <https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218>\
**Category:** Neo4j Graph Platform\
**Created:** [October 22, 2019, 9:09am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218 "2019-10-22T09:09:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aztars](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/aztars/32/7017_2.png) [@aztars](https://community.neo4j.com/u/aztars)\
**Post date:** [October 22, 2019, 9:09am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/1 "2019-10-22T09:09:10Z")

</div>

Hi,

I just started using Neo4j to visualize data. I am currently stuck on creating relationships. I have twe CSV files. One with the nodes and all attributes and one with all relationships.

The CSV with all nodes (Object.csv) are succesfully loaded in Neo4j as follows:  
Name, ID

I want to create relationships on these nodes with the following CSV (object-object-link.csv):  
Unid (relationship id), parentid, childid.

I want to join the Parentid and the child id from the relationship csv on the ID from the node CSV. How should I proceed? There are 51k relationships.

Thank you.

---

<div class="post-metadata">

**Author:** ![lju](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lju/32/12574_2.png) [@lju](https://community.neo4j.com/u/lju)\
**Post date:** [October 22, 2019, 11:42am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/2 "2019-10-22T11:42:42Z")

</div>

Hi!

You can do this across multiple CSVs as long as you've got some way to uniquely identify the nodes you wish to connect the relationships to.

Whilst this article is about performant LOAD CSV - it gives a great example of how you'd look up existing nodes as part of LOAD CSV to connect them: [Neo4j: Cypher - Avoiding the Eager | Mark Needham](https://markhneedham.com/blog/2014/10/23/neo4j-cypher-avoiding-the-eager/)

I hope this helps!

---

<div class="post-metadata">

**Author:** ![aztars](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/aztars/32/7017_2.png) [@aztars](https://community.neo4j.com/u/aztars)\
**Post date:** [October 22, 2019, 11:53am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/3 "2019-10-22T11:53:02Z")

</div>

Thank you for your response. The ID from the nodes are unique and are equivalent to either a parentid or a childid from the relationship CSV.

I tried to read through the link but I can't see what the connection is to my question, might be because of my inexperience though.

---

<div class="post-metadata">

**Author:** ![lju](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lju/32/12574_2.png) [@lju](https://community.neo4j.com/u/lju)\
**Post date:** [October 22, 2019, 12:02pm UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/4 "2019-10-22T12:02:05Z")

</div>

No problem at all, here's an example:

Let's say our Nodes.csv file which contains people, looks something like this...

```auto
id, name
1, Bob
2, Jane

```

Let's say our Relationship.csv file, which contains who knows who, looks something like this...

```auto
id_from, id_to
1, 2

```

We do our first pass which creates our person nodes, i.e.

```auto
LOAD CSV WITH HEADERS FROM "file:///Nodes.csv" AS row
CREATE (:Person {id:row.id, name:row.name});

```

Then, we're going to use the id property as a look-up so that we can join the nodes together, i.e.

```auto
LOAD CSV WITH HEADERS FROM "file:///Relationships.csv" AS row
//look up the two nodes we want to connect up
MATCH (p1:Person {id:row.id_from}), (p2:Person {id:row.id_to})
//now create a relationship between them
CREATE (p1)-[:KNOWS]->(p2);

```

Does that make sense?

---

<div class="post-metadata">

**Author:** ![DanielGittx](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/danielgittx/32/11558_2.png) [@DanielGittx](https://community.neo4j.com/u/DanielGittx)\
**Post date:** [May 19, 2020, 1:29am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/5 "2020-05-19T01:29:53Z")

</div>

Hi @lju,  
Thanks for this response. I am facing a similar issue and had already come to a solution similar to your proposal.  
While it works for small data, it's not optimal for large datasets (100M+ nodes , 1B+ relationships)..

Any proposals?

---

<div class="post-metadata">

**Author:** ![lju](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lju/32/12574_2.png) [@lju](https://community.neo4j.com/u/lju)\
**Post date:** [June 30, 2020, 9:14am UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/6 "2020-06-30T09:14:48Z")

</div>

Hi @DanielGittx  
Sorry I missed your post!

If you're looking at that size of data - if it's an initial load, I would heartily suggest you use the offline batch importer! It will load that of that size significantly faster than an online, transactional process (which LOAD CSV is).

You can find out more [here](https://neo4j.com/developer/guide-import-csv/#batch-importer).

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [November 4, 2020, 8:39pm UTC](https://community.neo4j.com/t/create-relationship-from-csv-on-existing-nodes/11218/7 "2020-11-04T20:39:27Z")

</div>

A. To make this example to work, you need to remove the spaces from the .csv file.

B. (EDIT): I had a typo. I fixed the query and it now works.

> I was problems getting this to work when the nodes are of **different** types:

owner.csv

```auto
id,name
1,Bob

```

```auto
id,petname
2,Rover

```

```auto
LOAD CSV WITH HEADERS FROM "file:///pet.csv" AS row
CREATE (:pet {id:row.id, petname:row.petname});

```

```auto
LOAD CSV WITH HEADERS FROM "file:///owner.csv" AS row
CREATE (:owner {id:row.id, name:row.name});

```

and using the same relationship file:

```auto
LOAD CSV WITH HEADERS FROM "file:///Relationships.csv" AS row
//look up the two nodes we want to connect up
MATCH (p1:owner {id:row.id_from}), (p2:pet {id:row.id_to})
//now create a relationship between them
CREATE (p1)-[:OWNS]->(p2);

```
