• 在Neo4j中删除节点中多个属性的方法


    译者言:本文介绍了如何批量删除节点的属性的方法,重点介绍了apoc.create.removeProperties 函数的使用。

    今天早些时候,Irfan和我在一个数据集上做实验,运行了一些图形算法的程序,结果节点上多了一些属性,所以,现在我要想办法清理这些属性。

    现在的数据情况,可以用下面的语句模拟出来:

    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

    对于其他要删除的属性只要重复这个命令就好,但是这个过程可有点痛苦啊----那么这个过程能自动化吗?当然可以,接下来我们就来看看如何自动化这个过程。

    首先,我们需要将每个节点的属性除了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中有没有相应的方法,所以,我输入“remove”在APOC库中进行搜索。具体语句如下:

    CALL dbms.procedures() YIELD name, signature, descriptionWHERE name starts with "apoc" and description contains "remove"return name, signature, description

    运行语句显示下图: 

    我看到apoc.create.removeProperties方法,应该就是删除属性的,我们先来试一下:

    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        |+----------+

    我们再来看一下节点上还有哪些属性:

    neo4j> MATCH (n:Node)       RETURN keys(n) AS keys;+----------+| keys     |+----------+| ["name"] || ["name"] || ["name"] |+----------+

    哦耶,太好了。

    虽然属性删除了,但是我还是想再优化一下,看是否能够不在每个节点上调用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 propertiesMATCH (n:Node)WITH collect(n) AS nodes, propertiesCALL apoc.create.removeProperties(nodes, properties)YIELD nodeRETURN count(*)

    更多的数据

    删除小规模数据节点上的属性时,上面的语句是可以正常工作的。但是在大型数据节点上去删除属性时,要怎么做呢?这时apoc.periodic.iterate是你最好的选择。

    下面我们先来创建100万个准备删除属性的节点:

    CALL apoc.periodic.iterate(  "UNWIND range(0, 1000000) AS id RETURN id",  "CREATE (:Node {name: 'name-' + id, pagerank: 2.302, louvain: 1, lpa: 4 })", {})

    现在我们稍微修改一下前面的删除语句,将删除除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     |+---------+

    最后,我们再来确认一下,所有属性是否都删除了?

    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/

    译者言:最后欢迎各位同行留言交流译文中的不足。


    原文地址:https://www.jianshu.com/p/f9dfa0d513ca
  • 相关阅读:
    VERSIONINFO Resource
    WCF 学习文摘
    hook 学习
    COM 学习
    ActiveX开发
    Word 开发资料集合
    Loops with PL/SQL
    TWain 在 Qt4 中的调用方法
    从 TWAIN 设备中扫描图像
    Qt enum使用总结
  • 原文地址:https://www.cnblogs.com/jpfss/p/11428664.html
Copyright © 2020-2023  润新知