Batch Importer – Part 3

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.

Tagged , , , ,

6 thoughts on “Batch Importer – Part 3

  1. […] If you recall, I’ve had three blog posts about the Batch Importer. In the first one, I showed you how to install the Batch Importer, in the second one, I showed you how to use data in your relational database to generate the csv files to create your graph, and just recently I showed you how to quickly index your data. […]

  2. […] Batch Importer – Part 3 [Neo4j] by Max De Marzi. […]

  3. […] needed to import this into Neo4j. I followed the examples from Batch Importer Part 2, and Batch Importer Part 3 to do some ETL, but first I needed to load the data into Postgresql so I could match up the two […]

  4. amergin says:

    Batch importer is great but I’m having some issues getting the right type casting for edge properties (https://github.com/jexp/batch-import/issues/6). Any ideas what might be going wrong? I saw Michael saying somewhere that the importer script just skips the missing property values but I now suspect it might be affecting the typecasting. Would be great to have this resolved since currently I cannot do any queries in Cypher that use comparison operators for edge properties.

    Furthermore, do you think the importer would at some point support numerical properties in indices instead of just strings? This would allow one to do range queries using Lucene (http://docs.neo4j.org/chunked/stable/indexing-lucene-extras.html#indexing-lucene-numeric-ranges).

  5. […] Loading into Neo4J Using the BatchInserter framework from Max De Marzi and Michael Hunger, the data can be quickly loaded into Neo4J (less than 5 […]

  6. […] we are now ready to import it into Neo4j using the Batch Importer. Much has changed since my last blog post about the batch importer. Michael Hunger has made our life easier by allowing us to specify a way to look up nodes by an […]

Leave a comment