如何在Apache AGE中删除重复的顶点/边。

huangapple go评论59阅读模式
英文:

How to remove duplicate vertices/edges in Apache AGE

问题

如何在Apache AGE图数据库中删除重复的顶点/边?

例如,如果有两个标记为“User”的顶点具有相同的“name”属性值,我如何删除其中一个?

我希望查询适用于所有类型的顶点/边。

英文:

How can I remove duplicate vertices/edges in an Apache AGE graph database?

For example, if there are two vertices labeled "User" with the same "name" property value, how can I delete one of them?

I want the query to be applicable to all types of vertices/edges.

答案1

得分: 2

以下是翻译好的部分:

为了删除重复的顶点,您可以按照以下方式制定查询:

SELECT * FROM cypher('graph', $$
MATCH (u:User {name: 'user'}) WITH u SKIP 1 DELETE u
RETURN u
$$) AS (u agtype);

在这里,SKIP 1 允许我们跳过/保留第一行(也就是找到的第一个顶点),然后我们在剩下的顶点上应用 DELETE

同样地,为了删除重复的边,使用以下方式:

SELECT * FROM cypher('graph', $$
MATCH (u:User {name: 'user'})-[e:EDGE]->(v:User1 {name: 'user1'}) WITH e SKIP 1 DELETE e
RETURN e
$$) AS (e agtype);
英文:

In order to drop duplicate vertices, you can formulate a query as follows:

SELECT * FROM cypher('graph', $$ 
MATCH (u:User {name: 'user'}) WITH u SKIP 1 DELETE u
RETURN u $$) AS (u agtype);

Here SKIP 1 allows us to skip/leave the first row (in other words the first vertex found) and then we apply DELETE on the remaining vertices.

In the same way, in order to drop duplicate edges, use:

SELECT * FROM cypher('graph', $$ 
MATCH (u:User {name: 'user'})-[e:EDGE]->(v: User1 {name: 'user1'}) WITH e SKIP 1 DELETE e
RETURN e $$) AS (e agtype);

答案2

得分: 0

你可以使用Cypher查询来删除节点/边。

另一种方法是尝试合并节点,然后从合并的节点中删除不需要的数据。

如果对你有用,请告诉我。

英文:

You can use cypher queries to delete the nodes/edges.

Another way could be to try merging the nodes and then removing the data from the merged node you do not require.

Let me know if it works for you.

答案3

得分: 0

I don't think there is a way to match every duplicate and then delete them, you have to do it manually for each duplicate. For example:

postgres=# SELECT * FROM cypher('test_graph', $$
CREATE (u:user {name: 'user'})
RETURN u $$) AS (u agtype);
                                         u                                         
-----------------------------------------------------------------------------------
 {"id": 1125899906842625, "label": "user", "properties": {"name": "user"}}::vertex
(1 row)

postgres=# SELECT * FROM cypher('test_graph', $$
CREATE (u:user {name: 'user'})
RETURN u $$) AS (u agtype);
                                         u                                         
-----------------------------------------------------------------------------------
 {"id": 1125899906842626, "label": "user", "properties": {"name": "user"}}::vertex
(1 row)

这里我们创建了两个相同的顶点,但是你可以看到ID不同,所以当我们想要删除其中一个时,我们使用以下命令:

SELECT * FROM cypher('test_graph', $$
MATCH (u)
WHERE id = 1125899906842625
DELETE u $$) AS (u agtype);

通过这个命令,我们可以删除特定的顶点(或边)。

英文:

I dont think there is a way to match every duplicate and then delete them, you have to do it manually each dupliate. For example :

postgres=# SELECT * FROM cypher('test_graph', $$ 
CREATE (u:user {name: 'user'}) 
RETURN u $$) AS (u agtype);
                                         u                                         
-----------------------------------------------------------------------------------
 {"id": 1125899906842625, "label": "user", "properties": {"name": "user"}}::vertex
(1 row)

postgres=# SELECT * FROM cypher('test_graph', $$ 
CREATE (u:user {name: 'user'}) 
RETURN u $$) AS (u agtype);
                                         u                                         
-----------------------------------------------------------------------------------
 {"id": 1125899906842626, "label": "user", "properties": {"name": "user"}}::vertex
(1 row)

here we create to identical vertices but as you can see the ID is different so when we want to delete one of them we use :

