Neo4j-列表字符串转数字列表

Neo4j-列表字符串转数字列表

  • 问题来自一个需求:用load csv导入neo4j时,数字列表变成了字符串,需要把它转回列表状态。

    由于手头没有原始数据,这里用其他数据库进行实验。

  • Neo4j版本:community-4.2

  • 时间:2022-10-21

方案一

  • 模拟一个列表字符串,保存为节点属性

    MATCH (n:IndustryChain{name:'主动元件'}) 
    set n.list = '[1,2,3,4]'
    
  • 尝试去掉字符串中的括号,然后拆分为列表

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with substring(n.list,1,7) as numstr,n
    with split(numstr,',') as nlist, n
    set n.list = nlist
    
  • 将列表中元素转为整型,修改节点属性为新列表

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with [num in n.list | toInteger(num) ] as numlist,n
    set n.list = numlist
    return n.list
    
  • 整合上述步骤

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with substring(n.list,1,7) as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    
  • 由于列表长度不固定,且没找到获取字符串长度的函数,用replace方式去除括号

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with replace(n.list, "[", "") as strL,n
    with replace(n.list, "]", "") as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    

方案二

  • 新命令:

    MATCH (n:IndustryChain{name:'主动元件'}) 
    with replace(n.list, "[", "") as strL,n
    with replace(n.list, "]", "") as numstr,n
    with split(numstr,',') as nlist, n
    with [num in nlist | toInteger(num) ] as numlist,n
    set n.list = numlist
    

相关知识:

  • String functions - Neo4j Cypher Manual
    • substring(n.list,1,7)
    • replace(n.list, “[”, “”)
    • split(numstr,‘,’)
  • Lists - Neo4j Cypher Manual
    • [num in nlist | toInteger(num) ]
  • SET - Neo4j Cypher Manual
    • set命令

你可能感兴趣的:(知识与图谱,neo4j,数据库,知识图谱)