Return an iterable of JSONObject from server plugin

127 views
Skip to first unread message

Anas

unread,
May 15, 2013, 9:24:45 AM5/15/13
to ne...@googlegroups.com
Hello, 

i am attempting to create a server plugin and i want it to return 2 iterables of Node.

Since this functionality doesn't seem to exist according to the doc, i took my 2 lists and created an iterable of JSONObject. 

So my question is : is it possible to return an Interable<JSONObject> from the server plugin ?? or to return JSON in general 

i tried this latter solution but it doesn't work .

Any help would be very appreciated.

Thanks 

Peter Neubauer

unread,
May 15, 2013, 9:39:08 AM5/15/13
to Neo4j User
Anas,
if you want to return JSON as according to the REST API, return a
ListRepresentation, as according to the other post. For generic
free-form JSON of your choice, use a ServerExtention,
http://docs.neo4j.org/chunked/snapshot/server-unmanaged-extensions.html

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

The authoritative book on graph databases - http://graphdatabases.com
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j
> --
> You received this message because you are subscribed to the Google Groups
> "Neo4j" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to neo4j+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Anas Zakki

unread,
May 15, 2013, 9:49:25 AM5/15/13
to ne...@googlegroups.com
Hello Peter,

Thanks for the answer. 

I was trying to return a List of org.neo4j.shell.util.json.JSONObject in my server plugin that i created from the 2 lists.

As you said, i found a post that suggests to use ListRepresentation, but i could'nt find how to use actually (even on the server plugin doc on neo4j website) ! 






You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/AyxEMtXIDE0/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.

Peter Neubauer

unread,
May 15, 2013, 9:51:44 AM5/15/13
to Neo4j User
Anas,
look at https://github.com/neo4j-contrib/gremlin-plugin/blob/master/src/main/java/org/neo4j/server/rest/repr/GremlinObjectToRepresentationConverter.java
for usage to convert arbitrary objects to Representations :)

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

The authoritative book on graph databases - http://graphdatabases.com
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j


Anas Zakki

unread,
May 15, 2013, 10:01:46 AM5/15/13
to ne...@googlegroups.com
going to see this right now ! 

Anas Zakki

unread,
May 15, 2013, 10:33:21 AM5/15/13
to ne...@googlegroups.com
Peter,

Before i dive into this, could you please confirm me that it is not possible to return an iterable of JSONObject (like an iterable of Node )  ?
otherwise, the task should be easier for me :)

Thanks 

Anas Zakki

unread,
May 15, 2013, 11:11:15 AM5/15/13
to ne...@googlegroups.com
here https://github.com/neo4j-contrib/gremlin-plugin/blob/master/src/main/java/org/neo4j/server/rest/repr/GremlinObjectToRepresentationConverter.java at the line 99 
the method  "protected Representation underlyingObjectToObject( Object value ) " does not exist.
i am using neo4j 1.9.RC2 and this method was deleted in this version ( v1.5 : http://api.neo4j.org/1.5/org/neo4j/helpers/collection/IterableWrapper.html

Peter Neubauer

unread,
May 17, 2013, 3:17:09 AM5/17/13
to Neo4j User
Anas,
it's not deleted, just made protected now which means it's not in the
public Javadoc. It actually might be a good idea to even generate
javadoc for protected methods, but we want to keep the public info
very clean, and this not a superpublic API :)

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

The authoritative book on graph databases - http://graphdatabases.com
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j


Anas Zakki

unread,
May 21, 2013, 3:04:13 PM5/21/13
to ne...@googlegroups.com
Hi Peter, 

I have just seen your answer.

Actually, my main objective wasn't to return a JSONObject but return two lists of Node.

I ended up merging those two lists before returning a unique list, and then in my code, i manage to split it and thus get my two lists :) 

Thanks for your help !!

Peter Neubauer

unread,
May 21, 2013, 3:06:47 PM5/21/13
to Neo4j User
Nice!

Feel free to make a short blog out of your approach in order to help others!

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.9 GA is out -
http://blog.neo4j.org/2013/05/neo4j-19-general-availability.html
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j


Anas Zakki

