apoc.export.graphml.query

This procedure is not considered safe to run from multiple threads. It is therefore not supported by the parallel runtime. For more information, see the Cypher Manual → Parallel runtime.

Details

Syntax

apoc.export.graphml.query(statement, file, config) :: (file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data)

Description

Exports the given NODE and RELATIONSHIP values from the Cypher statement to the provided GraphML file.

Input arguments

Name

Type

Description

statement

STRING

The query used to collect the data for export.

file

STRING

The name of the file to which the data will be exported.

config

MAP

{ stream = false :: BOOLEAN, format = 'cypher-shell' :: STRING timeoutSeconds = 100 :: INTEGER, compression = 'NONE' :: ['NONE', 'BYTES', 'GZIP', 'BZIP2', 'DEFLATE', 'BLOCK_LZ4', 'FRAMED_SNAPPY'], charset = 'UTF_8' :: STRING, source :: MAP, target :: MAP, useTypes :: BOOLEAN caption :: LIST<STRING>, nodesOfRelationships = false :: BOOLEAN }

Return arguments

Name

Type

Description

file

STRING

The name of the file to which the data was exported.

source

STRING

A summary of the exported data.

format

STRING

The format the file is exported in.

nodes

INTEGER

The number of exported nodes.

relationships

INTEGER

The number of exported relationships.

properties

INTEGER

The number of exported properties.

time

INTEGER

The duration of the export.

rows

INTEGER

The number of rows returned.

batchSize

INTEGER

The size of the batches the export was run in.

batches

INTEGER

The number of batches the export was run in.

done

BOOLEAN

Whether the export ran successfully.

data

ANY

The data returned by the export.

Configuration parameters

This procedure supports the following config parameters:

Configuration options
Parameter Type Default Description

stream

BOOLEAN

false

Return the data in the query result instead of storing it in a file.

compression

Enum[NONE, BYTES, GZIP, BZIP2, DEFLATE, BLOCK_LZ4, FRAMED_SNAPPY]

'NONE'

Allow taking binary data, either not compressed (value: NONE) or compressed (other values) .

timeoutSeconds

INTEGER

100

The maximum time in seconds the query should run before timing out.

charset

STRING

'UTF_8'

Name of the character extending java.nio.Charset in the currently used JDK. E.g.: US-ASCII, ISO-8859-1, UTF-8, UTF-16

useTypes

BOOLEAN

false

Add the types on to the file header.

format

STRING

'cypher-shell'

Export format.

source

MAP

false

To be used with target to import a relationships-only file. In this case, the source and target attributes of the edge tag are not based on the internal id of nodes but on a custom property value.
For example, in the path (:Foo {name: "aaa"})-[:KNOWS]→(:Bar {age: 666}), the KNOWS rel can be exported with a config <edge id="e2" source="aaa" sourceType="string" target="666" targetType="long" label="KNOWS"><data key="label">KNOWS</data><data key="id">1</data></edge>. Note the additional sourceType/targetType to detect the right type during the import.

target

MAP

100

To be used with source to import a relationships-only file. In this case, the source and target attributes of the edge tag are not based on the internal id of nodes but on a custom property value.
For example, in the path (:Foo {name: "aaa"})-[:KNOWS]→(:Bar {age: 666}), the KNOWS rel can be exported with a config <edge id="e2" source="aaa" sourceType="string" target="666" targetType="long" label="KNOWS"><data key="label">KNOWS</data><data key="id">1</data></edge>. Note the additional sourceType/targetType to detect the right type during the import.

caption

LIST<STRING>

['name', 'title', 'label', 'id']

Allow taking binary data, either not compressed (value: NONE) or compressed (other values) .

nodesOfRelationships

BOOLEAN

false

Add missing relationships between the found nodes.

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (Carrie:Person {name:'Carrie-Anne Moss', born:1967})
CREATE (Laurence:Person {name:'Laurence Fishburne', born:1961})
CREATE (Hugo:Person {name:'Hugo Weaving', born:1960})
CREATE (LillyW:Person {name:'Lilly Wachowski', born:1967})
CREATE (LanaW:Person {name:'Lana Wachowski', born:1965})
CREATE (JoelS:Person {name:'Joel Silver', born:1952})
CREATE
(Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix),
(Carrie)-[:ACTED_IN {roles:['Trinity']}]->(TheMatrix),
(Laurence)-[:ACTED_IN {roles:['Morpheus']}]->(TheMatrix),
(Hugo)-[:ACTED_IN {roles:['Agent Smith']}]->(TheMatrix),
(LillyW)-[:DIRECTED]->(TheMatrix),
(LanaW)-[:DIRECTED]->(TheMatrix),
(JoelS)-[:PRODUCED]->(TheMatrix);

