TinkerPop 3: Gremlin query to group count by edge direction - gremlin

I have a vertex id to start with and wanted to get counts of in and out edges.
g.traversal().V().has("__id", "1234").groupCount().by(Direction.BOTH)
As .by() step wont accept Direction type., is there any alternative way?

You can do this nicely with project step:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has('name','marko').
project('out','in').
by(outE().count()).
by(inE().count())
==>[out:3,in:0]
For TinkerPop 3.0.x, which didn't have project you could do:
gremlin> g.V().has('name','marko').as('out','in').
select('out','in').
by(outE().count()).
by(inE().count())
==>[out:3,in:0]

Related

Why can't I drop vertex properties?

I can't drop any properties, they're just stubbornly remaining. What am I doing wrong?
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V().properties('prop').drop()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V().properties('prop').drop().iterate()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V(28712).properties('prop').drop()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
gremlin> g.V(28712).properties('prop').drop().iterate()
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}
What's doubly strange is that it seems to show the drop was successful (there appears to be no vertices with the property prop), but calling valueMap on a specific vertex shows otherwise.
gremlin> g.V().properties('prop').drop()
gremlin> g.V().has('prop').count()
==>0
gremlin> g.V().has('prop', 0).count()
==>0
gremlin> g.V(28712).valueMap()
==>{entity_id=[256a631b-3c19-49b7-84f3-01911d66c744], prop=[0]}

How can I use until in janusgraph?

gremlin> a = graph.addVertex("name", "alice")
gremlin> b = graph.addVertex("name", "bobby")
gremlin> c = graph.addVertex("name", "cindy")
gremlin> d = graph.addVertex("name", "david")
gremlin> e = graph.addVertex("name", "eliza")
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
gremlin> e.addEdge("rates",a,"tag","java","value",9)
g.V().has('name', 'alice').repeat(out()).times(6).cyclicPath().path().by('name')
I want to end with alice node. and I want to repeat all the step not want to specify time as 6. The requirement is I want to get all the loop from alice or get all the loops from the graph.
You can refer to the Cycle Detection section in the TinkerPop Recipes - it adapts fairly easily to your sample graph:
gremlin> g.V().has('name', 'alice').as('a').
......1> repeat(out().simplePath()).
......2> emit(loops().is(gt(1))).
......3> both().where(eq('a')).
......4> path().
......5> by('name').
......6> dedup().
......7> by(unfold().order().dedup().fold())
==>[alice,bobby,cindy,david,eliza,alice]

Gremlin CSV parse creating extra vertices

My code should read the 4 columns, split them into vertices for the first 2 columns, and edge properties for the last two columns.The CSV file has 33 unique vertices in 37 lines of data. What I don't understand is why I get 74 vertices instead and 37 edges. Interestingly enough, if I omit the addE statment I just get 37 vertices.
Obviously the property portion hasn't been included as I've been trying to resolve my current issues.
1\t2\tstep\tcmp
2\t3\tconductor\tna
3\t4\tswitch\tZ300
\t for tab
etc.
My code is:
graph = TinkerGraph.open()
graph.createIndex('myId', Vertex.class)
g = graph.traversal()
getOrCreate = { myid ->
p = g.V('myId', myid)
if (!p.hasNext())
{g.addV('connector').property('myId',myid) }
else
{p.next()}
}
new File('Continuity.txt').eachLine {
if (!it.startsWith("#")){
def row = it .split('\t')
def fromVertex = getOrCreate(row[0])
def toVertex = getOrCreate(row[1])
g.addE("connection").from(fromVertex).to(toVertex).iterate()
}
}
There's at least on problem in the code that I see. In this line:
p = g.V('myId', myid)
you are telling gremlin to find vertices with ids "myId" and whatever the value of the variable myid is. You instead want:
p = g.V().has('myId', myid)
The syntax you were using is from TinkerPop 2.x. I tested your code this way with some other changes and it seems to work properly now:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.createIndex('myId', Vertex.class)
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> getOrCreate = { myid ->
......1> if (!g.V().has('myId', myid).hasNext())
......2> g.addV('connector').property('myId',myid)
......3> else
......4> g.V().has('myId', myid)
......5> }
==>groovysh_evaluate$_run_closure1#29d37757
gremlin> g.addE('connection').from(getOrCreate(1)).to(getOrCreate(2)).iterate()
gremlin> g.addE('connection').from(getOrCreate(1)).to(getOrCreate(2)).iterate()
gremlin> g.V()
==>v[0]
==>v[2]
gremlin> g.E()
==>e[4][2-connection->0]
==>e[5][2-connection->0]

