导入 GraphML

GraphML 被其他工具(如 Gephi 和 CytoScape)用于读取图数据。

可用过程

下表描述了可用过程

限定名称 类型

apoc.import.graphml apoc.import.graphml(urlOrBinaryFile ANY, config MAP<STRING, ANY>) - 从提供的 GraphML 文件导入图。

过程

配置参数

该过程支持以下配置参数

表 1. 配置选项
参数 默认值 描述

readLabels

false

根据 `node` 元素的 `labels` 属性中的值创建节点标签

defaultRelationshipType

RELATED

如果在 GraphML 文件中未指定关系类型,则使用的默认关系类型

storeNodeIds

false

存储 `node` 元素的 `id` 属性

batchSize

20000

每个事务要处理的元素数量

示例

本节包含演示如何使用 GraphML 导入过程的示例。

导入简单的 GraphML 文件

`simple.graphml` 文件包含来自 GraphML 快速入门 的图表示。

apoc.import.graphml.simple diagram
simple.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">
  <graph id="G" edgedefault="undirected">
    <node id="n0"/>
    <node id="n1"/>
    <node id="n2"/>
    <node id="n3"/>
    <node id="n4"/>
    <node id="n5"/>
    <node id="n6"/>
    <node id="n7"/>
    <node id="n8"/>
    <node id="n9"/>
    <node id="n10"/>
    <edge source="n0" target="n2"/>
    <edge source="n1" target="n2"/>
    <edge source="n2" target="n3"/>
    <edge source="n3" target="n5"/>
    <edge source="n3" target="n4"/>
    <edge source="n4" target="n6"/>
    <edge source="n6" target="n5"/>
    <edge source="n5" target="n7"/>
    <edge source="n6" target="n8"/>
    <edge source="n8" target="n7"/>
    <edge source="n8" target="n9"/>
    <edge source="n8" target="n10"/>
  </graph>
</graphml>
以下基于 `simple.graphml` 导入图
CALL apoc.import.graphml("http://graphml.graphdrawing.org/primer/simple.graphml", {})

如果我们运行此查询,将看到以下输出

表 2. 结果
文件 来源 格式 节点 关系 属性 时间 batchSize 批次 完成 数据

"http://graphml.graphdrawing.org/primer/simple.graphml"

"file"

"graphml"

11

12

0

618

0

-1

0

TRUE

NULL

我们也可以将 `simple.graphml` 复制到 Neo4j 的 `import` 目录,然后从那里导入文件。如果采用这种方法,我们需要在 `apoc.conf` 中添加以下条目

要从文件读取,我们需要启用以下配置选项

apoc.conf
apoc.import.file.enabled=true

然后我们可以按以下方式运行导入过程

以下基于 `simple.graphml` 导入图
CALL apoc.import.graphml("file://simple.graphml", {})

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

apoc.import.graphml.simple
图 1. 简单图可视化

导入由 Export GraphML 过程创建的 GraphML 文件

`movies.graphml` 包含 Neo4j 电影图的一个子集,由 导出 GraphML 过程 生成。

movies.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="roles" for="edge" attr.name="roles"/>
<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="n189" labels=":Person"><data key="labels">:Person</data><data key="born">1964</data><data key="name">Keanu Reeves</data></node>
<node id="n190" labels=":Person"><data key="labels">:Person</data><data key="born">1967</data><data key="name">Carrie-Anne Moss</data></node>
<node id="n191" labels=":Person"><data key="labels">:Person</data><data key="born">1961</data><data key="name">Laurence Fishburne</data></node>
<node id="n192" labels=":Person"><data key="labels">:Person</data><data key="born">1960</data><data key="name">Hugo Weaving</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>
<node id="n195" labels=":Person"><data key="labels">:Person</data><data key="born">1952</data><data key="name">Joel Silver</data></node>
<edge id="e267" source="n189" target="n188" label="ACTED_IN"><data key="label">ACTED_IN</data><data key="roles">["Neo"]</data></edge>
<edge id="e268" source="n190" target="n188" label="ACTED_IN"><data key="label">ACTED_IN</data><data key="roles">["Trinity"]</data></edge>
<edge id="e269" source="n191" target="n188" label="ACTED_IN"><data key="label">ACTED_IN</data><data key="roles">["Morpheus"]</data></edge>
<edge id="e270" source="n192" target="n188" label="ACTED_IN"><data key="label">ACTED_IN</data><data key="roles">["Agent Smith"]</data></edge>
<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>
<edge id="e273" source="n195" target="n188" label="PRODUCED"><data key="label">PRODUCED</data></edge>
</graph>
</graphml>
以下基于 `movies.graphml` 导入图
CALL apoc.import.graphml("movies.graphml", {})

