从多个 csv 文件加载重复节点时如何合并它们?

问题描述 投票:0回答:1

抱歉,如果这很难解析,我才使用 NEO4j 几天。 我正在尝试从多个 csv 文件构建数据库。 “styles.csv”包含著名的建筑师和建筑。建筑师和建筑物 csv 文件包含有关其各自条目的附加信息。

数据结构示例

样式 csv

Styles csv snapshot

建筑物 csv

Buildings csv snapshot

建筑师 csv

Architects csv snapshot

但是,当我顺序导入 csv 文件时,它会为具有相同名称值的每个节点创建重复的节点。例如,here it is creating a node for the 'roman' style.

我按顺序加载每个 csv 并在存在具有多个值的单元格的地方展开。

LOAD CSV WITH HEADERS FROM 'file:///restructured_data_styles.csv' AS stylesheet
MERGE (s:Style {Style: stylesheet.Style, Dates: stylesheet.`Dates`, Location: stylesheet.Location})
WITH s, stylesheet
UNWIND split(stylesheet.`Examples`, ';') AS stylebuildings
MERGE (buildings:Building{name:stylebuildings})
MERGE (s)-[rbs:BUILT_IN_STYLE]->(buildings)
LOAD CSV WITH HEADERS FROM 'file:///restructured_data_architects.csv' AS architectsheet
MERGE (a:Architect {Architect: architectsheet.Name, Born: architectsheet.Born, Died: architectsheet.Died, Description: architectsheet.Description})
WITH a, architectsheet
UNWIND split(architectsheet.`Buildings`, ';') AS architectbuildings
MERGE (buildings:Building{name:architectbuildings})
MERGE (a)-[rab:NOTABLE_PROJECTS]->(buildings)

我想要的是加载每个 csv 并让 neo4j 添加每个 csv 文件提供的附加信息,同时合并创建的重复节点,同时维护已存在的关系。 就目前情况而言,文件内构建的关系很好,但我在链接这些节点和 csv 之间的关系时遇到了麻烦。那有意义吗?如果没有的话会进一步澄清!谢谢! 正在创建节点,但它们不会根据名称值进行合并。已经研究过 APOC 重构,但我对此很陌生(而且也不是数据科学家!) - 简单的解释会非常有帮助。

neo4j cypher
1个回答
0
投票
  1. 与您显示的密码不一致。它有一个 Your graphStyle 节点,该节点具有

    name
    属性,但缺少
    Style
    Dates
    Location
    属性。您需要修复创建这些不一致的
    Style
    节点的原因,因为
    MERGE (s:Style {Style: stylesheet.Style, Dates: stylesheet.
    Dates
    , Location: stylesheet.Location})
    永远不会匹配这样的
    Style
    节点。
    
    

  2. 如果同一个标签有多个
  3. MERGE

    子句,它们都应该指定同一组属性。如果它们不相同,则具有更多属性的

    MERGE
    将永远不会与具有较少属性的
    MERGE
    创建的节点匹配。
    
    

  4. MERGE

    子句应仅指定唯一标识节点(或关系)所需的最少属性(如果有)。

    例如,代替:

    MERGE (a:Architect {Architect: architectsheet.Name, Born: architectsheet.Born, Died: architectsheet.Died, Description: architectsheet.Description})

    使用它会更安全(假设 
    Architect

    值是唯一的):

    MERGE (a:Architect {Architect: architectsheet.Name})
    ON CREATE SET s += {Born: architectsheet.Born, Died: architectsheet.Died, Description: architectsheet.Description}
    

    或者,甚至更好,因为架构师可以具有相同的名称(但这需要您的非架构师 csv 文件使用 
    id

    而不是名称来引用架构师):

    MERGE (s:Style {id: stylesheet.styId})
    ON CREATE SET s += {Architect: architectsheet.Name, Style: Dates: stylesheet.`Dates`, Location: stylesheet.Location}
    

    
        
© www.soinside.com 2019 - 2024. All rights reserved.