What's wrong with this code? It throws NoSuchElementException when I try to add an Edge?


刑天 <gaoxtw...@...>
 




package com.sankuai.kg;


import java.io.File;
import java.util.Iterator;


import org.apache.commons.io.FileUtils;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;


public class Loader {


   
public static void main(String[] args) throws Exception {
       
Graph graph = EmptyGraph.instance();
       
GraphTraversalSource g = graph.traversal().withRemote("remote-graph.properties");
       
Iterator<String> lineIt = FileUtils.lineIterator(new File(args[0]));
       
while (lineIt.hasNext()) {
           
String line = lineIt.next();
           
String[] cols = line.split(",");
           
           
GraphTraversal<Vertex, Vertex> t1 = g.V().has("poiId", cols[0]);
           
GraphTraversal<Vertex, Vertex> t2 = g.V().has("poiId", cols[3]);


           
if (!t1.hasNext())
                g
.addV().property("poiId", cols[0]).property("name", cols[1]).property("type", cols[2]).next();
           
if (!t2.hasNext())
                g
.addV().property(String.format("propId", cols[5]), cols[3]).property("name", cols[4])
                       
.property("type", cols[5]).next();
            g
.V().has("poiId", cols[0]).as("a").V().has("propId", cols[3]).as("b").addE(cols[6])
                   
.from("a").to("b").next();
           
       
}


        g
.close();
   
}


}



Jason Plurad <plu...@...>
 

Double check your usage of "propId" for the "b" vertex:

Vertex creation:

g.addV().property(String.format("propId", cols[5]), cols[3])

Traversal:

V().has("propId", cols[3])



On Sunday, August 20, 2017 at 2:41:59 PM UTC-4, 刑天 wrote:



package com.sankuai.kg;


import java.io.File;
import java.util.Iterator;


import org.apache.commons.io.FileUtils;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;


public class Loader {


   
public static void main(String[] args) throws Exception {
       
Graph graph = EmptyGraph.instance();
       
GraphTraversalSource g = graph.traversal().withRemote("remote-graph.properties");
       
Iterator<String> lineIt = FileUtils.lineIterator(new File(args[0]));
       
while (lineIt.hasNext()) {
           
String line = lineIt.next();
           
String[] cols = line.split(",");
           
           
GraphTraversal<Vertex, Vertex> t1 = g.V().has("poiId", cols[0]);
           
GraphTraversal<Vertex, Vertex> t2 = g.V().has("poiId", cols[3]);


           
if (!t1.hasNext())
                g
.addV().property("poiId", cols[0]).property("name", cols[1]).property("type", cols[2]).next();
           
if (!t2.hasNext())
                g
.addV().property(String.format("propId", cols[5]), cols[3]).property("name", cols[4])
                       
.property("type", cols[5]).next();
            g
.V().has("poiId", cols[0]).as("a").V().has("propId", cols[3]).as("b").addE(cols[6])
                   
.from("a").to("b").next();
           
       
}


        g
.close();
   
}


}