加载 GEXF (图交换 XML 格式)
许多现有应用程序和数据集成使用 GEXF 来描述带有节点和边的图。如需了解更多信息,请访问官方文档。
可以通过 apoc.load.gexf 和 apoc.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 示例
<?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')
| 值 |
|---|
{_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 示例
除了文件之外,您还可以传入一个配置映射 (config map)
| 名称 (name) | type | 默认 | description(描述) |
|---|---|---|---|
readLabels |
布尔值 |
false |
根据 |
defaultRelationshipType |
字符串 |
RELATED |
如果 GraphML 文件中未指定关系类型,则使用的默认关系类型 |
storeNodeIds |
布尔值 |
false |
存储 |
batchSize |
整数 |
20000 |
每个事务处理的元素数量 |
compression |
|
|
允许接收二进制数据,可以是未压缩的(值: |
source |
Map<String,String> |
空映射 |
请参阅下方的 |
target(目标) |
Map<String,String> |
空映射 |
请参阅下方的 |
通过以下文件将创建:
-
1 个带有 Gephi 标签的节点
-
2 个带有 Webatlas 标签的节点
-
1 个带有 RTGI 标签的节点
-
1 个带有 BarabasiLab 标签的节点
-
6 个类型为 KNOWS 的关系
-
1 个类型为 HAS_TICKET 的关系
-
1 个类型为 BAZ 的关系
<?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})
| 值 |
|---|
{ "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 config
允许在源节点和/或目标节点不在文件中时导入关系,通过自定义标签和属性搜索节点。为此,我们可以在配置映射中插入 source: {label: '<MY_SOURCE_LABEL>', id: ’<MY_SOURCE_ID>'}` 和/或 source: {label: '<MY_TARGET_LABEL>', id: ’<MY_TARGET_ID>'}`。通过这种方式,我们可以通过 edge 标签的 source 和 target 属性来搜索开始节点和结束节点。
例如,使用配置映射 {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')。