使用APOC.export时是否可以不输出关系列?

Is it possible not to output relation column when using APOC.export?

MATCH (m:computer)

WITH collect(DISTINCT m) AS M
CALL apoc.export.csv.data( M, [], null, {stream:true, batchSize:100}) YIELD data as mdata

对于这样的查询,即使只是导出节点,没有关系,它仍然会给出以下字段全为空值:

 _start
  _end
  _type

并且很难在此过程中过滤掉它们。有参数控制吗?

添加:

MATCH (m:SportsTeam)-[r:hasMember]-(n)
WITH collect(DISTINCT m) AS M, collect(DISTINCT n) AS N, collect(r) as R
CALL apoc.export.csv.data( M, [], null, {stream:true}) YIELD data
WITH apoc.text.replace(data, '(?:,"_start","_end","_type"|,,,)(?=\n)', '') AS mdata
CALL apoc.export.csv.data( N, [], null, {stream:true}) YIELD data as ndata
WITH apoc.text.replace(data, '(?:,"_start","_end","_type"|,,,)(?=\n)', '') AS ndata
CALL apoc.export.csv.data( [], R, null, {stream:true}) YIELD data as rdata
RETURN mdata, ndata, rdata

我在这里做的是将节点M、节点N和关系R输出到3个不同的文件中。在我添加 2 个替换函数以删除最后 3 列后,它报告此错误:

neobolt.exceptions.CypherSyntaxError: Variable `N` not defined (line 5, column 28 (offset: 282))
"CALL apoc.export.csv.data( N, [], null, {stream:true}) YIELD data as ndata"

CALL 和 WITH 不能这样连着用吗?

您可以使用 APOC 函数 apoc.text.replace 修改 CSV 字符串以删除不需要的行:

MATCH (c:computer)
WITH COLLECT(DISTINCT c) AS cs
CALL apoc.export.csv.data(cs, [], null, {stream:true, batchSize:100}) YIELD data
WITH apoc.text.replace(data, '(?:,"_start","_end","_type"|,,,)(?=\n)', "") AS newData
... // use newData, which does not have the last 3 columns

由于您不需要的 3 列位于每行的末尾,因此正则表达式 (?:,"_start","_end","_type"|,,,)(?=\n) 匹配每行末尾不需要的文本(无论是 header 行还是一个数据行)。

[更新]

关于 ADDITIONS 问题:WITH 子句将删除该子句未指定的任何变量,因此 N 未被结转。另外,你还有其他问题。这应该可以解决所有问题:

MATCH (m:SportsTeam)-[r:hasMember]-(n)
WITH COLLECT(DISTINCT m) AS M, COLLECT(DISTINCT n) AS N, COLLECT(r) as R
CALL apoc.export.csv.data( M, [], null, {stream:true}) YIELD data
WITH apoc.text.replace(data, '(?:,"_start","_end","_type"|,,,)(?=\n)', '') AS mdata, N, R
CALL apoc.export.csv.data( N, [], null, {stream:true}) YIELD data
WITH apoc.text.replace(data, '(?:,"_start","_end","_type"|,,,)(?=\n)', '') AS ndata, mdata, R
CALL apoc.export.csv.data( [], R, null, {stream:true}) YIELD data AS rdata
RETURN mdata, ndata, rdata