美文网首页知识与图谱
Neo4j-列表字符串转数字列表

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

作者: 王十三 | 来源:发表于2022-10-21 13:39 被阅读0次

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
    

相关知识:

相关文章

网友评论

    本文标题:Neo4j-列表字符串转数字列表

    本文链接:https://www.haomeiwen.com/subject/occmzrtx.html