# Loading multiple CSV files in Neo4j with each row of each CSV being a node

**URL:** <https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663>\
**Category:** Import / Export\
**Tags:** load-csv\
**Created:** [November 6, 2019, 5:00pm UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663 "2019-11-06T17:00:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [November 6, 2019, 5:00pm UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/1 "2019-11-06T17:00:09Z")

</div>

I need to upload multiple CSV files in Neo4j with each row of each CSV file being a node. How to effectively do this? I cannot concatenate the CSVs since each row is a label and the rows are the same across multiple CSVs.

It looks like the LOAD CSV command either take a single CSV and generates nodes for each row or takes a bunch of CSVs and makes a node for each CSV. Thanks!

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [November 6, 2019, 7:32pm UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/2 "2019-11-06T19:32:34Z")

</div>

LOAD CSV reads only one file at a time. You have to run the import with each file.

Other option is copy the data from one file and copy into the first file as second column.

---

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [November 6, 2019, 10:19pm UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/3 "2019-11-06T22:19:38Z")

</div>

Thanks. Can I automate the import with each file? Like writing LOAD CSV _filename_ multiple times in a file and uploading the query file somewhere so each LOAD CSV query is executed? If so, how do I do it?

---

<div class="post-metadata">

**Author:** ![mckenzma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mckenzma/32/78_2.png) [@mckenzma](https://community.neo4j.com/u/mckenzma)\
**Post date:** [November 7, 2019, 1:54am UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/4 "2019-11-07T01:54:58Z")

</div>

One way I have handled importing ~1000 files is to create another CSV file with the file names/urls. I use `LOAD CSV` to import the rows from the CSV that contains all the file names and create nodes for each file. Then iterate over the file nodes to import rows from each file.

---

<div class="post-metadata">

**Author:** ![kasthuri](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/kasthuri/32/7299_2.png) [@kasthuri](https://community.neo4j.com/u/kasthuri)\
**Post date:** [November 7, 2019, 2:15am UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/5 "2019-11-07T02:15:47Z")

</div>

Ok, I will try this. Is there a query that you could provide for such creation? Like a pseudo-code with Cypher statements. Sorry, I am pretty new to Neo4j. Thanks!

---

<div class="post-metadata">

**Author:** ![mckenzma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mckenzma/32/78_2.png) [@mckenzma](https://community.neo4j.com/u/mckenzma)\
**Post date:** [November 29, 2019, 3:07pm UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/6 "2019-11-29T15:07:47Z")

</div>

Hi,  
Here is an example of what I have been using:

```auto
LOAD CSV WITH HEADERS FROM "url" AS row
WITH row
WITH row, row.URL AS fileUrl
MERGE (file:File: {url: fileUrl})
ON CREATE SET file.url = fileUrl,
              file.folder = row.Folder,
              file.name = row.File,
              file.createdOn = timestamp()

```

Note that I am currently creating `File` Nodes in my graph to store the url and then iterating over the file nodes to import and connect data.

```auto
MATCH (file:File)
WHERE NOT (file)-[:CONTAINS]->(:Row)
WITH collect(file.url) AS fileURLs
UNWIND fileURLs AS fileURL
CALL apoc.periodic.iterate(
'
CALL apoc.load.csv($url,{header:true,quoteChar:"\u0000"}) YIELD map AS row
RETURN row
','
CREATE (fileRow:Row {createdOn: date()})
SET fileRow += row,
fileRow.url = $url,
fileRow.createdOn = date()
',
{batchSize:10000,parallel:false,params:{url:fileURL}}) YIELD batches, total
RETURN batches, total

```

After this iterate through and connect file nodes to row nodes. Then once all of that is in the graph I just start working with the raw data within the graph.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![tangxingbang](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tangxingbang/32/22619_2.png) [@tangxingbang](https://community.neo4j.com/u/tangxingbang)\
**Post date:** [February 24, 2022, 4:45am UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/7 "2022-02-24T04:45:34Z")

</div>

I use python ,neo4j python driver , python os , python csv. Then let python uploads files to database one by one.

> **[Using Neo4j from Python - Developer Guides](https://neo4j.com/developer/python/)**
>
> This guide provides an overview of how to connecting to Neo4j from Python.

> **[csv — CSV File Reading and Writing](https://docs.python.org/3/library/csv.html)**
>
> Source code: Lib/csv.py The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. CSV format was used for many years prior to att...

> **[os — Miscellaneous operating system interfaces](https://docs.python.org/3/library/os.html)**
>
> Source code: Lib/os.py This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, s...

---

<div class="post-metadata">

**Author:** ![sameer.gijare14](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/sameer.gijare14/32/11986_2.png) [@sameer.gijare14](https://community.neo4j.com/u/sameer.gijare14)\
**Post date:** [February 24, 2022, 7:07am UTC](https://community.neo4j.com/t/loading-multiple-csv-files-in-neo4j-with-each-row-of-each-csv-being-a-node/11663/8 "2022-02-24T07:07:44Z")

</div>

May be you can write a custom procedure that takes input folder where you keep all CSV files.You can loop over all file handles ,then transform data as per your target Data model and finally load it into Your graph DBMS.  
Many thanks  
Mr Sameer S Gijare
