
Importing data to Neo4j the spreadsheet way in Neo4j 2.0!
Rik Van Bruggen
did a lovely
blog post on how to import data to Neo4j using spreadsheets
in March last year. Simple and easy to understand but only for Neo4j version 1.9.3. Now it’s a new year and in December we launched a shiny new version of Neo4j, the 2.0.0 release! Baadadadaam! So, I thought better provide an update to his
blogpost,
with the spirit of his work. (Thank you Rik!)
Now for 2.0.0
) from
Michael Hunger
, or look at other
Data Import Options
.
How does it work?
Open the spreadsheet.
-
columnsA, B
and
C
: these contain the data for the
Nodes
of our graph, using a custom “
id
”, a “
name
”, and a “
gender” as properties.
-
columnsF, 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
.
statements from this nodes and relationship information.
cypher syntax
and string concatenation. Look at the columns
D
and
I
:
Nodes
=”MERGE (meetup:Event {id:’”&A3&”‘, name:’”&B3&”‘})”
here in the Neo4j Manual
.
Output for row 3:
MERGE (meetup:Event{id:’153602002′, name:’Meetup Malmö’})
Output for row 4:
As you can see, it takes that
id
,
name
and
gender
properties from columns
A, B
and
C
, and puts these into a “MERGE” Cypher statement.
Relationships
In this particilar dataset we don’t have to create a index from our labels and the nodes properties, but since I can do it I will show you.
The reason why we are using WITH 1 as dummy is that
it’s for the single statement for the neo4j-browser where all the match merge follow each other with no separation in a single big query.
Output for row 2:
in column
G
.
Having done this, we end up with two columns each containing a number of cypher statements. So then what?
The Instructions Sheet
-
Download
and unzip Neo4j server.
-
Copy/paste the Cypher statements from the top part the Import Sheet into a text file or the browser window directly.All these statements form a
single large Cypher statement
as the browser can currently only execute single cypher statements.
-
Drag the file into the browser input area and then execute it
-
If you want to use the Neo4j-Shell for importing larger amounts of data use the approach shown in the second tab titled: “For the shell”.It uses one cypher statement (terminated with semi-colons) per line
and a begin / commit block around the statements to speed up the import with a single transaction.
-
Paste all statements into a file and usebin/neo4j-shell -file import.txt
or copy and paste direct in the browser
You can download it here.
Want to learn more about graph databases? Click below to get your free copy of O’Reilly’s Graph Databases ebook and discover how to use graph technologies for your application today.