Create csv values as edge properties while importing

apoc

(jim F) #1

I have almost similar case to the question here in stack overflow . https://stackoverflow.com/questions/48529861/create-a-node-for-each-column-only-once-while-importing-csv-into-neo4j
Here the edges are made if the cell have value of 1, but in my case I want to create edges properties with value inside the cell

I am new to neo4j space and I tried my best to do this but with no luck, then I found a hard way, which I am sure is not computationally effective (it got unnecessary iteration and is running for few hours now). Here is what I tried.

LOAD CSV FROM 'file:///newquotes.csv' AS line
FIELDTERMINATOR '\t'
MERGE (h:Headers)
SET h.names = line
WITH line
LIMIT 1
UNWIND line[2..] AS name
MERGE (c:Cell {name: name})
WITH apoc.map.fromNodes('Cell', 'name') AS cells
MATCH (h:Headers)
LOAD CSV FROM 'file:///newquotes.csv' AS line
FIELDTERMINATOR '\t'
WITH h, cells, line
SKIP 1 
MERGE (g:Gene {id: line[1], name: line[0]})
FOREACH(
  x IN REDUCE(s = [], i IN RANGE(2, SIZE(line)-1) |
    CASE line[i] WHEN "type1" THEN s + cells[h.names[i]] ELSE s END) |
  MERGE (g)-[:has {value :"type1"}]-(x))
FOREACH(
  y IN REDUCE(s = [], i IN RANGE(2, SIZE(line)-1) |
    CASE line[i] WHEN "type2" THEN s + cells[h.names[i]] ELSE s END) |
  MERGE (g)-[:has {value : "type2"}]-(y))
FOREACH(
  z IN REDUCE(s = [], i IN RANGE(2, SIZE(line)-1) |
    CASE line[i] WHEN "type3" THEN s + cells[h.names[i]] ELSE s END) |
  MERGE (g)-[:has {value : "type3"}]-(z))

I asked this in slack, but I dint got any help from there. Hope someone here could help this newbie to get started .


(M. David Allen) #2

This:

MERGE (h:Headers)
SET h.names = line

Is probably not doing what you think it is. You should first adjust to use LOAD CSV WITH HEADERS so that you can refer to things like line.name instead of line[0] that would clean a lot of things up and make it easier to read. This line where you're MERGE headers is creating a new "Headers" node for every single line in your CSV file which is probably not what you want.

It'd help if you explain what you're trying to end up with, then we can adjust this more.

You also don't need to do the UNWIND. I'll take a stab at a quick rewrite. You'll need to adjust this but maybe it'll get you pointed in the right direction.

LOAD CSV WITH HEADERS FROM 'file:///newquotes.csv' AS line
MERGE (c:Cell { name: line.name })
MERGE (g:Gene { id: line.geneID, name: line.geneName })

(and then so on)


(jim F) #3

@david.allen I really appreciate in replying to this query. I got some idea now, let me try to work on it.