Thursday, October 29, 2020

Neo4j - Import Data from a CSV File using Cypher

 You can import data from a CSV (Comma Separated Values) file into a Neo4j database. To do this, use the LOAD CSV clause.


LOAD CSV FROM 'https://www.quackit.com/neo4j/tutorial/genres.csv' AS line

CREATE (:Genre { GenreId: line[0], Name: line[1]})



Import a CSV file containing Headers


The previous CSV file didn't contain any headers. If the CSV file contains headers, you can use WITH HEADERS.


Using this method also allows you to reference each field by their column/header name.


We have another CSV file, this time with headers. This file contains a list of album tracks.


Again, this one's not a large file — it contains a list of 32 tracks, so it will create 32 nodes (and 96 properties).


This file is also stored on Quackit.com, so you can run this code from your Neo4j browser and it should import directly into your database (assuming you are connected to the Internet).


You can also download the file here: tracks.csv



LOAD CSV WITH HEADERS FROM 'https://www.quackit.com/neo4j/tutorial/tracks.csv' AS line

CREATE (:Track { TrackId: line.Id, Name: line.Track, Length: line.Length})



Importing Large Files


If you're going to import a file with a lot of data, the PERODIC COMMIT clause can be handy.


Using PERIODIC COMMIT instructs Neo4j to commit the data after a certain number of rows. This reduces the memory overhead of the transaction state.


The default is 1000 rows, so the data will be committed every thousand rows.


To use PERIODIC COMMIT just insert USING PERIODIC COMMIT at the beginning of the statement (before LOAD CSV)


Here's an example:



USING PERIODIC COMMIT

LOAD CSV WITH HEADERS FROM 'https://www.quackit.com/neo4j/tutorial/tracks.csv' AS line

CREATE (:Track { TrackId: line.Id, Name: line.Track, Length: line.Length})




References:

https://www.quackit.com/neo4j/tutorial/neo4j_import_data_from_csv_file_using_cypher.cfm


No comments:

Post a Comment