簡體   English   中英

使用一個 csv 文件中的節點和關系在 neo4j 中加載 csv

[英]Load csv in neo4j with nodes and relationships in one csv file

抱歉,我是 neo4j 的新手,並且在我想象的一個非常簡單的例子中苦苦掙扎。

我想建模一個我存儲為 csv 的組織結構圖

id,name,manager_id
1,allan,2
2,bob,4
3,john,2
4,sam,
5,Jim,2

請注意,Bob 有 3 個直接下屬,Bob 向 Sam 報告,Sam 不向任何人報告。

我想制作一個顯示管理鏈的圖表。 我嘗試了以下方法,但它產生了與人脫節的關系:

LOAD CSV WITH HEADERS FROM "file///employees.csv" AS csvLine
CREATE (p:Person {id: csvLine.id, name: csvLine.name})
CREATE (p)-[:MANAGED_BY {manager: csvLine.manager_id}]->(p)

這個查詢創建了一堆自引用關系。 無論如何,是否可以通過單個 csv 的一個命令來填充圖形? 我一定遺漏了一些東西,我們將不勝感激。 謝謝

我想這就是你要找的。

在您的查詢中,您正在創建pp之間的關系,因此是自引用關系。

我添加了一個 coalesce 語句來處理沒有manager_id值的人。 這樣 Sam 就可以向自己報告。

LOAD CSV WITH HEADERS FROM "file:///employees.csv" AS csvLine
// create or match the person in the left column
MERGE (p:Person {id: csvLine.id })

// if they are created then assign their name
ON CREATE SET p.name = csvLine.name

// create or match the person/manager in the right column
MERGE (p1:Person {id: coalesce(csvLine.manager_id, csvLine.id) })

// create the reporting relationship
CREATE (p)-[:MANAGED_BY]->(p1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM