Platinum Partner
java,architects,nosql,tool,neo4j

More on Neo4j and Michael Hunger's Batch Importer

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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}