Use current BATCH iterative number (integer) WITHIN an apoc.periodic.iterate action

Within the action portion of an apoc.period.iterate I am performing an apoc.export.json.query.
I want the resulting filename to iterate along with the periodic.iterate batches.
The method I've used so far is making the export filename something like this:

WITH "my cypher query" as the query,
"CALL apoc.export.json.query('UNWIND $_batch as row with row.cid as cid MATCH (....','//subgraph//customer_export_'+$_count+'.json',{format:'plain',params:{_batch:$_batch}}) yield file RETURN file" as the action
CALL apoc.periodic.iterate(thequuery,theaction,{batchsize:100}) YIELD batches,committedOperations,failedBatches,errorMessages
RETURN  batches,committedOperations,failedBatches,errorMessages

$_count doesn't seem like the correct variable to be using... is there an equivilant that returns the iterating BATCH count, 0,1,2,3 that I can use to increment the output filename string from INSIDE the apoc.periodic.iterate function?

How about appending the batch index to each record in your data query, so it is available in your action query? You can service an index based on your given batch size. This may not be practical if you have a very large data set, since it requires collecting all the data first. The idea is unwind it with the batch index appended to the record. The following illustrates the concept. Not sure if this is helpful.

match(n:Item) 
with collect(n) as data, 3 as batchSize
unwind range(0,size(data)-1) as index
return id(data[index]), index/batchSize as batchIndex

In your case, you can use $_batch[0]. batchIndex as your file extension.

1 Like

This worked perfectly - thank you for the suggestion!

1 Like