0
votes

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()  
   }
}
1

1 Answers

2
votes

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]