0

I am playing with titan db (cassandra and es backend). I am sending queries to gremlin server via gremlin-javascript module in nodejs. I have been able to write getOrInsert vertices in titan using graph.traversal().V().has(idKey,idVal).tryNext().orElseGet{ graph.addVertex(T.label,"product","product_id",991);graph.tx().commit();

But the problem I am facing is how should i add an edge between 2 vertices . Generally what we do in gremlin is

a=graph.traversal().addVertex(...some properties);
b=graph.traversal().addVertex(...some properties);
a.addEdge("someEdgeType",b);

I have discovered that there is grex module which is helpful as it supports

// JavaScript
var query = gremlin();
var bob = query.var(g.addVertex({ name: 'Bob' }));
var alice = query.var(g.addVertex({ name: 'Alice' }));
query(g.addEdge(bob, alice, 'likes', { since: 'now' }));

I am not able to use grex module(it supports 2.0) as I am using gremlin 3.0.1 and the format of writing queries have changed(I think!) .

My usecase : I should be able to send gremlin queries to remote gremlin server.

Is Any other grex like module present to make query creation easier? How should I create edges through querying gremlin server ?

  • 1
    Try https://www.npmjs.com/package/gremlin for JS with Titan 1.0/TinkerPop 3.0.1 – Jason Plurad May 03 '16 at 12:27
  • Thanks jason. Using the same module. when running this query ```g.V(245764248).next().addEdge("somerelation",g.V(245764248).next()).tx().commit();``` relation is created but not committed.Not able to figure out why. – palash kulshreshtha May 03 '16 at 12:59

1 Answers1

4

First, doing graph.traversal() is not a good pattern to follow. Create your GraphTraversalSource once and re-use it:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

As for your problem, your "getOrInsert" line of code returns a Vertex, so just use it to create your Edge:

gremlin> v1 = g.V().has('k',1).tryNext().orElseGet{graph.addVertex('k',1)}
==>v[0]
gremlin> v1.addEdge('self',v1)
==>e[2][0-self->0]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thanks Stephen.My graph is initialized in gremlin-server via gremlin-server.yaml.I am sending queries to gremlin server via javascript-gremlin module. It supports string queries. So each time I make a call I would have to call graph.traversal()... (if I am not missing anything here). – palash kulshreshtha May 03 '16 at 10:33
  • 1
    `g` or whatever you named your graph in your yaml configuration file should already be bound to Gremlin Server when it is initialized. you should not need to call `graph.traversal()` in your script. – stephen mallette May 03 '16 at 10:38
  • thanks,You are absolutely correct. I just found out that graph is bound to StandardTitanGraph and g is bound to graph.traversal(). In gremlin-server.yaml its graphs: {graph: conf/titan-cassandra-es.properties}. But part of my question still remains I have to do something like g.V(81924272).addEdge("buys",g.V(163844256)). where 81924272=customer vertex id & 163844256=product vertex id – palash kulshreshtha May 03 '16 at 10:50
  • so why not do that? i mean your syntax is a bit off, but it's perfectly fine to do: `g.V().has('customerId',81924272).next().addEdge("buys",g.V().has('productId',163844256).next())`. or just use a variable like i did above in my answer. – stephen mallette May 03 '16 at 10:57
  • thanks for showing the right direction. What I was trying to do was to use the internal vertex ids retrieved because I think it will be faster to access a vertex via internal vertex id (==>v[81924272]). Otherwise I would be searching a vertex twice first time in getOrCreate and second time in adding the edge.(an overhead i think). One more thing. any Idea about grex support for tinkerpop 3.0? – palash kulshreshtha May 03 '16 at 11:10
  • yes - the vertex id lookup would be faster. i don't know about the future of grex. you would have to contact the author. – stephen mallette May 03 '16 at 11:42
  • @palashkulshreshtha grex author/maintainer here: grex will most likely not be updated anymore. If you require an HTTP connection to TP3 Gremlin Server, I plan to add support for this in gremlin-javascript (see https://github.com/jbmusso/gremlin-javascript) – jbmusso May 03 '16 at 19:35
  • @jbmusso thanks . with your suggestion to my some previous question. I having been successfuly using gremlin-javascript along with TP3. Grex will definitely be missed. – palash kulshreshtha May 04 '16 at 05:47