2

I am running apoc.create.node against a .CSV file. I need the nodes that can be created to be created even if one or more nodes fails due to a duplicate key.

I run this:

CALL apoc.load.csv('FILE:///C:/Temp/Test/Test/Neo4jTest/import/Neo4j_AttributeProvenance.csv',{sep:","}) YIELD map  CALL apoc.create.node([map.NodeType], {Key:map.Key, AttributeName:map.AttributeName, TableName:map.TableName, SchemaName:map.SchemaName, DataType:map.DataType, PreviousKey:map.PreviousKey}) yield node return count(*)

and I get this

Neo4jUtils.ExecActionQuery().execute(): Failed to invoke procedure `apoc.create.node`: Caused by: org.neo4j.graphdb.ConstraintViolationException: Node(357) already exists with label `SubNode` and property `Key` = 'queryprocessingtest.ttablea.testDateTime'

The .CSV file looks like this

NodeType,Key,SchemaName,TableName,AttributeName,DataType,PreviousKey
RootNode,queryprocessingtest.q01.testDateTimeX,queryprocessingtest,q01,testDateTime,datetime,
SubNode,queryprocessingtest.ttablea.testDateTime,queryprocessingtest,ttablea,testDateTime,datetime,queryprocessingtest.q01.testDateTime
nicomp
  • 4,344
  • 4
  • 27
  • 60
  • 1
    Not sure that can be done in apoc.load.csv, but you can definitely do that if you use "plain" LOAD CSV. – Tom Geudens Aug 06 '17 at 10:25
  • @TomGeudens True, but if I do that then I can't do this: https://stackoverflow.com/questions/45166907/how-to-use-a-csv-field-to-define-the-node-label-in-a-load-statement , it's a catch-22. :) – nicomp Aug 06 '17 at 14:45

1 Answers1

2

I notice it is not shown in the generated documentation, but if you check dbms.procedures(), you'll see this is also possible :

CALL apoc.merge.node()

The signature looks the same as for create, so your statement then becomes :

CALL apoc.load.csv('FILE:///C:/Temp/Test/Test/Neo4jTest/import/Neo4j_AttributeProvenance.csv',{sep:","}) YIELD map  CALL apoc.merge.node([map.NodeType], {Key:map.Key, AttributeName:map.AttributeName, TableName:map.TableName, SchemaName:map.SchemaName, DataType:map.DataType, PreviousKey:map.PreviousKey}) yield node return count(*)

That should solve your problem.

Hope this helps.

Regards, Tom

Tom Geudens
  • 2,638
  • 9
  • 15
  • 1
    Nice! I already wrote Java to process a multi-line .CSV file into single-line CSVs , then submit the individual files to apoc.load.csv(). Next time I will use apoc.merge.node() -- and there will be a next time... – nicomp Aug 07 '17 at 10:27