Tuesday, December 8, 2020

Importing Data from Spreadsheet to Neo4J

From this article, the sample sheet is here https://docs.google.com/spreadsheets/d/1ggI2s-ttysSxJ1PymNz3GESE5vkbRIQI1MSsFVbe-3U/edit#gid=1 

This sheet is composed of two parts:

Columns A, B and C: These contain the data for the Nodes of our graph, using an “id”, a “name”, and a “type”

Columns F, G and H: These contain the data for the Relationships of our graph, having a “from-id” (where the relationship starts), a “to-id” (where the relationship ends), and a “relationship type”. Columns F and G reference the nodes and their id’s in column A.


For this we use very simple statements that leverage the columns mentioned above, the Cypher syntax and string concatenation. Look at the columns D and I:


Cypher statements to create the nodes:


create n={id:'"&A2&"', name:'"&B2&"', type:'"&C2&"'};


output for row 2:


create n={id:'1', name:'Amada Emory', type:'Female'};


Copy/paste the Cypher statements from the Import Sheet into a text file.

Wrap these with a Neo4j transaction (begin, commit) – so that all of the statements get persisted to disk in the same transaction (or not in case of an error). (This is not important for smaller datasets, but is much more important for larger datasets.)

Some instructions on how to enable auto-indexing on Neo4j. This is important, because as you insert data into the database, it needs to get indexed for setting up the relationships properly (see above) and for future use.

And some instructions on how you can pipe the text file into the Neo4j shell – if necessary. For small datasets (and therefore, a limited number of Cypher statements) you can do with copy/pasting the text file into the Web-UI console – but that might not always work.

Starting the server and browsing the Web-UI


Thats it mainly. This is quite straight forward. 



References:

https://neo4j.com/blog/importing-data-into-neo4j-the-spreadsheet-way/


No comments:

Post a Comment