加载 GEXF (Graph Exchange XML Format)

许多现有应用和数据集成使用 GEXF 来描述带有节点和边的图。欲了解更多信息,请访问官方文档

可以使用过程 apoc.load.gexfapoc.import.gexf 从 GEXF 文件加载或导入节点和关系。您需要

  • 提供 GEXF 文件的路径

  • 提供配置(可选)

apoc.import.gexf 的读取方式与 apoc.load.gexf 相同,但也会在 Neo4j 中创建节点和关系。

对于从文件读取,您需要启用配置选项

apoc.import.file.enabled=true

默认情况下文件路径是全局的,对于相对于 import 目录的路径,请设置

apoc.import.file.use_neo4j_config=true

apoc.load.gexf 的示例

load.gexf
<?xml version="1.0" encoding="UTF-8"?>
<gexf version="1.2">
  <graph defaultedgetype="directed">
    <nodes>
      <node foo="bar">
        <attvalues>
          <attvalue for="0" value="http://gephi.org"/>
        </attvalues>
      </node>
    </nodes>
  </graph>
</gexf>
CALL apoc.load.gexf('load.gexf')
表格 1. 结果

{_type: gexf, _children: [{_type: graph, defaultedgetype: directed, _children: [{_type: nodes, _children: [{_type: node, _children: [{_type: attvalues, _children: [{_type: attvalue, for: 0, value: http://gephi.org}]}], foo: bar}]}]}], version: 1.2}

apoc.import.gexf 的示例

除了文件之外,您还可以传入配置映射

表格 2. 配置参数
名称 类型 默认值 描述

readLabels

Boolean

false

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

defaultRelationshipType

String

RELATED

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

storeNodeIds

Boolean

false

存储节点元素的 id 属性

batchSize

Integer

20000

每个事务处理的元素数量

compression

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

null

允许接收二进制数据,未压缩(值:NONE)或已压缩(其他值)

source

Map<String,String>

空映射

参见下面的 source / target 配置参数

target

Map<String,String>

空映射

参见下面的 source / target 配置参数 参见二进制文件示例

使用以下文件将创建

  • 1 个带有标签 Gephi 的节点

  • 2 个带有标签 Webatlas 的节点

  • 1 个带有标签 RTGI 的节点

  • 1 个带有标签 BarabasiLab 的节点

  • 6 个类型为 KNOWS 的关系

  • 1 个类型为 HAS_TICKET 的关系

  • 1 个类型为 BAZ 的关系

data.gexf
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://gexf.net/1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://gexf.net/1.3 http://gexf.net/1.3/gexf.xsd" version="1.2">
  <meta lastmodifieddate="2009-03-20">
    <creator>Gephi.org</creator>
    <description>A Web network</description>
  </meta>
  <graph defaultedgetype="directed">
    <attributes class="node">
      <attribute id="0" title="url" type="string"/>
      <attribute id="room" title="room" type="integer"/>
      <attribute id="projects" title="projects" type="long"/>
      <attribute id="price" title="price" type="double"/>
      <attribute id="1" title="indegree" type="float"/>
      <attribute id="members" title="members" type="liststring"/>
      <attribute id="pins" title="pins" type="listboolean"/>
      <attribute id="2" title="frog" type="boolean">
        <default>true</default>
      </attribute>
    </attributes>
    <attributes class="edge">
      <attribute id="score" title="score" type="float"/>
    </attributes>
    <nodes>
      <node id="0" label="Gephi">
        <attvalues>
          <attvalue for="0" value="http://gephi.org"/>
          <attvalue for="1" value="1"/>
          <attvalue for="room" value="10"/>
          <attvalue for="price" value="10.02"/>
          <attvalue for="projects" value="300"/>
          <attvalue for="members" value="[Altomare, Sterpeto, Lino]"/>
          <attvalue for="pins" value="[true, false, true, false]"/>
        </attvalues>
      </node>
       <node id="5" label="Gephi">
          <attvalues>
            <attvalue for="0" value="http://test.gephi.org"/>
            <attvalue for="1" value="2"/>
          </attvalues>
        </node>
      <node id="1" label="Webatlas">
        <attvalues>
          <attvalue for="0" value="http://webatlas.fr"/>
          <attvalue for="1" value="2"/>
        </attvalues>
      </node>
      <node id="2" label="RTGI">
        <attvalues>
          <attvalue for="0" value="http://rtgi.fr"/>
          <attvalue for="1" value="1"/>
        </attvalues>
      </node>
      <node id="3" label=":BarabasiLab:Webatlas">
        <attvalues>
          <attvalue for="0" value="http://barabasilab.com"/>
          <attvalue for="1" value="1"/>
          <attvalue for="2" value="false"/>
        </attvalues>
      </node>
    </nodes>
    <edges>
      <edge source="0" target="1" kind="KNOWS">
          <attvalues>
            <attvalue for="score" value="1.5"/>
          </attvalues>
      </edge>
      <edge source="0" target="0" kind="BAZ">
          <attvalues>
            <attvalue for="foo" value="bar"/>
            <attvalue for="score" value="2"/>
          </attvalues>
      </edge>
      <edge source="0" target="2" kind="HAS_TICKET">
          <attvalues>
            <attvalue for="ajeje" value="brazorf"/>
            <attvalue for="score" value="3"/>
          </attvalues>
      </edge>
      <edge source="0" target="2" kind="KNOWS" />
      <edge source="1" target="0" kind="KNOWS" />
      <edge source="2" target="1" kind="KNOWS" />
      <edge source="0" target="3" kind="KNOWS" />
      <edge source="5" target="3" kind="KNOWS" />
    </edges>
  </graph>
</gexf>
CALL apoc.import.gexf('data.gexf', {readLabels:true})
表格 3. 结果

{ "relationships" : 8, "batches" : 0, "file" : "file:/../data.gexf", "nodes" : 5, "format" : "gexf", "source" : "file", "time" : 9736, "rows" : 0, "batchSize" : -1, "done" : true, "properties" : 21 }

我们还可以通过执行以下操作来存储节点 ID

CALL apoc.import.gexf('data.gexf', {readLabels:true, storeNodeIds: true})

source / target 配置

允许在源节点和/或目标节点不存在于文件中的情况下导入关系,通过自定义标签和属性搜索节点。为此,我们可以在配置映射中插入 source: {label: '<MY_SOURCE_LABEL>', id: ’<MY_SOURCE_ID>'} 和/或 target: {label: '<MY_TARGET_LABEL>', id: ’<MY_TARGET_ID>'}。通过这种方式,我们可以通过 edge 标签的 sourcetarget 属性搜索起始节点和结束节点。

例如,使用配置映射 {source: {id: 'myId', label: 'Foo'}, target: {id: 'other', label: 'Bar'}} 以及像 <edge id="e0" source="n0" target="n1" label="KNOWS"><data key="label">KNOWS</data></edge> 这样的边行,我们搜索一个源节点 (:Foo {myId: 'n0'}) 和一个结束节点 (:Bar {other: 'n1'})id 键是可选的(默认为 'id')。

© . All rights reserved.