首页 文章

Neo4J - 如何2导入CSV文件与它们之间的关系?

提问于
浏览
2

我有两个CSV文件:员工和部门 . 我可以使用以下查询将它们加载到 Neo4J 中:

LOAD CSV WITH HEADERS FROM "file:///Path/to/myCSVfile/depart.csv" AS row
CREATE (:Department{departmentID:toInt(row.id),name:row.name, depart_manager:toInt(row.department_manager)});

LOAD CSV WITH HEADERS FROM "file:///Path/to/myCSVfile/Empl.csv" AS row
CREATE (:Employee{employeeID:toInt(row.id),firstName:row.first_name, lastName:row.last_name, email:row.email, 
startDate:row.startdate, birthDay:row.birthdate, function:row.function, departementId:row.depart_id});

现在我想添加它们之间的关系......换句话说,每个员工应该属于一个特定的部门(depart_id) .

CSV文件已经导入,我可以显示每个标签的节点 .

怎么能达到这个目的?是否可以在使用脚本/查询导入后执行此操作,还是应该使用其他脚本重新导入它们?

1 回答

  • 3

    您可以尝试在第二个LOAD CSV语句的末尾添加所需的关系 .

    像这样:

    LOAD CSV WITH HEADERS FROM "file:///Path/to/myCSVfile/depart.csv" AS depRow
    CREATE (:Department{departmentID:toInt(depRow.id),name:depRow.name, depart_manager:toInt(depRow.department_manager)})
    WITH *
    LOAD CSV WITH HEADERS FROM "file:///Path/to/myCSVfile/Empl.csv" AS empRow
    CREATE (employee:Employee{employeeID:toInt(empRow.id),firstName:empRow.first_name, lastName:empRow.last_name, email:empRow.email, 
    startDate:empRow.startdate, birthDay:empRow.birthdate, function:empRow.function, departmentId:empRow.depart_id})
    WITH employee
    // Adding relation between employee and department
    MATCH (dep:Department {departmentID : employee.departmentId})
    CREATE (employee)-[:BELONGS_TO]->(dep)
    

相关问题