The Neo4j Browser visualization below shows the imported graph:

play movies
Figure 1. Movies Graph Visualization

The apoc.export.graphml.query procedure exports the results of a Cypher query to a CSV file or as a stream.

The following query exports all DIRECTED relationships and the nodes with Person and Movie labels on either side of that relationship to the file movies-directed.graphml:

WITH "MATCH path = (person:Person)-[directed:DIRECTED]->(movie)
      RETURN person, directed, movie" AS query
CALL apoc.export.graphml.query(query, "movies-directed.graphml", {})
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data;
Results
file source format nodes relationships properties time rows batchSize batches done data

"movies-directed.graphml"

"statement: nodes(3), rels(2)"

"graphml"

3

2

7

2

5

-1

0

TRUE

NULL

The contents of movies-directed.csv are shown below:

movies-directed.graphml
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="born" for="node" attr.name="born"/>
<key id="name" for="node" attr.name="name"/>
<key id="tagline" for="node" attr.name="tagline"/>
<key id="label" for="node" attr.name="label"/>
<key id="title" for="node" attr.name="title"/>
<key id="released" for="node" attr.name="released"/>
<key id="label" for="edge" attr.name="label"/>
<graph id="G" edgedefault="directed">
<node id="n188" labels=":Movie"><data key="labels">:Movie</data><data key="title">The Matrix</data><data key="tagline">Welcome to the Real World</data><data key="released">1999</data></node>
<node id="n193" labels=":Person"><data key="labels">:Person</data><data key="born">1967</data><data key="name">Lilly Wachowski</data></node>
<node id="n194" labels=":Person"><data key="labels">:Person</data><data key="born">1965</data><data key="name">Lana Wachowski</data></node>
<edge id="e271" source="n193" target="n188" label="DIRECTED"><data key="label">DIRECTED</data></edge>
<edge id="e272" source="n194" target="n188" label="DIRECTED"><data key="label">DIRECTED</data></edge>
</graph>
</graphml>

The following query returns a stream of all DIRECTED relationships and the nodes with Person and Movie labels on either side of that relationship:

WITH "MATCH path = (person:Person)-[directed:DIRECTED]->(movie)
      RETURN person, directed, movie" AS query
CALL apoc.export.graphml.query(query, null, {stream: true})
YIELD file, nodes, relationships, properties, data
RETURN file, nodes, relationships, properties, data;
Results
file nodes relationships properties data

NULL

3

2

7

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
 <graphml xmlns="\"http://graphml.graphdrawing.org/xmlns\""
          xmlns:xsi="\"http://www.w3.org/2001/XMLSchema-instance\""
          xsi:schemaLocation="\"http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\"">
   <key id="\"born\"" for="\"node\"" attr.name="\"born\""/>
   <key id="\"name\"" for="\"node\"" attr.name="\"name\""/>
   <key id="\"tagline\"" for="\"node\"" attr.name="\"tagline\""/>
   <key id="\"label\"" for="\"node\"" attr.name="\"label\""/>
   <key id="\"title\"" for="\"node\"" attr.name="\"title\""/>
   <key id="\"released\"" for="\"node\"" attr.name="\"released\""/>
   <key id="\"label\"" for="\"edge\"" attr.name="\"label\""/>
   <graph id="\"G\"" edgedefault="\"directed\"">
     <node id="\"n188\"" labels="\":Movie\"">
       <data key="\"labels\"">:Movie</data>
       <data key="\"title\"">The Matrix</data>
       <data key="\"tagline\"">Welcome to the Real World</data>
       <data key="\"released\"">1999</data>
     </node>
     <node id="\"n193\"" labels="\":Person\"">
       <data key="\"labels\"">:Person</data>
       <data key="\"born\"">1967</data>
       <data key="\"name\"">Lilly Wachowski</data>
     </node>
     <node id="\"n194\"" labels="\":Person\"">
       <data key="\"labels\"">:Person</data>
       <data key="\"born\"">1965</data>
       <data key="\"name\"">Lana Wachowski</data>
     </node>
     <edge id="\"e271\"" source="\"n193\"" target="\"n188\"" label="\"DIRECTED\"">
       <data key="\"label\"">DIRECTED</data>
     </edge>
     <edge id="\"e272\"" source="\"n194\"" target="\"n188\"" label="\"DIRECTED\"">
       <data key="\"label\"">DIRECTED</data>
     </edge>
   </graph>
 </graphml>
  "