понедельник, 17 сентября 2012 г.

Create neo4j plugin

We use neo4j for transport db storage and for find routes. Find routes process is large task, and so we want find routes via server plugin.

It's some step to create plugin for neo4j-server:
1) The plugin class must like as follows:

 * License information

// package name - for keeping structure and your convenience
package ru.companyName.neo4j.plugins;

// import classes from neo4j
import org.neo4j.graphdb.GraphDatabaseService;

// import standard java classes
import java.util.ArrayList;

// decorator description of plugin (plugin class)
@Description( "An extension to the Neo4j Server for find routes between two nodes" )
// your plugin class must extension ServerPlugin class
public class MyPlugin extends ServerPlugin {
// follow - standard structure of the class (there is implementation of your algorithms)
Set<String> trueTypes;

// create class method
public TransportRouter() {
this.trueTypes = new HashSet<String>(Arrays.asList("type1", "type2"));

// http-query method
@Name( "get_paths" )
@Description( "Get paths from node to node" )
// from this class you will call your plugin class
@PluginTarget( GraphDatabaseService.class )
public Representation getPaths(
@Source GraphDatabaseService graphDb,
     @Description( "The id of the node from" )
                @Parameter( name = "id_from" ) Node nodeFrom,
     @Description( "The id of the node to" )
                @Parameter( name = "id_to" ) Node nodeTo
                ) throws Exception
                // implementation of your algorithms
                // returnable type Representation allow type String (in it we put string in json; also plugin method can return Iterable<Node> and Iterable<Relationship>)
return ValueRepresentation.string(result.toString());

2) Then plugin class must be compiled (Eclipse, javac, etc.), and then all compiled classes (*.class) put into JAR.
Directories tree in JAR-archive must like as name of package:
/ru/companyName/neo4j/plugins/<there are compiled classes>
JAR also must contain file:
In this file you must write name of your package: ru.companyName.neo4j.plugins.TransportRouter

3) put resulting JAR in folder .../neo4j-server/plugins
4) restart server
5) get plugin list in server (in bash):
curl http://localhost:7474/db/data/ext/
6) Then we can query our plugin class method:
curl -X POST http://localhost:7474/db/data/ext/MyPlugin/graphdb/get_path -H "Content-Type: application/json" -d '{"id_from": "http://localhost:7474/db/data/node/1", "id_to": "http://localhost:7474/db/data/node/2"}'
In this string after plugin name ("MyPlugin") we write "graphdb", because in decorator @PluginTarget( GraphDatabaseService.class ) we difine this class as parent class. Parameter -d define dictionary (Map), which consider all input parameters for our plugin method (which define via @Parameter).

There is problem in process of test plugin: DB wasn't see my plugin. This problem may occur by some reason (if your compilation classes was access):
- there isn't file /META-INF/services/org.neo4j.server.plugins.ServerPlugin or wrong string in it;
- there is error while initialization plugin class. You can try to create you class from other application to see this errors.
If your plugin was contained in the list of plugins, you may try query class method (from bash). And you will see all errors in the tail.

Комментариев нет:

Отправить комментарий