使用APOC将CSV导入neo4j时处理空数组类型

问题描述 投票:0回答:1

我有一个csv文件,其中某些字段是数组类型。字段用,分隔,数组项用;分隔。例如:

index, name, friends, neighbors
0,Jim,John;Tim;Fred,Susan;Megan;Cheryl
1,Susan,Jim;John,Megan;Cheryl
2,Sean,,,

其中Jim有三个朋友JohnTimFred,以及三个邻居,SusanMeganCheryl,并且Sean没有朋友并且没有邻居。

但是,当我使用neo4j将其读入apoc.load.csv时,我得到的是带有空字符串(而不是空列表)的列表属性。例如:

CALL apoc.periodic.iterate("
CALL apoc.load.csv('file.csv',
    {header:true,sep:',',
    mapping:{
        friends:{array:true},
        neighbors:{array:true}}
    })
YIELD map as row RETURN row
","
CREATE (p:Person) SET p = row
", 
{batchsize:50000, iterateList:true, parallel:true});

给我一个名称为Person但名称为Seanfriends=[ "" ]neighbors=[ "" ]

我想要的是Sean具有friends=[]neighbors=[]

谢谢!

neo4j cypher neo4j-apoc
1个回答
0
投票
  1. 请确保CSV文件标题中没有多余的空格(否则某些属性名称将以空格开头:]

    index,name,friends,neighbors
    0,Jim,John;Tim;Fred,Susan;Megan;Cheryl
    1,Susan,Jim;John,Megan;Cheryl
    2,Sean,,,
    
  2. 使用list comprehension帮助消除所有为空字符串的friendsneighbors元素:

    CALL apoc.periodic.iterate(
      "CALL apoc.load.csv(
         'file.csv',
         {
           header:true, sep:',',
           mapping: {
             friends: {array: true},
             neighbors: {array: true}
           }
         }) YIELD map
       RETURN map
      ",
      "CREATE (p:Person)
       SET p = map
       SET p.friends = [f IN p.friends WHERE f <> '']
       SET p.neighbors = [n IN p.neighbors WHERE n <> '']
      ", 
      {batchsize:50000, iterateList:true, parallel:true}
    );
    

经过上述更改,此查询:

MATCH (person:Person) RETURN person;

返回此结果:

╒══════════════════════════════════════════════════════════════════════╕
│"person"                                                              │
╞══════════════════════════════════════════════════════════════════════╡
│{"name":"Jim","index":"0","neighbors":["Susan","Megan","Cheryl"],"frie│
│nds":["John","Tim","Fred"]}                                           │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Susan","index":"1","neighbors":["Megan","Cheryl"],"friends":[│
│"Jim","John"]}                                                        │
├──────────────────────────────────────────────────────────────────────┤
│{"name":"Sean","index":"2","neighbors":[],"friends":[]}               │
└──────────────────────────────────────────────────────────────────────┘

[更新]

此外,如果您的CSV文件不可能包含“空”朋友或邻居子字符串(例如John;;Fred),那么使用CASE而不是列表解析的此版本的查询会更有效:

CALL apoc.periodic.iterate(
  "CALL apoc.load.csv(
     'file.csv',
     {
       header:true, sep:',',
       mapping: {
         friends: {array: true},
         neighbors: {array: true, arraySep:';'}
       }
     }) YIELD map
   RETURN map
  ",
  "CREATE (p:Person)
     SET p = map
     SET p.friends = CASE p.friends WHEN [''] THEN [] ELSE p.friends END
     SET p.neighbors = CASE p.neighbors WHEN [''] THEN [] ELSE p.neighbors END
  ", 
  {batchsize:50000, iterateList:true, parallel:true}
);
© www.soinside.com 2019 - 2024. All rights reserved.