Execute Gremlin queries on Neo4j Graph in Java

2,004 views
Skip to first unread message

Neo

unread,
Jun 5, 2012, 10:52:46 AM6/5/12
to ne...@googlegroups.com
Hello,
     I am trying to execute gremlin queries(that I can execute on console) in Java directly(not through REST-API) similar to executing Cypher queries. Some documentation is given at which gives following instructions
1 ScriptEngineManager manager = new ScriptEngineManager();
2 ScriptEngine engine = manager.getEngineByName("gremlin-groovy");
3 // or, instead of above, simply do:
4 //       ScriptEngine engine = new GremlinGroovyScriptEngineFactory().getScriptEngine();
5 // or
6 //       ScriptEngine engine = new GremlinGroovyScriptEngine();
7 Graph graph = TinkerGraphFactory.createTinkerGraph();
8 List results = new ArrayList();
...
9 engine.eval("v.out('knows'){it.name.equals(name)}.fill(results)");
10 String script = "i = 0\n" +
                "for(x in 1..10)\n" +
                "  i++";
11 engine.eval(script); 
In line number 7 I have to give a reference to my graphdb, I have tried following 

Graph graph = new Neo4jGraph($PATH); 

But I am not able to query anything on the graph. How can I give the reference to my graphdb to Execution Engine?
I tried exploring further, but still after one full day, I can not execute simple queries on my graphdb . Please tell any other alternative than this if any.

Thanks

Peter Neubauer

unread,
Jun 5, 2012, 11:35:54 AM6/5/12
to ne...@googlegroups.com
Hi there,
you put the reference to any bound variables in a map and give it to
the engine for evaluation, look at the Gremlin plugin for reference,

https://github.com/neo4j/gremlin-plugin/blob/master/src/main/java/org/neo4j/server/plugin/gremlin/GremlinPlugin.java#L81

Cheers,

/peter neubauer

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

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Neo

unread,
Jun 5, 2012, 11:46:52 AM6/5/12
to ne...@googlegroups.com
Hey Peter,
Thanks for the reply. Could you please kindly give a very simple sample code to run queries on a Neo4j graphdb (not based on gremlin plug-in for REST API). It's very difficult for me to comprehend from the link you have give.

Marko Rodriguez

unread,
Jun 5, 2012, 12:33:17 PM6/5/12
to ne...@googlegroups.com
Hi,

There is an example here:


See the last section entitled:
Using JSR 223 GremlinGroovyScriptEngine

Where instead of 

graph = TinkerGraphFactory.createTinkerGraph();

do
graph = new Neo4jGraph(...);

Good luck,
Marko.

Neo

unread,
Jun 6, 2012, 3:12:06 AM6/6/12
to ne...@googlegroups.com
Hey thanks Mark, I have tried according to your instructions with following code.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import com.tinkerpop.blueprints.pgm.Graph;
import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;

public class query_caller{
    public static void main(String[] args){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("gremlin-groovy");
        Graph graph = new Neo4jGraph("E:/Projects/Neo4j/neo4j-community-1.7/data/graph.db");
        
    }
}


But I got following error.

Exception in thread "main" org.neo4j.graphdb.TransactionFailureException: Could not create data source [nioneodb], see nested exception for cause of error
    at org.neo4j.kernel.impl.transaction.TxModule.registerDataSource(TxModule.java:153)
    at org.neo4j.kernel.GraphDbInstance.start(GraphDbInstance.java:111)
    at org.neo4j.kernel.EmbeddedGraphDbImpl.<init>(EmbeddedGraphDbImpl.java:190)
    at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:80)
    at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:64)
    at com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph.<init>(Neo4jGraph.java:71)
    at com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph.<init>(Neo4jGraph.java:62)
    at query_caller.main(query_caller.java:12)
Caused by: org.neo4j.kernel.impl.transaction.xaframework.IllegalLogFormatException: Invalid log format version found, expected 1 but was 2. To be able to upgrade from an older log format version there must have been a clean shutdown of the database
    at org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.readLogHeader(LogIoUtils.java:54)
    at org.neo4j.kernel.impl.transaction.xaframework.XaLogicalLog.readLogHeader(XaLogicalLog.java:1213)
    at org.neo4j.kernel.impl.transaction.xaframework.XaLogicalLog.doInternalRecovery(XaLogicalLog.java:742)
    at org.neo4j.kernel.impl.transaction.xaframework.XaLogicalLog.open(XaLogicalLog.java:230)
    at org.neo4j.kernel.impl.transaction.xaframework.XaLogicalLog.open(XaLogicalLog.java:184)
    at org.neo4j.kernel.impl.transaction.xaframework.XaContainer.openLogicalLog(XaContainer.java:97)
    at org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource.<init>(NeoStoreXaDataSource.java:143)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.neo4j.kernel.impl.transaction.XaDataSourceManager.create(XaDataSourceManager.java:75)
    at org.neo4j.kernel.impl.transaction.TxModule.registerDataSource(TxModule.java:147)
    ... 7 more

Neo

unread,
Jun 6, 2012, 8:20:16 AM6/6/12
to ne...@googlegroups.com
Resolved!! Thanks for the help Mark.

Spandana Otra

unread,
Feb 5, 2017, 8:39:34 PM2/5/17
to Neo4j
How to execute through engine gremlin queries on neo4j database in java? 
 

Michael Hunger

unread,
Feb 6, 2017, 7:13:42 AM2/6/17
to ne...@googlegroups.com
See the gremlin docs for running gremlin on top of Neo4j.

You might also want to have a look at the github.com/neo4j-contrib/neo4j-tinkerpop-api-impl
Which also comes with a procedure to run Gremlin from Cypher, the procedure implementation should help you in getting started in Java.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages