如何使用Neo4j MERGE包含具有NULL值的属性 [英] How to include properties with NULL values using Neo4j MERGE

查看:650
本文介绍了如何使用Neo4j MERGE包含具有NULL值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node表和Edge表,它们都可以作为CSV文件使用. 我设法通过以下方式加载Node表:

I have a Node table and an Edge table, both available as CSV files. I managed to load the Node table by:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///NodesETL.csv' AS line
CREATE (:InfoNodes {id: toString(line.id), description: toString(line.description)})

此查询使用CSV文件的字段值作为:InfoNodes的属性来创建InfoNodes.

This query creates the InfoNodes with the field values of the CSV file as properties of :InfoNodes which is fine.

InfoNode与其他InfoNode有关系,例如这些关系在具有相同标签的节点之间存在. 这些关系存储在Edge表中,可作为其他CSV文件使用. Edge表的每一行都包含idfrom和idto字段,这些字段根据InfoNode的id属性定义了InfoNode之间的关系. 边缘表还包含3个其他字段,它们代表关系的属性. firstproperty始终是字符串,绝不能为NULL,例如永远不会是一个空字符串.均为字符串的secondproperty和thirdproperty可以具有NULL值,例如".因此,secondproperty和/或thirdproperty可以包含NULL值. 我尝试使用此Edge表通过以下方式创建(:InfoNodes)之间的[:RELATIONSHIP {firstproperty:,secondproperty:,thirdproperty:}]关系:

InfoNodes have relationships with other InfoNodes, e.g. these relationships exists between Nodes with the same label. These relationships are stored in an Edge table available as an additional CSV file. Every row of this Edge table holds idfrom and idto fields that defines the relationships between InfoNodes on basis of their id property. The Edge table holds also 3 additional fields representing properties of the relationship. The firstproperty is always a string and never NULL e.g. never an empty string. The secondproperty and thirdproperty, both type string, can have NULL values like "". So secondproperty and/or thirdproperty can contain NULL values. I try to use this Edge table to create the [:RELATIONSHIP {firstproperty:, secondproperty:, thirdproperty:}] relationships between (:InfoNodes) by:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///EdgesETL.csv' AS line
MATCH (from:InfoNodes{id: toString(line.idfrom)})
MATCH (to:InfoNodes{id: toString(line.idto)})
MERGE (from)-[:RELATION {firstproperty: toString(line.firstproperty), secondproperty: toString(line.secondproperty), thirdproperty: toString(line.thirdproperty)}]->(to)

当Edge表中的secondproperty和thirdproperty包含NULL值时,第二个Cypher脚本将导致错误. Neo4j错误消息是:无法合并使用null属性值作为第二属性的关系. 当我从第二个脚本中删除secondproperty字段和secondproperty:属性时,发生了相同类型的错误,并提到了Thirdproperty:无法合并对null属性使用null属性值的关系 当我从上一个脚本中删除了第二属性和第三属性字段以及属性时,就会创建InfoNode之间的[:RELATIONS]关系,包括存储为[:RELATION]关系的firstproperty:属性的firstproperty表字段.

This second Cypher script results into an error when secondproperty and thirdproperty in the Edge table contain NULL values. The Neo4j error message is: Cannot merge relationship using null property value for secondproperty. When I remove from the second script the secondproperty field and secondproperty: property than the same type of error occurs mentioning thirdproperty: Cannot merge relationship using null property value for thirdproperty When I remove secondproperty and thirdproperty fields and properties from the previous script then the [:RELATIONS] relationships between InfoNodes are created including firstproperty table fields stored as firstproperty: property of the [:RELATION] relationship.

问题:如何扩展第二个脚本,以便从Edge表中将第二个属性和Thirdproperty字段加载到[:RELATION]关系(包括NULL值)中的secondproperty:和thirdproperty:中?

Question: How to extend the second script in order to load from the Edge table the second property and thirdproperty fields into secondproperty: and thirdproperty: of [:RELATION] relationships including NULL values?

可以t合并为空值; neo4j 中的无法使用空属性值合并节点" 描述了相同的问题,但是在具有空值的多个字段/属性的情况下无法回答我的问题.

Can't MERGE with null values; 'Cannot merge node using null property value' in neo4j describes the same problem but doesn't answer my question in case of multiple fields/properties with NULL values.

推荐答案

您将要重新查看

You'll want to re-review the MERGE section in the developer guide. Specifically, in the introduction, there's mention of ON CREATE and ON MATCH. This allows you to set properties in cases where the MERGE resulted in a creation, or when instead the MERGE matched upon existing elements.

通常,您只希望合并唯一定义事物的属性(例如ID),并在ON CREATE内设置其余属性.

Typically you will want to MERGE only properties that uniquely define the thing, like IDs, and set the rest of the properties within ON CREATE.

此更改后的查询可能类似于以下内容:

Your query after this change might look something like this:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM 'file:///EdgesETL.csv' AS line
MATCH (from:InfoNodes{id: toString(line.idfrom)})
MATCH (to:InfoNodes{id: toString(line.idto)})
MERGE (from)-[r:RELATION {firstproperty: toString(line.firstproperty)}]->(to)
ON CREATE SET r.secondproperty = toString(line.secondproperty), r.thirdproperty = toString(line.thirdproperty)

这篇关于如何使用Neo4j MERGE包含具有NULL值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