一次遍历两个列表

时间:2019-10-22 19:18:27

标签: neo4j cypher

我输入的CSV格式如下

email,skills,expertiseLevels
john1@xyz.com,"Oracle database;SSIS;SQL Server","5;4;3"
john2@xyz.com,"Python;Hadoop;SQL Server","1;2;4"

每行 expertiseLevels [i] 表示该人在技能[i]

方面的专业知识

我想编写一个Cypher查询以获取如下数据集:

╒════════════════════════════════════╤═════════════════╤════════════════╕
│"email"                             │"skill"          │"expertiseLevel"│
╞════════════════════════════════════╪═════════════════╪════════════════╡
│"john1@xyz.com"                     │"Oracle database"│"5"             │
├────────────────────────────────────┼─────────────────┼────────────────┤
│"john1@xyz.com"                     │"SSIS"           │"4"             │
├────────────────────────────────────┼─────────────────┼────────────────┤
│"john1@xyz.com"                     │"SQL Server"     │"3"             │
├────────────────────────────────────┼─────────────────┼────────────────┤
│"john2@xyz.com"                     │"Python"         │"1"             │
├────────────────────────────────────┼─────────────────┼────────────────┤
│"john2@xyz.com"                     │"Hadoop"         │"2"             │
├────────────────────────────────────┼─────────────────┼────────────────┤
│"john2@xyz.com"                     │"SQL Server"     │"4"             │
└────────────────────────────────────┴─────────────────┴────────────────┘

我目前拥有的查询确实有效,只是我想知道是否有更直接的方法来完成我要寻找的东西:

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS line
WITH line.email AS email, split(line.skills,";") AS skills, split(line.expertiseLevels,";") AS expertiseLevels
WITH email, reduce(x = "", idx in range(0,size(skills)-1) | x + skills[idx] + ":" + expertiseLevels[idx] + ";") AS se
WITH email, split(se,";") AS skillsWithExpertise
UNWIND skillsWithExpertise AS skillWithExpertise
WITH email AS email, split(skillWithExpertise,":") AS tokens
WHERE skillWithExpertise <> ""
WITH email, tokens[0] AS skill, tokens[1] AS expertiseLevel
RETURN email, skill, expertiseLevel;

谢谢

1 个答案:

答案 0 :(得分:2)

这将产生您想要的输出:

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS line
WITH
  line.email AS email,
  SPLIT(line.skills, ";") AS skills,
  SPLIT(line.expertiseLevels, ";") AS expertiseLevels
UNWIND RANGE(0, SIZE(skills)-1) AS i
RETURN email, skills[i] AS skill, expertiseLevels[i] AS expertiseLevel