unread,
May 21, 2013, 4:51:23 PM5/21/13
to ne...@googlegroups.com
Here is the code i've used (not exactly what i was asking for, but it solved my problem )
I have posted my question on SO as well , here is the link : http://stackoverflow.com/questions/16586369/return-jsonobject-from-server-plugin-in-neo4j

Hope it will help !! :) 

public class Ond extends ServerPlugin {

@PluginTarget(GraphDatabaseService.class)
public static Iterable<Node> getOnd(
        @Source GraphDatabaseService graphDb,
        @Description("the airline's node ID") @Parameter(name = "id") int id) {

    List<Node> results= new ArrayList<Node>();

    String n4jQuery= "START al= node("+id+") match ond-[:operatedBy]->al, ond-[:origin]->orig, ond-[:destination]->dest RETURN orig, dest ;"; 

    ExecutionEngine engine= new ExecutionEngine(graphDb);
    ExecutionResult result= engine.execute(n4jQuery);
    List<Node> orig= new ArrayList<Node>();
    List<Node> dest= new ArrayList<Node>();

    //creating the lists i want to return
    //an outter loop over table's rows
    for (Map<String, Object> row : result) {
    //an inner loop over the two columns : orig and dest
        for (Map.Entry<String, Object> column : row.entrySet()) {

            String key = column.getKey();
            Node n = (Node) column.getValue();
            if(key.equals("dest")){
                dest.add(n);
            }else{
                orig.add(n);
            }
        }

    }

      //merging the two lists
    results.addAll(orig);
    results.addAll(dest);

      // orig elements are between indices 0 and size(results)/2 -1 
      //and dest element are between size(results)/2 and size(results)-1
    return results;
}

Peter Neubauer

unread,
May 21, 2013, 5:55:12 PM5/21/13
to Neo4j User
Nice Anas!

Also, you might want to instantiate the ExecutionEngine not at every
call but only once.

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Neo4j 1.9 GA is out -
http://blog.neo4j.org/2013/05/neo4j-19-general-availability.html
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j


Anas Zakki

unread,
May 23, 2013, 2:33:58 AM5/23/13
to ne...@googlegroups.com
Hi peter,

I think i can't do that since i'm packaging the class in a jar and using it as a server plugin ! 

Up to now, i haven't seen any issue related to performance or latency, so it's fine :) 

Michael Hunger

unread,
May 23, 2013, 5:19:57 AM5/23/13
to ne...@googlegroups.com
I would do it like this:

public class Ond extends ServerPlugin {

    final static String n4jQuery= "START al= node({id}) match ond-[:operatedBy]->al, ond-[:origin]->orig, ond-[:destination]->dest RETURN orig, dest ;"; 

    ExecutionEngine engine = null;

@PluginTarget(GraphDatabaseService.class)
public static Iterable<Node> getOnd(
        @Source GraphDatabaseService graphDb,
        @Description("the airline's node ID") @Parameter(name = "id") int id) {

    
    if (engine == null ) {
       engine = new ExecutionEngine(graphDb);
    }


    ExecutionResult result= engine.execute(n4jQuery, MapUtils.map("id",id));
    List<Node> orig= new ArrayList<Node>();
    List<Node> dest= new ArrayList<Node>();

    //an outer loop over table's rows
    for (Map<String, Object> row : result) {
        dest.add((Node)row.get("dest"));
        orig.add((Node)row.get("orig"));
    }

      //merging the two lists
    List<Node> results= new ArrayList<Node>(orig);
    results.addAll(dest);

      // orig elements are between indices 0 and size(results)/2 -1 
      //and dest element are between size(results)/2 and size(results)-1
    return results;
}

Anas Zakki

unread,
May 23, 2013, 5:31:25 AM5/23/13
to ne...@googlegroups.com
Thanks Michael !! 

it is more clean and optimized than what i was doing !! 

PS : could you please see another problem i faced, which is relative to processing parallel requests in neo4j ? here is the link : https://groups.google.com/forum/?fromgroups#!topic/neo4j/pL9nO6HgTa0

Thanks 
Reply all
Reply to author
Forward
0 new messages