Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-17-2020 01:54 AM
Hi.
I want to import data from CSV which contains multiple lists and nested lists, like this example:
name,[articles],[keywords]
jack,[art1,art2,art3],[[data,lists],[data,analysis,qualitative research,quantification],[data,]]
I want to 'weave' the lists in such a way that I can create nodes and relation as:
(name)-[:WROTE]->(article)-[:ABOUT]->(keyword)
I have been looking into some of the apoc.map functions but it doesn't seem to be the way to go.
I am in my first month of learning Neo4j and my question might be straightforward, but I am stuck at the moment and would very much appreciate help or guidance.
Thank you,
Emil
11-17-2020 03:19 PM
Problem with csv file is that it treats all column values as strings. There is no datatype like list or map. To circumvent this situation we need to take a little bit longer route.
As there are many commas in the column values this poses a bigger problem when the selected delimiter is ','. Couple of options:
1. Select '|' (pipe) as the delimiter.
eg:
name|articles|keywords
jack|[art1,art2,art3]|[[data,lists],[data,analysis,qualitative research,quantification],[data,]]
2. Enclose your article and keywords strings in double quotes
eg:
name,articles,keywords
jack,"[art1,art2,art3]","[[data,lists],[data,analysis,qualitative research,quantification],[data,]]"
Here is a solution (with option 2):
LOAD CSV WITH HEADERS FROM "file:///emba.csv" AS row
with row
//parsing articles.................
with row.articles as a1, row
with replace(a1, '[', '') as a2, row
with replace(a2, ']', '') as a2, row
with split(a2, ',') as a22, row
//creating Name and Article nodes................
merge (a:Name {name: "jack"})
merge (b:Article {name: a22[0]})
merge (b1:Article {name: a22[1]})
merge (b2:Article {name: a22[2]})
//relating name and article nodes..................
merge (a)-[:WROTE]->(b)
merge (a)-[:WROTE]->(b1)
merge (a)-[:WROTE]->(b2)
with b, b1, b2, row
//parsing the keywords...............................
with b, b1, b2, row.keywords as k1
with b, b1, b2, split(k1, '],') as k21
with b, b1, b2, replace(k21[0], '[', '') as k22, replace(k21[1], ']', '') as k23, replace(k21[2], ']', '') as k24
with b, b1, b2, k22, k23, k24, replace(k23, '[', '') as k23a, replace(k24, '[', '') as k24a
//creating keyword nodes........................
merge (k:Keywords {keyword: k22})
merge (k1:Keywords {keyword: k23a})
merge (k2:Keywords {keyword: k24a})
//relating keywords to articles....................
merge (b)-[:ABOUT]->(k)
merge (b1)-[:ABOUT]->(k1)
merge (b2)-[:ABOUT]->(k2)
;
Added 7 labels, created 7 nodes, set 7 properties, created 6 relationships, completed after 692 ms.
Result:
11-18-2020 12:37 PM
Hi ameyasoft - Thank you very much for the answer, this looks exactly like what I am looking for 🙂
All the sessions of the conference are now available online