0
votes

https://raw.githubusercontent.com/saurabhkumar1903/neo4j/master/alterFile/sampletestoutput1.csv

here's the link to an image showning my expected output:[https://i.imgur.com/x6CYdfU.jpg] I've drawn it on paper just to show the expected output.

[I have a csv file containing a list of nodes where each line denotes a relationship of node at line[0] with every other list of nodes line2,line2,line[3].....line[4500] in that line]

eg. 1,3,4,5,7,8

2,4,5,11

4,10,11,15

here node at line[0] i.e. "1" has a directed relationship with nodes at line[2] i.e "3" as a friend, nodes at line[4], i,e."4" as a friend, nodes at line[6], i,e."5" as a friend,

what i am trying to do is I want to show a graph in neo4j dipicting the suggested friend relationship among each nodes.

what I cannot figure out is how to iterate the whole csv file as well as capture the relationship among each nodes on each line of csv file.

1
I don't understand what you want to do Can you give a more explicit example of what you have in your CSV (an extract) and what you want to achieve ?logisima
First of all, in Neo4j line[0] means column 0 of the currently read CSV live (provided you wrote 'AS line' in your instruction) Would this CYpher solve your issue MATCH (p0:Person{id:line[0]}),(p1:Person{id:line[0]}) CREATE (p0)-[:FRIEND_OF]->(p1) Still, I agree with @logisima, you should be more explicitJerome_B

1 Answers

0
votes

If you want to ensure there is a HAS_FRIEND relationship between a person whose id comes first (in line) and the people whose ids come afterwards (in the same line), something like this should work:

LOAD CSV FROM 'file:///friends.csv' AS line
MERGE (p:Person {id: TOINT(line[0])})
FOREACH(id IN line[1..] |
  MERGE(f:Person {id: TOINT(id)})
  MERGE (p)-[:HAS_FRIEND]-(f))

This query presumes that you only want a single HAS_FRIEND relationship between any 2 nodes. Therefore, the MERGE for the relationship does not specify a direction. That way, if there is already such a relationship in either direction, no new relationship is created.

Also, the TOINT function is used to convert the id values to integers (since LOAD CSV automatically treats all values as strings). If you don't need that conversion, then you can remove the function calls.