譯者言:本文介紹了如何批量刪除節(jié)點(diǎn)的屬性的方法次伶,重點(diǎn)介紹了apoc.create.removeProperties 函數(shù)的使用。
今天早些時候帆卓,Irfan和我在一個數(shù)據(jù)集上做實(shí)驗(yàn)巨朦,運(yùn)行了一些圖形算法的程序,結(jié)果節(jié)點(diǎn)上多了一些屬性剑令,所以糊啡,現(xiàn)在我要想辦法清理這些屬性。
現(xiàn)在的數(shù)據(jù)情況吁津,可以用下面的語句模擬出來:
CREATE (:Node {name: "Mark", pagerank: 2.302, louvain: 1, lpa: 4 })
CREATE (:Node {name: "Michael", degree: 23, triangles: 12, betweeness: 48.70 })
CREATE (:Node {name: "Ryan", eigenvector: 2.302, scc: 1, unionFind: 4 })
除了name屬性悔橄,其他都是算法生成的,我要怎樣才能刪除這些生成的屬性呢?
我可以一次刪除一個屬性癣疟,像下面這樣:
MATCH (n:Node)
REMOVE n.pagerank
對于其他要刪除的屬性只要重復(fù)這個命令就好挣柬,但是這個過程可有點(diǎn)痛苦啊----那么這個過程能自動化嗎?當(dāng)然可以睛挚,接下來我們就來看看如何自動化這個過程邪蛔。
首先,我們需要將每個節(jié)點(diǎn)的屬性除了name,都放到一個列表中扎狱。查詢語句如下:
neo4j> MATCH (n:Node)
WITH [k in keys(n) where not k in ["name"]] as keys
RETURN keys;
+---------------------------------------+
| keys |
+---------------------------------------+
| ["lpa", "pagerank", "louvain"] |
| ["betweeness", "degree", "triangles"] |
| ["unionFind", "eigenvector", "scc"] |
+---------------------------------------+
接下來我們再來嘗試刪除這些屬性侧到,這次我們的查詢語句這么寫:
neo4j> MATCH (n:Node)
WITH n, [k in keys(n) where not k in ["name"]] as keys
UNWIND keys AS key
REMOVE n[key];
Invalid input '[': expected an identifier character, whitespace, node labels, 'u/U', '{', 'o/O', a property map, a relationship pattern, '.' or '(' (line 4, column 9 (offset: 103))
"REMOVE n[key];"
啊偶~, 出錯了.....趕快看看APOC是不是有刪除的方法淤击,我也不知道APOC中有沒有相應(yīng)的方法匠抗,所以,我輸入“remove”在APOC庫中進(jìn)行搜索污抬。具體語句如下:
CALL dbms.procedures() YIELD name, signature, description
WHERE name starts with "apoc" and description contains "remove"
return name, signature, description
運(yùn)行語句顯示下圖:?
我看到apoc.create.removeProperties
方法汞贸,應(yīng)該就是刪除屬性的,我們先來試一下:
neo4j> MATCH (n:Node)
WITH n, [k in keys(n) where not k in ["name"]] as keys
CALL apoc.create.removeProperties(n, keys) YIELD node
RETURN count(*);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
我們再來看一下節(jié)點(diǎn)上還有哪些屬性:
neo4j> MATCH (n:Node)
RETURN keys(n) AS keys;
+----------+
| keys |
+----------+
| ["name"] |
| ["name"] |
| ["name"] |
+----------+
哦耶印机,太好了矢腻。
雖然屬性刪除了,但是我還是想再優(yōu)化一下射赛,看是否能夠不在每個節(jié)點(diǎn)上調(diào)用keys方法多柑,而直接傳一個除name之外的屬性列表。我們可以先通過下面的語句得到除name之外的所有屬性:
neo4j> CALL db.propertyKeys() YIELD propertyKey WHERE propertyKey <> 'name'
RETURN collect(propertyKey);
+-----------------------------------------------------------------------------------------+
| collect(propertyKey) |
+-----------------------------------------------------------------------------------------+
| ["degree", "pagerank", "louvain", "lpa", "triangles", "betweeness", "scc", "unionFind"] |
+-----------------------------------------------------------------------------------------+
如果我要把那個屬性列表傳到apoc.create.removeProperties中楣责,只要像下面這樣就可以了:
CALL db.propertyKeys() YIELD propertyKey WHERE propertyKey <> 'name'
WITH collect(propertyKey) AS properties
MATCH (n:Node)
WITH collect(n) AS nodes, properties
CALL apoc.create.removeProperties(nodes, properties)
YIELD node
RETURN count(*)
更多的數(shù)據(jù)
刪除小規(guī)模數(shù)據(jù)節(jié)點(diǎn)上的屬性時竣灌,上面的語句是可以正常工作的。但是在大型數(shù)據(jù)節(jié)點(diǎn)上去刪除屬性時秆麸,要怎么做呢帐偎?這時apoc.periodic.iterate
是你最好的選擇。
下面我們先來創(chuàng)建100萬個準(zhǔn)備刪除屬性的節(jié)點(diǎn):
CALL apoc.periodic.iterate(
"UNWIND range(0, 1000000) AS id RETURN id",
"CREATE (:Node {name: 'name-' + id, pagerank: 2.302, louvain: 1, lpa: 4 })", {})
現(xiàn)在我們稍微修改一下前面的刪除語句蛔屹,將刪除除name之外的所有屬性:
neo4j> CALL db.propertyKeys() YIELD propertyKey WHERE propertyKey <> 'name'
WITH collect(propertyKey) AS properties
CALL apoc.periodic.iterate(
"MATCH (n:Node) RETURN n",
"WITH collect(n) AS nodes
CALL apoc.create.removeProperties(nodes, $properties)
YIELD node
RETURN count(*)",
{params: {properties: properties}})
YIELD batches
RETURN batches;
+---------+
| batches |
+---------+
| 101 |
+---------+
最后削樊,我們再來確認(rèn)一下,所有屬性是否都刪除了兔毒?
neo4j> MATCH (n:Node)
RETURN keys(n), count(*);
+---------------------+
| keys(n) | count(*) |
+---------------------+
| ["name"] | 1000001 |
+---------------------+
OK,完美漫贞!
原文鏈接: https://markhneedham.com/blog/2019/03/14/neo4j-delete-dynamic-properties/
譯者言:最后歡迎各位同行留言交流譯文中的不足。