如果我们运行此查询,将看到以下输出

表 3. 结果
文件 来源 格式 节点 关系 属性 时间 batchSize 批次 完成 数据

"movies.graphml"

"file"

"graphml"

8

7

36

23

0

-1

0

TRUE

NULL

我们可以运行以下查询来查看导入的图

MATCH p=()-->()
RETURN p
表 4. 结果
p

({name: "Laurence Fishburne", born: "1961", labels: ":Person"})-[:ACTED_IN {roles: "[\"Morpheus\"]", label: "ACTED_IN"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", labels: ":Movie"})

({name: "Carrie-Anne Moss", born: "1967", labels: ":Person"})-[:ACTED_IN {roles: "[\"Trinity\"]", label: "ACTED_IN"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", la bels: ":Movie"})

({name: "Lana Wachowski", born: "1965", labels: ":Person"})-[:DIRECTED {label: "DIRECTED"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", labels: ":Movie"})

({name: "Joel Silver", born: "1952", labels: ":Person"})-[:PRODUCED {label: "PRODUCED"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", labels: ":Movie"})

({name: "Lilly Wachowski", born: "1967", labels: ":Person"})-[:DIRECTED {label: "DIRECTED"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", labels: ":Movie"})

({name: "Keanu Reeves", born: "1964", labels: ":Person"})-[:ACTED_IN {roles: "[\"Neo\"]", label: "ACTED_IN"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", labels: ": Movie"})

({name: "Hugo Weaving", born: "1960"})-[:ACTED_IN {roles: "[\"Agent Smith\"]", label: "ACTED_IN"}]→({tagline: "Welcome to the Real World", title: "The Matrix", released: "1999", la bels: ":Movie"})

GraphML 文件中定义的标签已添加到每个节点的 `labels` 属性中,而不是作为节点标签添加。我们可以设置配置属性 `readLabels: true` 来导入原生标签

以下基于 `movies.graphml` 导入图并存储节点标签
CALL apoc.import.graphml("movies.graphml", {readLabels: true})
表 5. 结果
文件 来源 格式 节点 关系 属性 时间 batchSize 批次 完成 数据

"movies.graphml"

"file"

"graphml"

8

7

21

23

0

-1

0

TRUE

NULL

现在让我们重新运行查询以查看导入的图

MATCH p=()-->()
RETURN;
表 6. 结果
p

(:Person {name: "Lilly Wachowski", born: "1967"})-[:DIRECTED]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Carrie-Anne Moss", born: "1967"})-[:ACTED_IN {roles: "[\"Trinity\"]"}]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Hugo Weaving", born: "1960"})-[:ACTED_IN {roles: "[\"Agent Smith\"]"}]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Laurence Fishburne", born: "1961"})-[:ACTED_IN {roles: "[\"Morpheus\"]"}]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Keanu Reeves", born: "1964"})-[:ACTED_IN {roles: "[\"Neo\"]"}]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Joel Silver", born: "1952"})-[:PRODUCED]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

(:Person {name: "Lana Wachowski", born: "1965"})-[:DIRECTED]→(:Movie {tagline: "Welcome to the Real World", title: "The Matrix", released: "1999"})

© . All rights reserved.