2
votes

I am trying to create tree structure in Neo4j with Cypher Query.

I am loading data from CSV to graph.

Data structure is. Its having two Column that is Parent and Child

P1,C1

P1,C2

P1,C3

C1,SC1

C1,SC2

C2,SC3

C3,SC4

SC1,SSC1

SC2,SSC2

And so on....

Here P is 1st level(Parent, parent 1) and C is Child, SC is Sub-Child and SSC is Sub-Sub-Child, I want to Load the data and create tree structure from top-to bottom

I have used this Query but not getting the tree structure.

LOAD CSV  FROM 'file:C:/Users/ykumarx077781/Desktop/parent.csv' AS line

CREATE (Parent:Parent { Parent: line[0]} )

CREATE (Child:Child { Child: line[1]} )

CREATE (Parent)-[R:Parent_off]->(Child)

return R;

Please help me out to create the structure..

Also used MERGE but did not get

LOAD CSV  FROM 'file:C:/Users/ykumarx077781/Desktop/parent.csv' AS line

MERGE (Parent:Parent { Parent: line[0]} )

MERGE (Child:Child { Child: line[1]} )

CREATE (Parent)-[R:Parent_off]->(Child)

return R;
2

2 Answers

1
votes

You have to use MERGE for any node that you might want to reuse, see http://neo4j.com/docs/stable/cypherdoc-importing-csv-files-with-cypher.html e.g. for the countries.

1
votes

Try

create index on :Node(id)

LOAD CSV  FROM 'file:C:/Users/ykumarx077781/Desktop/parent.csv' AS line

MERGE (Parent:Node { id: line[0]} ) ON CREATE SET Parent:Parent

MERGE (Child:Node { id: line[1]} ) ON CREATE SET Child:Child

MERGE (Parent)-[R:Parent_off]->(Child)

return R;