0

I need to figure out how to import CSV files using pre-defined indexed property keys, I tried this:

With a empty Titan graph this code works perfectly

new File("/home/User/titan-server-0.4.2/propnodes.csv").eachLine{ line -> (BaseRecId,StreetNumber,StreetName,StreetSuffix,City,County,State,Zip) = line.split(",");

prop = g.addVertex("BaseRecId::"+BaseRecId);

ElementHelper.setProperties(prop, ["BaseRecId" : BaseRecId,"StreetNumber": StreetNumber,"StreetName" : StreetName,"StreetSuffix" : StreetSuffix,"City" : City,"County": County,"State" : State,"Zip" : Zip])}

Now, due Titan indexes need to be setup before to populate any data, first step I do this:

g.makeKey('BaseRecId').dataType(Integer.class).indexed('search', Vertex.class).make(); g.makeKey('StreetName').dataType(String.class).indexed('search', Vertex.class).make();

and then when I try to import the data with the above code, and I got this error

An error occurred while processing the script for language [groovy]. All transactions across all graphs in the session have been concluded with failure: javax.script.ScriptException: javax.script.ScriptException: java.lang.NumberFormatException: For input string: "BaseRecId"

How can I import CSV files using existing external indexed property Keys?

Alejandro
  • 39
  • 7

1 Answers1

0

You defined BaseRecId as an integer but you are trying to push it in as a string. Your code should look something like this:

ElementHelper.setProperties(prop, ["BaseRecId" : Integer.parseInt(BaseRecId),"StreetNumber": StreetNumber,"StreetName" : StreetName,"StreetSuffix" : StreetSuffix,"City" : City,"County": County,"State" : State,"Zip" : Zip])}

Also g.addVertex will not respect that ID you are passing it. Titan does not allow for user supplied ids. Finally, you might consider using groovy-csv for working with CSV files in the Gremlin REPL.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thank you Stephen for the helpful approach, even when the problem still, I've changed **prop = g.addVertex("BaseRecId::"+BaseRecId)** to **prop = g.addVertex()** and I made a String type key BaseRecId, and it worked. I wonder why it doesn't work with integer, I've tried **"BaseRecId" : Integer.parseInt(BaseRecId)** and **'BaseRecId' : BaseRecId.toInteger()** with the same error above. – Alejandro Mar 06 '14 at 15:47
  • Glad you figured it out, however if you wanted to do numerical range queries, a String won't help you do that i don't think. Not sure why Integer doesn't work for. The "graph of the gods" example shows that it is possible: https://github.com/thinkaurelius/titan/blob/master/titan-core/src/main/java/com/thinkaurelius/titan/example/GraphOfTheGodsFactory.java#L52 – stephen mallette Mar 06 '14 at 16:15
  • Yeah, I've seem that, anyways, I'm moving forward. Thanks for your help! – Alejandro Mar 06 '14 at 16:22