DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. More on Neo4j and Michael Hunger's Batch Importer

More on Neo4j and Michael Hunger's Batch Importer

Max De Marzi user avatar by
Max De Marzi
·
Jul. 03, 12 · Interview
Like (0)
Save
Tweet
Share
6.40K Views

Join the DZone community and get the full member experience.

Join For Free

at the end of february, we took a look at michael hunger’s batch importer . it is a great tool to load millions of nodes and relationships into neo4j quickly. the only thing it was missing was indexing… i say was, because i just submitted a pull request to add this feature. let’s go through how it was done so you get an idea of what the neo4j batch import api looks like, and in the next blog post i’ll show you how to generate data to take advantage of it.

first thing i had to do was update the pom.xml file to include the latest version of neo4j, and the lucene index dependency.

<dependency>
            <groupid>org.neo4j</groupid>
            <artifactid>neo4j-kernel</artifactid>
            <version>1.8.m05</version>
        </dependency>
        <dependency>
            <groupid>org.neo4j</groupid>
            <artifactid>neo4j-lucene-index</artifactid>
            <version>1.8.m05</version>
        </dependency>

the batch insert functionality has been moved to org.neo4j.unsafe to remind you that this should be used on first time loading only.

import org.neo4j.unsafe.batchinsert.batchinserter;
import org.neo4j.unsafe.batchinsert.batchinserters;
import org.neo4j.unsafe.batchinsert.batchinserterimpl;
import org.neo4j.unsafe.batchinsert.batchinserterindexprovider;
import org.neo4j.unsafe.batchinsert.batchinserterindex;
import org.neo4j.unsafe.batchinsert.lucenebatchinserterindexprovider;

we create a lucenebatchinserterindexprovider called lucene from the db:

db = batchinserters.inserter(graphdb.getabsolutepath(), config);
lucene = new lucenebatchinserterindexprovider(db);

let’s take a look at the importnodeindexes method:

private void importnodeindexes(file file, string indexname, string indextype) throws ioexception {
    	batchinserterindex index;
    	if (indextype.equals("fulltext")) {
    		index = lucene.nodeindex( indexname, stringmap( "type", "fulltext" ) );
    	} else {
    		index = lucene.nodeindex( indexname, exact_config );
    	}
        
        bufferedreader bf = new bufferedreader(new filereader(file));
        
        final data data = new data(bf.readline(), "\t", 1);
        object[] node = new object[1];
        string line;
        report.reset();
        while ((line = bf.readline()) != null) {        
            final map<string, object> properties = map(data.update(line, node));
            index.add(id(node[0]), properties);
            report.dots();
        }
                
        report.finishimport("nodes into " + indexname + " index");
    }

we pass in an indextype variable to decide if this index will be exact or fulltext. we create the index with lucene.nodeindex. we then read the values to be added to our index from the file passed in. once we capture the key and values from our file, we simply pass them along with our node id to the add method.

let’s take a look at importrelationshipindexes:

private void importrelationshipindexes(file file, string indexname, string indextype) throws ioexception {
    	batchinserterindex index;
    	if (indextype.equals("fulltext")) {
    		index = lucene.relationshipindex( indexname, stringmap( "type", "fulltext" ) );
    	} else {
    		index = lucene.relationshipindex( indexname, exact_config );
    	}

        bufferedreader bf = new bufferedreader(new filereader(file));
        
        final data data = new data(bf.readline(), "\t", 1);
        object[] rel = new object[1];
        string line;
        report.reset();
        while ((line = bf.readline()) != null) {        
            final map<string, object> properties = map(data.update(line, rel));
            index.add(id(rel[0]), properties);
            report.dots();
        }
                
        report.finishimport("relationships into " + indexname + " index");

    }

it’s practically identical but we are creating relationship indexes instead of node indexes. finally we need to shutdown lucene as well as our graph.

    private void finish() {
        lucene.shutdown();
        db.shutdown();
        report.finish();
    }

you can take a look at the full source code at https://github.com/maxdemarzi/batch-import .

to use it, we will add four arguments per each index to the command line:

to create a full text node index called users using nodes_index.csv:

node_index users fulltext nodes_index.csv 

to create an exact relationship index called worked using rels_index.csv:

rel_index worked exact rels_index.csv

example command line:

java -server -xmx4g -jar ../batch-import/target/batch-import-jar-with-dependencies.jar neo4j/data/graph.db nodes.csv rels.csv node_index users fulltext nodes_index.csv rel_index worked exact rels_index.csv

we expect nodes_index.csv to look like:

id	name	language
1	victor richards	west frisian
2	virginia shaw	korean
3	lois simpson	belarusian
4	randy bishop	hiri motu
5	lori mendoza	tok pisin

we expect rels_index.csv to look like:

id	property1	property2
0	cwqbnxrv	rpyqdwhk
1	qthnrret	tzjmmhta
2	dtztaqpy	pbmcdqyc

the id columns refers to the node or relationship id. the headers are the index keys, and the values on each row is what will be added to the index for that key.

follow me on twitter at @maxdemarzi and you’ll know when the next blog post which takes advantage of these features is published.


Neo4j Importer (computing)

Published at DZone with permission of Max De Marzi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Visual Network Mapping Your K8s Clusters To Assess Performance
  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How
  • Type Variance in Java and Kotlin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: