Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-20-2020 02:59 AM
I am trying to import a 80mb json file with around 2mio lines.
Therefore i am running into a out of heap space error.
To prevent the error i wanted to use periodic commit which i was not successfull with.
Thats the command i tried:
CALL apoc.periodic.commit(
"CALL apoc.load.json('file:/master.json')
YIELD value
MERGE (p:Product {name:value.name})
WITH p, value
UNWIND value.categories as cat
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)",
{batchSize:100, parallel:true})
Is it even possible to nest two apocs?
And how can i achieve batch import with json files in neo4j?
Solved! Go to Solution.
08-20-2020 04:58 AM
I think i found it: there was a sneaky little comma too much inside the UNWIND line.
And i closed the first query after "YIELD value" already
The final working code is the following:
CALL apoc.periodic.iterate(
"CALL apoc.load.json('file:/master_small.json')
YIELD value",
"MERGE (p:Product {name:value.name})
WITH value, p
UNWIND value.categories as cat
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)",
{batchSize:100, parallel:true})
Thank you @Cobra for sticking with me!
08-20-2020 03:23 AM
Hello @daniel.muellner
Yes it's possible
Did you have a look at apoc.periodic.iterate()?
Regards,
Cobra
08-20-2020 04:24 AM
Thank you for your confirmation.
Although i read the docs I was confused because it does not show a nested apoc call and i ran into weird errors as i mixed the syntax of periodic.commit() and periodic.iterate().
The corrected script is:
(i added the limit and removed the batchsize params)
CALL apoc.periodic.commit(
"CALL apoc.load.json('file:/master_small.json')
YIELD value
MERGE (p:Product {name:value.name})
WITH value, p limit $limit
UNWIND value.categories as cat
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)",
{limit:100})
Running this code throws this error:
{
"No element found in java.util.Arrays$ArrayItr@224f659a": 1
}
When i run it without the apoc.periodic.commit() it works with no errors though:
CALL apoc.load.json('file:/master_small.json')
YIELD value
MERGE (p:Product {name:value.name})
WITH value, p
UNWIND value.categories as cat
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)
08-20-2020 04:31 AM
I never see this error but it looks like something is empty
Can you try with apoc.periodic.iterate()?
I don't think commit is the right function for what you want to
08-20-2020 04:48 AM
I will give you some more insights into my plans:
I want to import a json file with the following structure:
(just with a lot more entries and properties....you get the point)
My goal is to import all products as nodes and categories as nodes.
As you can see the categories are repetetive and hierarchical. So i want no duplicates of the categories.
Without the periodic import and a small json the result is this:
Back to your idea: periodic.iterate()
I tried the following code:
CALL apoc.periodic.iterate(
"CALL apoc.load.json('file:/master_small.json')
YIELD value
MERGE (p:Product {name:value.name})",
"WITH value, p
UNWIND value.categories as cat,
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)",
{batchSize:100, parallel:true})
I received the following error:
(I am not entirely sure where to split the cypher queries (as iterate() needs two?!)
I appreciate your help a lot
08-20-2020 04:53 AM
CALL apoc.periodic.iterate('
CALL apoc.load.json("file:/master_small.json")
YIELD value
MERGE (p:Product {name:value.name})
RETURN value, p
','
WITH value, p
UNWIND value.categories AS cat,
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)
', {batchSize:100, parallel:true})
08-20-2020 04:58 AM
I think i found it: there was a sneaky little comma too much inside the UNWIND line.
And i closed the first query after "YIELD value" already
The final working code is the following:
CALL apoc.periodic.iterate(
"CALL apoc.load.json('file:/master_small.json')
YIELD value",
"MERGE (p:Product {name:value.name})
WITH value, p
UNWIND value.categories as cat
MERGE (c:Categorie {name:cat})
MERGE (c)-[r:INHERITS]->(p)",
{batchSize:100, parallel:true})
Thank you @Cobra for sticking with me!
All the sessions of the conference are now available online