4

I am starting to work with LOAD CSV of Cypher for Neo4J to import larger csv-files into my DB. I would like to add to each imported node a unique ID (uuid) as a property.

My try was:

LOAD CSV FROM "file:..." AS csvLine
CREATE (c:Customer { uuid: {uuid}, name: csvLine[0], code: csvLine[1]})

Unfortunately I receive for each node the same UUID (although its a function that would normally generate the UUID new when called), it looks like the UUID is generated 1 time and then attached to each node while creating the node and parsing the csv-file.

Is there a way to generate a new UUID for each imported csv-line to mark the node?

Thanks for your hints from Balael

smartcaveman
  • 38,142
  • 26
  • 119
  • 203
Balael
  • 381
  • 2
  • 15

2 Answers2

6

Not sure where you've seen that {uuid} is a function. It is just using whatever you pass in as an parameter "uuid".

You'd have to generate a uuid when creating your CSV. In cypher there is currently no uuid() function.

One workaround that you could do is:

LOAD CSV FROM "file:..." AS csvLine
CREATE (c:Customer { name: csvLine[0], code: csvLine[1]})
SET c.id = id(c)
Michael Hunger
  • 39,665
  • 3
  • 48
  • 74
  • you could also use tools like csvkit (http://csvkit.readthedocs.org/en/0.9.0/) to modify your csv before importing it to have an uuid. – Stefan Armbruster Nov 23 '14 at 12:39
  • Hi Michael, thanks for sheding some light here. Unfortunately there is no way to generate a UUID in the CSV as they come from a system I dont have access. I understood that UUID cannot generated each time when the node is created. Also the set-command is just repeating to add the same UUID to each node as even e.g. a uniqid() in PHP seems not be called more than once so only one UUID is assigned to every node - which is exactly the opposite of what I want to create - a unique identifier.Seems I need to run again after the CSV through each node that has been created and add a UUID node by node. – Balael Nov 23 '14 at 12:48
  • If you're looking for a way to loop through again and add a UUID without doing it one node at a time, maybe check out http://stackoverflow.com/questions/25952884/neo4j-assign-unique-values-to-all-nodes-matching-query. Might get you started. – subvertallchris Nov 23 '14 at 15:39
  • Hi Stefan, wow, thats a neat tool, thanks for sharing. I have meanwhile added a second loop into the code to stamp all imported nodes with a UUID - not beautiful but working. I will defintly have a look at csvkit! – Balael Nov 23 '14 at 19:33
  • Thanks Chris, I will dig into the thread and se eif I can adapt the idea to my needs. – Balael Nov 23 '14 at 19:34
4

You could also use the GraphAware UUID Module.

All you would have to do is drop the framework jar and the uuid module jar into the plugins directory, add the following 2 lines into neo4j.properties, and restart Neo4j.

com.graphaware.runtime.enabled=true
com.graphaware.module.UIDM.1=com.graphaware.module.uuid.UuidBootstrapper

Any new node (no matter how it is created) will automatically get a UUID.

Michal Bachman
  • 2,621
  • 15
  • 22
  • Hey Michal, thanks for your response, this works well. Do you know some resources on how one would build this from source? I am not familiar with the Java build ecosystem. – bryanph Jul 19 '16 at 11:55
  • After cloning the project from GIT ( git clone https://github.com/graphaware/neo4j-uuid.git ), install the Java build tool named Maven. Depending on your environment macosx : "brew install maven", ubuntu/debian & co : "apt-get install maven". Then you can "cd neo4j-uuid" then "mvn clean install" from the command line. The compiled build will reside in the "target/" directory of the project you cloned. PS. This may take a long time as all unit tests will be launched and all project dependencies will be downloaded. – Sebastien Nov 13 '16 at 20:59