简体   繁体   English

使用密码查询在neo4j中提取子图

[英]Extract subgraph in neo4j using cypher query

I'm using neo4j 3.1 with java 8 and I want to extract a connected subgraph as to store it as a test database.我在 java 8 中使用 neo4j 3.1,我想提取一个连接的子图以将其存储为测试数据库。 Is it possible to do it and how?是否有可能做到这一点以及如何做到? How to do it with the clause Return which returns the output.如何使用返回输出的子句 Return 来做到这一点。 So, I had to create new nodes and relations or just export the subgraph and put it in a new database.因此,我必须创建新的节点和关系,或者只是导出子图并将其放入新数据库中。

How can I extract a connected subgraph since I have a disconnected graph.由于我有一个断开连接的图,我如何提取一个连接的子图。

Thank you谢谢

There are two parts to this...getting the connected subgraph, and then finding a means to export.这有两个部分......获取连接的子图,然后找到导出的方法。

APOC Procedures seems like it can cover both of these. APOC 程序似乎可以涵盖这两个方面。 The approach in this answer using the path expander should get you all the nodes in the connected subgraph (if the relationship type doesn't matter, leave off the relationshipFilter parameter).此答案中使用路径扩展器的方法应该为您提供连接子图中的所有节点(如果关系类型无关紧要,请不要使用 relationshipFilter 参数)。

The next step is to get all relationships between all of those nodes.下一步是获取所有这些节点之间的所有关系。 APOC's apoc.algo.cover() function in the graph algorithms section should accomplish this. 图算法部分中的 APOC 的 apoc.algo.cover() 函数应该可以实现这一点。

Something like this (assuming this is after the subgraph query, and subgraphNode is in scope for the column of distinct subgraph nodes):像这样(假设这是在子图查询之后,并且subgraphNode在不同子图节点列的范围内):

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

Now that you have the collections of both the nodes and relationships in the subgraph, you can export them.现在您在子图中拥有节点和关系的集合,您可以导出它们。

APOC Procedures offers several means of exporting , from CSV to CypherScript. APOC Procedures 提供了多种导出方式,从 CSV 到 CypherScript。 You should be able to find an option that works for you.您应该能够找到适合您的选项。

You can also use the neo4j-shell to extract the result of a query to a file and use this same file to re-import it in the neo4j database :您还可以使用neo4j-shell将查询结果提取到文件中,并使用相同的文件将其重新导入到 neo4j 数据库中:

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

check the file检查文件

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

Use this file for feeding another neo4j :使用此文件提供另一个 neo4j :

ikwattro@graphaware-team ~/d/_/310> ./bin/neo4j-shell -file result.cypher
Transaction started
Transaction committed
Transaction started
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 4
Relationships created: 3
Properties set: 8
Labels added: 4
52 ms
Transaction committed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM