Search code examples
pythonneo4jpy2neo

How to insert Bulk data into Neo4j using Python


I want to insert some data into Neo4j using py2neo . Link to data file. I am new to Neo4j. Can someone tell me how to insert bulk data into Neo4j.Actually i want to do performance testing of Neo4j.....

I have tried this but this is just for small data set ...

from pprint import pprint
from py2neo import neo4j,node, rel
graph_db = neo4j.GraphDatabaseService()

def insert_data():
    die_hard = graph_db.create(
        node(name="Bruce Willis"),
        node(name="John McClane"),
        node(name="Alan Rickman"),
        node(name="Hans Gruber"),
        node(name="Nakatomi Plaza"),
        rel(0, "PLAYS", 1),
        rel(2, "PLAYS", 3),
        rel(1, "VISITS", 4),
        rel(3, "STEALS_FROM", 4),
        rel(1, "KILLS", 3),)
    pprint(die_hard)

insert_data()

error :

src/test/java/org/neo4j/batchimport/TestDataGenerator.java:3: error: package org.junit does not exist
import org.junit.Ignore;
                ^
src/test/java/org/neo4j/batchimport/TestDataGenerator.java:14: error: cannot find symbol
@Ignore
 ^
  symbol: class Ignore
2 errors

Solution

  • Not sure if this is the issue you're having, but when I tried your sample, I got errors on the [name=] syntax. What's being passed to the node() constructor is a dictionary. There are multiple syntax for the node() constructor, and I didn't see a syntax that matches the one your using. So, try using dictionary syntax like this:

    node({"name": "Bruce Willis"})
    

    Also, I'm not sure if you've configured a default neo4j url, but I had to specify a connection point url in my new4j.GraphDatabaseService() call.

    So, your code would look like:

    from pprint import pprint
    from py2neo import neo4j, node, rel
    graph_db = neo4j.GraphDatabaseService('http://localhost:7474/db/data')
    
    def insert_data():
        die_hard = graph_db.create(
            node({"name": "Bruce Willis"}),
            node({"name": "John McClane"}),
            node({"name": "Alan Rickman"}),
            node({"name": "Hans Gruber"}),
            node({"name": "Nakatomi Plaza"}),
            rel(0, "PLAYS", 1),
            rel(2, "PLAYS", 3),
            rel(1, "VISITS", 4),
            rel(3, "STEALS_FROM", 4),
            rel(1, "KILLS", 3),)
        pprint(die_hard)
    
    insert_data()