Gremlin CSV parse creating extra vertices - gremlin

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]

Related

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]

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]]

TinkerPop 3: Gremlin query to group count by edge direction

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]

How do you find all vertices that have no incoming edges?

Below I'm trying to find all vertices where there are no incoming edges using a filter on the vertices. fullyQualifiedName is a unique index. I noticed some vertices that appeared to have incoming edges so I added a step below to just print them out if they existed. I would have expected no output since I thought I had filtered these vertices above; however, I'm still seeing incoming edges displayed.
def g = BerkeleyGraphFactory.create()
def vertices = g.V.filter {
it.inE('depends').count() == 0
}
Set<String> u = []
u.addAll(vertices.collect {v->
v.fullyQualifiedName
})
u.each {
def focusIter = g.V('fullyQualifiedName', it)
def vertex = focusIter.next()
// this shouldn't print out anything since these vertices were filtered above
vertex.inE('depends').each { e->
def classRefV = e.outV.next()
println it + " is used by " + classRefV.name + " " + e.toString()
}
}
I can't seem to recreate your problem. A rough simplification of your code here seems to show that things work as expected:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> ids = g.V.filter{!it.inE('knows').hasNext()}.id.toList()
==>1
==>3
==>5
==>6
gremlin> ids.collect{g.v(it).inE('knows').toList()}
==>[]
==>[]
==>[]
==>[]
Perhaps you can try to convert your code to match the approach I took to see if that helps? I'm not sure what else to say short of you providing some sample data to work with for your specific case where the problem can be recreated.

Resources