Search code examples
neo4jcypherneo4j-apoc

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

For such a query, even if just for export nodes, without relationships, it still gives the following field with all empty values:

 _start
  _end
  _type

And it's hard to filter out them in the process. Is there a parameter to control this?

ADDITIONS:

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

What I am doing here is to output the node M, node N and relation R into 3 different files. After I added the 2 replace functions to get rid of the last 3 columns, it reports this error:

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"

Does the CALL and WITH can't be used in a row like this?


Solution

  • You can use the APOC function apoc.text.replace to modify the CSV string to remove the unwanted rows:

    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
    

    Since the 3 columns you don't want are at the end of each row, the regular expression (?:,"_start","_end","_type"|,,,)(?=\n) matches the unwanted text at the end of each row (whether the header row or a data row).

    [UPDATE]

    With respect to the ADDITIONS issues: a WITH clause will drop any variables not specified by the clause, so N was not being carried forward. Also, you had other issues. This should fix up all the issues:

    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