使用cypher查询在neo4j中提取子图

时间:2017-02-01 09:52:17

标签: java neo4j

我正在使用neo4j 3.1和java 8,我想提取一个连接的子图,将其存储为测试数据库。 有可能这样做吗? 如何使用Return返回输出。因此,我必须创建新的节点和关系,或者只是导出子图并将其放入新的数据库中。

我如何提取连接的子图,因为我有一个断开连接的图。

谢谢

2 个答案:

答案 0 :(得分:1)

这有两个部分......获取连接的子图,然后找到一种导出方法。

APOC Procedures似乎可以涵盖这两者。 this answer中使用路径扩展器的方法应该可以获得连接子图中的所有节点(如果关系类型不重要,请不要使用relationshipFilter参数)。

下一步是获取所有这些节点之间的所有关系。 APOC的graph algorithms section中的apoc.algo.cover()函数应该可以实现这一点。

这样的事情(假设这是在子图查询之后,subgraphNode在不同子图节点的列的范围内):

...
WITH COLLECT(subgraphNode) as subgraph, COLLECT(id(subgraphNode)) as ids
CALL apoc.algo.cover(ids) YIELD rel
WITH subgraph, COLLECT(rel) as rels
...

现在您已经拥有了子图中节点和关系的集合,您可以将它们导出。

APOC程序提供several means of exporting,从CSV到CypherScript。您应该能够找到适合您的选项。

答案 1 :(得分:0)

您还可以使用HttpClient httpClient = HttpClientBuilder.create().build(); HttpGetWithEntity e = new HttpGetWithEntity(); e.setURI(new URI(yourURL)) e.setEntity(yourEntity); HttpResponse response = httpclient.execute(e); 将查询结果提取到文件中,并使用同一文件在neo4j数据库中重新导入:

neo4j-shell

检查文件

ikwattro@graphaware-team ~/d/_/310> ./bin/neo4j-shell -c 'dump MATCH (n:Product)-[r*2]->(x) RETURN n, r, x;' > result.cypher

使用此文件提供另一个neo4j:

ikwattro@graphaware-team ~/d/_/310> cat result.cypher
begin
commit
begin
create (_1:`Product` {`id`:"product123"})
create (_2:`ProductInformation` {`id`:"product123EXCEL"})
create (_3:`ProductInformationElement` {`id`:"product123EXCELtitle", `key`:"title", `value`:"Original Title"})
create (_5:`ProductInformationElement` {`id`:"product123EXCELproduct_type", `key`:"product_type", `value`:"casual_bag"})
create (_1)-[:`PRODUCT_INFORMATION`]->(_2)
create (_2)-[:`INFORMATION_ELEMENT`]->(_3)
create (_2)-[:`INFORMATION_ELEMENT`]->(_5)
;
commit