|| apoc.export.graphml.query - APOC 核心文档 - Neo4j 文档

apoc.export.graphml.query

此过程不被认为是多线程安全运行的。因此,并行运行时不支持此过程。欲了解更多信息,请参阅 Cypher 手册 → 并行运行时

详情

语法

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

描述

将 Cypher 语句中给定的 NODERELATIONSHIP 值导出到提供的 GraphML 文件。

输入参数

名称

类型

描述

statement

STRING

用于收集导出数据的查询。

file

STRING

数据将导出到的文件名称。

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 }

返回参数

名称

类型

描述

file

STRING

数据导出的文件名称。

source

STRING

导出数据的摘要。

format

STRING

文件导出的格式。

nodes

INTEGER

导出的节点数量。

relationships

INTEGER

导出的关系数量。

properties

INTEGER

导出的属性数量。

time

INTEGER

导出持续时间。

rows

INTEGER

返回的行数。

batchSize

INTEGER

导出运行时批处理的大小。

batches

INTEGER

导出运行时批处理的次数。

done

BOOLEAN

导出是否成功运行。

data

ANY

导出返回的数据。

配置参数

此过程支持以下配置参数

配置选项
参数 类型 默认值 描述

stream

BOOLEAN

false

在查询结果中返回数据,而不是将其存储到文件中。

compression

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

'NONE'

允许获取二进制数据,可不压缩(值:NONE)或压缩(其他值)。

timeoutSeconds

INTEGER

100

查询在超时前应运行的最长时间(秒)。

charset

STRING

'UTF_8'

当前使用 JDK 中扩展 java.nio.Charset 的字符名称。例如:US-ASCII, ISO-8859-1, UTF-8, UTF-16

useTypes

BOOLEAN

false

将类型添加到文件头。

format

STRING

'cypher-shell'

导出格式。

source

MAP

false

target 配合使用,用于导入仅包含关系的文件。在这种情况下,edge 标签的 source 和 target 属性不基于节点的内部 ID,而是基于自定义属性值。
例如,在路径 (:Foo {name: "aaa"})-[:KNOWS]→(:Bar {age: 666}) 中,KNOWS 关系可以通过配置 <edge id="e2" source="aaa" sourceType="string" target="666" targetType="long" label="KNOWS"><data key="label">KNOWS</data><data key="id">1</data></edge> 导出。请注意附加的 sourceType/targetType,用于在导入时检测正确的类型。

target

MAP

100

source 配合使用,用于导入仅包含关系的文件。在这种情况下,edge 标签的 source 和 target 属性不基于节点的内部 ID,而是基于自定义属性值。
例如,在路径 (:Foo {name: "aaa"})-[:KNOWS]→(:Bar {age: 666}) 中,KNOWS 关系可以通过配置 <edge id="e2" source="aaa" sourceType="string" target="666" targetType="long" label="KNOWS"><data key="label">KNOWS</data><data key="id">1</data></edge> 导出。请注意附加的 sourceType/targetType,用于在导入时检测正确的类型。

caption

LIST<STRING>

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

允许获取二进制数据,可不压缩(值:NONE)或压缩(其他值)。

nodesOfRelationships

BOOLEAN

false

在找到的节点之间添加缺失的关系。

使用示例

本节示例基于以下示例图

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);

下面的 Neo4j Browser 可视化显示了导入的图

play movies
图 1. 电影图可视化

apoc.export.graphml.query 过程将 Cypher 查询结果导出到 CSV 文件或作为流输出。

以下查询将所有 DIRECTED 关系以及该关系两侧带有 PersonMovie 标签的节点导出到文件 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;
结果
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

以下显示 movies-directed.csv 的内容

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>

以下查询返回所有 DIRECTED 关系以及该关系两侧带有 PersonMovie 标签的节点的流

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;
结果
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>
  "
© . This site is unofficial and not affiliated with Neo4j, Inc.