0

Suppose I have the following table in a csv.

 elementID | groupID | sequence
     abc   |    A    |    0
     dcv   |    A    |    1
     asd   |    B    |    3
     ccc   |    B    |    2
     abc   |    B    |    4

I have already created the Element nodes in Neo4j (sequence is not an attribute of the nodes) having as attribute the key elementID.

From this csv I need to create a relationship from each Element node belonging to a group to another element belonging to the same group having the next sequence number.

With the data above I need to create a relationship from abc to dcv (labeled A for example), from asd to abc (labeled B) and from ccc to asd labeled B again.

I thought the query in this way:

LOAD CSV WITH HEADERS FROM "file:///file.csv" AS row
WITH row
MATCH (from:Element {elementID : row.elementID})
MATCH (to:Element {...})
MERGE (from)-[r:row.groupID]->(t)

The problem is that I do not know how to MATCH the second node since in the graph I need the nodeID while with the current row variable I have only data of the from node.

How can I do it? Is there a way to use a second variable representing another entry in the csv to be used for the second MATCH?

In this last case, I would need to express conditions on it such as:

WHERE row2.sequence = row.sequence+1 AND row2.groupID = row.groupID
Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
roschach
  • 8,390
  • 14
  • 74
  • 124
  • This type of CSV is not recommended. Instead use csv with columns like: `from, to, group`. – Rajendra Kadam May 30 '19 at 12:30
  • You can't match the second-row entry in this way. If you can't make this like above format think of collect all element id for each group and sort them with a sequence number and Then create a relationship between elements within that list. – Rajendra Kadam May 30 '19 at 12:33
  • For dynamic relationship names, you can use `APOC Plugin`. – Rajendra Kadam May 30 '19 at 12:33
  • @Raj I do not choose the structure of the `csv` file. Just to let you know although I changed the table column names it is the same structure of the csv `stop_times.txt` file of the google_transit in the General Transit Feed Specification (GTFS) format. – roschach May 30 '19 at 12:41
  • They only thing I could do is create a new csv from this one and then import it in Cypher. – roschach May 30 '19 at 12:41
  • Okay. You can also load data from this CSV but the query will be complicated. – Rajendra Kadam May 30 '19 at 12:56
  • 1
    I mean create a new csv with other tools such as Python and Pandas, save it and just use that in Cypher, discarding the original one. Columns of the new one `first_el`, `second_el`, `groupID` for example. – roschach May 30 '19 at 13:00
  • Yeah, that would be easier – Rajendra Kadam May 30 '19 at 13:03

0 Answers0