Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j cypher store array property during csv import

I need to import data from a csv of the form

id;name;targetset
1;"somenode",[1,3,5,8]
2,"someothernode",[3,8]

into the graph and I need to have targetset stored as collection (array) using cypher. I tried

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/mytable.csv" AS row FIELDTERMINATOR ';'
CREATE (:MyNode {id: row.id, name: row.name, targetset: row.targetset});

but it stores targetset as a string, e.g. "[1,3,5,8]". There does not seem to be a function to convert array-encoding-strings to actual arrays, like there is toInt to convert strings to integers. Is there still another possibility?

like image 329
tscherg Avatar asked Sep 02 '25 05:09

tscherg


2 Answers

APOC Procedures will be your best bet here. Use the function apoc.convert.fromJsonList().

An example of use:

WITH "[1,3,5,8]" as arr
RETURN apoc.convert.fromJsonList(arr)
like image 131
InverseFalcon Avatar answered Sep 04 '25 18:09

InverseFalcon


You can try this:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:/mytable.csv" AS row FIELDTERMINATOR ';'
CREATE (:MyNode {id: row.id, name: row.name, targetset: split(substring(row.targetset, 1, length(row.targetset) - 2), ',') });

The above code remove the [ and ] chars from the string [1,3,5,8] using substring() and length() functions. After the string 1,3,5,8is splited considering , as separator.

like image 33
Bruno Peres Avatar answered Sep 04 '25 19:09

Bruno Peres