How to parameterized Gremlin query in Java?

2.2k Views Asked by At

I am interested in parameterizing the Gremlin Query in Java code as we do in case of SQL query using PreparedStatement (example : statement.setString(int, String), statement.setInt(int, int)).

Kindly, please let me know can we do this kind of stuff in Java for Gremlin query.

Thanks in advance.

Regards, Kamal

2

There are 2 best solutions below

5
Akshaya On

Check this out at https://github.com/tinkerpop/rexster/wiki/RexPro-Java

When possible, parameterize Gremlin scripts, as this leads to better overall performance. The above example can be done as a parameterized request as follows:

RexsterClient client = RexsterClientFactory.open("localhost", "tinkergraph");
List<Map<String, Object>> results = client.execute("g.v(start).map", 
new HashMap<String, Object>(){{
    put("start", 1);
}});
Map<String, Object> map = results.get(0);
System.out.println(map.get("name"));

Akshaya

0
stephen mallette On

For those looking for an answer here for Titan 1.x and TinkerPop 3.x:

gremlin> cluster = Cluster.open()
==>localhost/127.0.0.1:8182
gremlin> client = cluster.connect()
==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient@412c995d
gremlin> client.submit("g.V(start).valueMap(m)",[start:1L, m:(['name','age'] as String[])]).all().get()
==>result{object={name=[marko], age=[29]} class=java.util.HashMap}