How To: Neo4j Data Import – Minimal Example

Posted by Michael Hunger on Apr 18, 2015 in import, neo4j |

We want to import data into Neo4j, there are too many resources with a lot of information which makes it confusing.
Here is the minimal thing you need to know.

Imagine the data coming from the export of a relational or legacy system, just plain CSV files without headers (this time).

people.csv
1,"John"
10,"Jane"
234,"Fred"
4893,"Mark"
234943,"Anne"
friendships.csv
1,234
10,4893
234,1
4893,234943
234943,234
234943,1

Graph Model

Our graph Model would be very simple:

import data model.jpg
(p1:Person {userId:10, name:"Anne"})-[:KNOWS]->(p2:Person {userId:123,name:"John"})

Import with Neo4j Server & Cypher

  1. Download, install and start Neo4j Server.

  2. Open http://localhost:7474

  3. Run the following statements one by one:

I used http-urls here to run this as an interactive, live Graph Gist.

CREATE CONSTRAINT ON (p:Person) ASSERT p.userId IS UNIQUE;
LOAD CSV FROM "https://gist.githubusercontent.com/jexp/d8f251a948f5df83473a/raw/people.csv" AS row
CREATE (:Person {userId: toInt(row[0]), name:row[1]});
USING PERIODIC COMMIT
LOAD CSV FROM "https://gist.githubusercontent.com/jexp/d8f251a948f5df83473a/raw/friendships.csv" AS row
MATCH (p1:Person {userId: toInt(row[0])}), (p2:Person {userId: toInt(row[1])})
CREATE (p1)-[:KNOWS]->(p2);
You can also use file-urls.
Best with absolute paths like file:/path/to/data.csv, on Windows use: file:c:/path/to/data.csv

If you want to find your people not only by id but also by name quickly, also run:

CREATE INDEX ON :Person(name);

For instance all second degree friends of “Anne” and on how many ways they can be reached.

MATCH (:Person {name:"Anne"})-[:KNOWS*2..2]-(p2)
RETURN p2.name, count(*) as freq
ORDER BY freq DESC;

Bulk Data Import

For tens of millions up to billions of rows.

Shutdown the server first!!

Create two additional header files:

people_header.csv
userId:ID,name
friendships_header.csv
:START_ID,:END_ID

Execute from the terminal:

path/to/neo/bin/neo4j-import --into path/to/neo/data/graph.db  \
--nodes:Person people_header.csv,people.csv --relationships:KNOWS friendships_header.csv,friendships.csv

After starting your database again, run:

CREATE CONSTRAINT ON (p:Person) ASSERT p.userId IS UNIQUE;
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Netvibes
  • PDF
  • Ping.fm

6 Comments

  • George says:

    Where is neo4j-import?

    I don’t have one in my bin folder and I don’t have one after following the directions to install the Neo4j Management tools via PowerShell in Windows.

    Thank you!

  • If you installed the zip distribution you should find Neo4jImport.bat on your system.

  • The above is not working….

    neo4j-import –into C:\Users\nprasadpatro\Downloads\neo4j-community-3.0.3-windows\neo4j-community-3.0.3\data\databases \
    –nodes:Person C:\Users\nprasadpatro\Desktop\people_header.csv,C:\Users\nprasadpatro\Desktop\people.csv –relationships:KNOWS C:\Users\nprasadpatro\Desktop\friendships_header.csv,C:\Users\nprasadpatro\Desktop\friendships.csv

    I am running this command in CMD under bin directory C:\Users\nprasadpatro\Downloads\neo4j-community-3.0.3-windows\neo4j-community-3.0.3\bin getting some error please help.

  • headcode says:

    Bravo! In casting about for info on loading a CSV all the neo4j “help” pages assume you already basically know how to do it. This is a great example. Thank you.

  • Hello Michael,

    this is a very nice little example.

    Good luck to you.

    Regards
    Hans Werner

  • You have to share more detail, best on StackOverflow or on neo4j.com/slack

    Please note that you need to run a script like this to get neo4j-import on the Neo4j-Community-Desktop app
    “C:\Program Files\Neo4j Community\jre\bin\java” -cp “C:\Program Files\Neo4j Community\bin\neo4j-desktop-3.0.3.jar” org.neo4j.tooling.ImportTool %*

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2017 Better Software Development All rights reserved.
Multi v1.4.5 a child of the Desk Mess Mirrored v1.4.6 theme from BuyNowShop.com.