2

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

Kamal Verma
  • 115
  • 1
  • 1
  • 10

2 Answers2

2

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}
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
1

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

Akshaya
  • 21
  • 3
  • Hi Akshaya, Thanks alot for your reply. – Kamal Verma Jan 05 '16 at 09:10
  • Hi Akshaya, Thanks alot for your reply. But this is not solving my requirement. As this parametrization is working for 'g.v(start).map' but if I change it to 'g.v(1).map' its not working and also parametrization for queries like 'g=rexster.getGraph(\"geograph\");g.V.map('1','2','3')' is not working(here I want to replace 1,2 & 3 with corresponding propertyKey). Thanks, – Kamal Verma Jan 05 '16 at 09:16
  • You could pass a `List` as an argument, but I think you would then have to manually convert that in your script to an array for the call to `map()`. I have a feeling that passing a `String` array will simply convert it to `List` so either way you would have to do the conversion (but perhaps it is worth a try). – stephen mallette Jan 05 '16 at 11:25
  • Hi Stephen, I am sorry but I didnt understood your solution. I have used following code: String script = "g=rexster.getGraph(\"geograph\");g.V.map('1','2','3')"; HashMap hashMap = new HashMap<>(); hashMap.put("1", propertyKey1); hashMap.put("2", propertyKey2); hashMap.put("3", propertyKey3); List> results = getClient().execute(script, hashMap); but 1,2,3 are not getting replaced by the values – Kamal Verma Jan 06 '16 at 06:06
  • not sure what error you're getting, but i don't think that will work. you can't use numeric values for variables. replace "1", "2" and "3" with "a", "b", "c" and i would expect that to work – stephen mallette Jan 06 '16 at 19:38