Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-28-2020 08:41 AM
Hi All, I have a data in excel sheet and I want to import it into Neo4j. The above pic is just a example not original data. I just want to import data and create relationship based on personId and OrganizationId as per column 5 .
Thanks in advance for your kind response.
Solved! Go to Solution.
12-28-2020 10:11 AM
Try this;
After creating the Person and Organization nodes, run your .csv file again to create the relationships based on column 5 values. Here is the Cypher:
with left(line.column5, 2) as n1, right(line.column5, 2) as n2
MATCH (a:Person) where a.id = n1
MATCH (b:Organization) where b.id = n2
with a, b
MERGE (a)-[:ORGANIZATION]->(b)
12-28-2020 10:11 AM
Try this;
After creating the Person and Organization nodes, run your .csv file again to create the relationships based on column 5 values. Here is the Cypher:
with left(line.column5, 2) as n1, right(line.column5, 2) as n2
MATCH (a:Person) where a.id = n1
MATCH (b:Organization) where b.id = n2
with a, b
MERGE (a)-[:ORGANIZATION]->(b)
12-28-2020 11:11 PM
Thanks for your solution it worked but it will not make relationship between all nodes. One More thing how can i make relation between nodes if I have 3 ids in column 5 e.g. P1.O1.P2
12-29-2020 12:49 AM
Hello Ameyasoft, The ID will increments on row 11 which mean now we have different ID in column5 as P15O12... So how i can query this ?
12-29-2020 05:57 AM
Hi @syedsibtainshahbaz, I took the liberty of generating my own csv like your standard. (I can't upload a csv file, so uploaded as txt file, please, rename it to csv file). data.txt (2.5 KB)
match (n) detach delete (n);
USING PERIODIC COMMIT 100 load csv with headers from 'file:///p.csv' as row with
row where row.person_id is not null
merge (p:Person {id: row.person_id}) set p.name=row.person_name
merge (o:Orgaization {id: row.organization_id}) set o.name=row.organization_name
with split(row.relation, 'O')[0] as per, "O"+split(row.relation, 'O')[1] as company
match (p:Person{id:per}) match(o:Orgaization{id:company})
merge (p)-[r:LINKS]->(o) return p.id,p.name,r,o.id,o.name;
The above query is dynamically get the Person ID and Organization ID without any hard code substrings.
Feel free to change the relationship name and other properties.
and of course, they need index etc, i ignore them for providing the solution.
Let me or the community forums know, if you need further assistance.
12-29-2020 07:08 AM
Thanks..
12-30-2020 11:28 AM
Hi Ameyasoft,
Can you please tell me how can I enter relationship properties from csv file. As I have some relationship properties so I need to import it from csv to relationship against the ids. So Can you please guide me what can I do ? Waiting for your kind response . Thanks
12-30-2020 11:33 AM
You have a column (say column 6) with values and you want to add them as properties to the relationships? Share some sample data and I can help with Cypher queries.
01-05-2021 05:48 AM
Hi ameyasoft, as above mention example i have load data from csv file and now i want to represent it in hierarchal structure . So can you please guide me how can i do this ?
12-30-2020 11:40 AM
Here is the example of data column 1 and 2 are the seperate ids whereas column 3 is a combine ids.. column 4 and 5 is the relationship properties of column 1 and 2.. column 3 is used to make relationship based on ids.. Hope so you understand.
12-30-2020 07:46 PM
Hope this is what you are looking for:
MERGE (a:Person {id:line.column1)
MERGE (b:Organization {id:line.column2)
MERGE (a)-[:NEXT {prop:line.column4, id:toInteger(line.column5)}]->(b)
12-31-2020 12:33 AM
12-31-2020 06:33 AM
Please, refer apoc.create.setRelProperty documentation
Attributes are Key Value pairs.
A small demo
data.csv
col1,col2,col3,col4,col5
T1,TA1,T1TA1,Medium,4
T2,TA2,T2TA2,High,5
T3,TA3,T3TA3,Low,3
T4,TA4,T4TA4,Moderate,2
T5,TA5,T5TA5,Very Low,1
USING PERIODIC COMMIT 100 load csv with headers from 'file:///data.csv' as row
with row where row.col1 is not null
merge (c1:Col1 {id: row.col1})
merge (c2:Col2 {id: row.col2})
merge (c1)-[r:HAS]->(c2)
with r,c1,c2,row
CALL apoc.create.setRelProperty(r, row.col4, row.col5)
YIELD rel
return c1,r,c2;
╒═══════════╤════════════════╤════════════╕
│"c1" │"r" │"c2" │
╞═══════════╪════════════════╪════════════╡
│{"id":"T1"}│{"Medium":"4"} │{"id":"TA1"}│
├───────────┼────────────────┼────────────┤
│{"id":"T2"}│{"High":"5"} │{"id":"TA2"}│
├───────────┼────────────────┼────────────┤
│{"id":"T3"}│{"Low":"3"} │{"id":"TA3"}│
├───────────┼────────────────┼────────────┤
│{"id":"T4"}│{"Moderate":"2"}│{"id":"TA4"}│
├───────────┼────────────────┼────────────┤
│{"id":"T5"}│{"Very Low":"1"}│{"id":"TA5"}│
└───────────┴────────────────┴────────────┘
All the sessions of the conference are now available online