how can we get 2 vertexes path?

Now I have the query below and I would like to get all the edge and It is interesting why we got 2 same path and I want to get the path detail. How can I implement?
Vertex fromNode = g.V().has('name', 'alice').next();Vertex toNode = g.V().has('name', 'bobby').next();g.V(fromNode).repeat(both().simplePath()).until(is(toNode)).path()
==>[v[4224],v[40964296]]
==>[v[4224],v[40964296]]
==>[v[4224],v[4144],v[40964256],v[4096],v[40964296]]
We have the Graph below.
gremlin> a = graph.addVertex("name", "alice")
==>v[4208]
gremlin> b = graph.addVertex("name", "bobby")
==>v[40968424]
gremlin> c = graph.addVertex("name", "cindy")
==>v[4192]
gremlin> d = graph.addVertex("name", "david")
==>v[40972520]
gremlin> e = graph.addVertex("name", "eliza")
==>v[40964272]
gremlin> a.addEdge("rates",b,"tag","ruby","value",9)
==>e[2ry-38w-azv9-oe3fs][4208-rates->40968424]
gremlin> b.addEdge("rates",c,"tag","ruby","value",8)
==>e[odzq5-oe3fs-azv9-38g][40968424-rates->4192]
gremlin> c.addEdge("rates",d,"tag","ruby","value",7)
==>e[170-38g-azv9-oe6lk][4192-rates->40972520]
gremlin> d.addEdge("rates",e,"tag","ruby","value",6)
==>e[oe04d-oe6lk-azv9-oe08g][40972520-rates->40964272]
gremlin> a.addEdge("rates",e,"tag","java","value",9)
==>e[366-38w-azv9-oe08g][4208-rates->40964272]
gremlin> g.E().values("tag")
==>ruby
==>ruby
==>ruby
==>ruby
==>java
gremlin> graph.tx().commit()
I would like to get the path detail like below:
==>bobby=[v[0], e[10][0-rates->2], v[2]]
==>cindy=[v[0], e[10][0-rates->2], v[2], e[11][2-rates->4], v[4]]
==>david=[v[0], e[10][0-rates->2], v[2], e[11][2-rates->4], v[4], e[12][4-rates->6], v[6]]
You just need to specify that you want the edges as well by traversing over them explicitly:
gremlin> g.V(fromNode).repeat(bothE().otherV().simplePath()).until(is(toNode)).path()
==>[v[0],e[10][0-rates->2],v[2]]
==>[v[0],e[14][0-rates->8],v[8],e[13][6-rates->8],v[6],e[12][4-rates->6],v[4],e[11][2-rates->4],v[2]]

how to add a vertex only if it doesn't exist and continue this single traversal with other graph mutations?

Currently I have this gremlin/groovy code:
if(!g.V().has("Number","number","3").hasNext()) {
g.addV("Number").property("number","3")
}
Is it possible to have the same result without using multiple traversals?
I tried this and it doesn't work (it doesn't add either Number or User vertices)
g.V().choose(has("Number","number", "3"),
addV("Number").property("number", "3"),
has("Number","number", "3")
).as("number")
.addV("User").property("uuid","test uuuid")
.forEachRemaining(System.out::println);
I tried what it was suggested here (https://stackoverflow.com/a/33965737/986160) but it doesn't allow me to continue my single traversal with adding another user in a single transaction for DSE:
g.V()
.has("Number","number", "3")
.tryNext()
.orElseGet(
() -> g.addV("Number")
.property("number", "3").next()
);
Thanks!
Unfortunately we don't have g.coalesce() yet, but there's a workaround:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.inject(1).coalesce(V().has("Number", "number", 3), addV("Number").property("number", 3))
==>v[0]
gremlin> g.V().valueMap()
==>[number:[3]]

Resources