SELECT * FROM cypher('test_graph', $$ 
MATCH (u) 
WHERE id = 1125899906842625 
DELETE u $$) AS (u agtype);

and with this we delete the specific vertex (or edge) that we want.

答案4

得分: 0

为了执行此操作,AGE 自动生成的 ID 是重要的特征。以下查询应该适用于不仅仅是一个情况,而是多种情况。

SELECT * 
FROM cypher('graph_name', $$
	MATCH (v:User)
    WHERE NOT id = VERTEX_ID
	DETACH DELETE v
$$) as (v agtype);

为了避免这种重复,应该使用 MERGE 子句。

根据这个链接,"MERGE 执行一个“选择或插入”操作,首先检查数据是否存在于数据库中。如果存在,则Cypher按原样返回它,或者进行您指定的现有节点或关系的任何更新。如果数据不存在,那么Cypher将根据您指定的信息创建它。"

英文:

To carry out this, the distinctive property which is the ID automatically generated by AGE is important. The following query should work for not just one case but several.

SELECT * 
FROM cypher('graph_name', $$
	MATCH (v:User)
    WHERE NOT id = VERTEX_ID
	DETACH DELETE v
$$) as (v agtype);

To avoid such duplicates, the MERGE clause should be utilised.

According to this, "MERGE does a "select-or-insert" operation that first checks if the data exists in the database. If it exists, then Cypher returns it as is or makes any updates you specify on the existing node or relationship. If the data does not exist, then Cypher will create it with the information you specify."

答案5

得分: 0

  1. 首先,您需要识别重复项,并为此任务使用以下代码:
MATCH (u:User)
WITH u.name AS name, collect(u) AS duplicates
WHERE size(duplicates) > 1
RETURN duplicates
  1. 其次,您需要决定哪些顶点或边需要保留。

  2. 现在,通过对上一个查询进行一些修改来删除顶点/边。

MATCH (u:User)
WITH u.name AS name, collect(u) AS duplicates
WHERE size(duplicates) > 1
WITH duplicates, max(id(u)) AS keepId
UNWIND duplicates AS duplicate
WHERE id(duplicate) <> keepId
DELETE duplicate
英文:
  1. First you need to identify the duplicates. and for this task Use this:

> MATCH (u:User)
>
> WITH u.name AS name, collect(u) AS duplicates
>
> WHERE size(duplicates) > 1
>
> RETURN duplicates

  1. Secondly you need to decide which vertices or edges need to be kept.

  2. Now delete the vertices/edges, by doing some modification to the previous query.

     MATCH (u:User)
    
     WITH u.name AS name,  collect(u) AS duplicates
    
     WHERE size(duplicates) &gt; 1
    
     WITH duplicates, max(id(u)) AS keepId
    
     UNWIND duplicates AS duplicate
    
     WHERE id(duplicate) &lt;&gt; keepId
    
     DELETE duplicate
    

答案6

得分: 0

To find duplicate vertices/edges depending on the property value, combine the MATCH clause with the GROUP BY and HAVING clauses.

使用MATCH子句与GROUP BY和HAVING子句结合以查找根据属性值的重复顶点/边。

MATCH (u:User)
WITH u.name AS name, COUNT(u) AS count
WHERE count > 1
RETURN name, count

一旦定位到重复项,您可以使用DELETE子句将它们删除,并使用MATCH子句选择重复的顶点和边。

一旦找到重复项,您可以使用DELETE子句将它们删除,并使用MATCH子句选择重复的顶点和边。

MATCH (u:User {name: 'dup_name'})
WITH u LIMIT 1
DELETE u

LIMIT 1子句确保仅删除一个重复的顶点。

英文:

To find duplicate vertices/edges depending on the property value, combine the MATCH clause with the GROUP BY and HAVING clauses.

MATCH (u:User)
WITH u.name AS name, COUNT(u) AS count
WHERE count &gt; 1
RETURN name, count

Once you have located the duplicates, you can use the DELETE clause to eliminate them and the MATCH clause to pick the duplicate vertices and edges.

MATCH (u:User {name: &#39;dup_name&#39;})  
WITH u LIMIT 1
DELETE u

The LIMIT 1 clause ensures that only one of the duplicate vertices is deleted.

huangapple
  • 本文由 发表于 2023年5月13日 22:14:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定