More on Neo4j and Michael Hunger's Batch Importer
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.
Published at DZone with permission of Max De Marzi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Measuring Service Performance: The Whys and Hows
-
System Testing and Best Practices
-
How to Handle Secrets in Kubernetes
-
Integration Architecture Guiding Principles, A Reference
Comments