'How to execute conditional statements inside an apoc.periodic.iterate call in Neo4j Cypher
I currently have the following query:
CALL apoc.periodic.iterate(
"LOAD CSV WITH HEADERS FROM $url AS row
CASE row.Type
WHEN *Condition* THEN
CALL apoc.create.node(['NODE', row.Type], {name: row.Name, Type: row.Type, Details: row.Description, Function: row.Function, NrFunctions: row.NrTypes, ReturnType: row.ReturnType}) yield node return count(node)
ELSE
CALL apoc.create.node(['NODE', row.Type], {name: row.Name, Type: row.Type, Details: row.Description, ReturnType: row.ReturnType}) yield node return count(node)
END AS result", "return null", {batchSize:10000, parallel:true, params:{url: "file:///nodes.csv"}});
I get Failed to invoke procedure apoc.periodic.iterate: Caused by: org.neo4j.exceptions.SyntaxException: Invalid input 'CASE': expected
when I run the query above. How can I change/replace the query above to execute conditional statements within a periodic iterate statement for the cypher query?
Solution 1:[1]
Maybe it's a copy-paste error,
but the use of the apoc.periodic.iterate is not clear.
That is, the purpose of this procedure is to return something that has many results as the first parameter,
and then (in the second parameter) do heavy operations divided into batches, for example of write (like apoc.create.node() in your case).
Instead here it seems that in the second parameter there is "return null" and in the first parameter everything is executed.
Moving on to the actual question, afaik, I don't think that the CASE statement can currently be used in conjunction with the CALL apoc.create.node.
But in any case, since you can use the apoc, I suggest you to use the available procedures.
So apoc.do.when() instead of CASE,
and the apoc.load.csv() instead of the LOAD CSV:
So, to sum up:
CALL apoc.periodic.iterate(
"CALL apoc.load.csv($url) YIELD map RETURN map AS row", // return all result
"CALL apoc.do.when(true, // here the condition, TO BE CHANGED!
\"CALL apoc.create.node(['NODE', row.Type], {name: row.Name, Type: row.Type, Details: row.Description, Function: row.Function, NrFunctions: row.NrTypes, ReturnType: row.ReturnType}) yield node return count(node)\",
\"CALL apoc.create.node(['NODE', row.Type], {name: row.Name, Type: row.Type, Details: row.Description, ReturnType: row.ReturnType}) yield node return count(node)\", {row: row}) YIELD value RETURN 1 // return something in case
", {batchSize:10000, parallel:true, params:{url: "file:///nodes.csv"}})
Anyway, if you want/need, you can also use LOAD CSV WITH HEADERS FROM $url AS row RETURN row instead of CALL apoc.load.csv($url) YIELD map RETURN map AS row
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Giuseppe Villani |
