Neo4j:加载CSV文件并结合子字符串功能

时间:2017-12-11 22:23:39

标签: csv neo4j cypher

我正在尝试将一个* .csv文件加载到neo4j中并在同一个load语句中拆分该行(它没有分隔符,但是我需要创建节点的数据的设置位置)。我想使用substring函数,我无法弄清楚如何让它工作。数据以单行读入:

0067011990999991958051507004 + 68750 + 023550FM-12 + + 038299999V0203301N00671220001CN9999999N9 00001 + 99999999999

我尝试使用以下代码:

LOAD CSV WITH HEADERS FROM "file:/c:/itw/Ltemps.csv" AS line
WITH line
WHERE line.year IS split((substring(line, 15, 19))) and line.temp IS split((substring(line, 88, 92))) and line.qlfr IS split((substring(line, 87, 88))) and line.qual IS split((substring(line, 92, 93)))
MERGE (y:Year {year:line.year})
MERGE (t:Temp {temp:line.temp})
MERGE (f:Qlfr {qlfr:line.qlfr})
MERGE (q:Qual {qual:line.qual})
CREATE (y)-[r:HAS_TEMP]->(t);

我希望得到4个节点:年,temp(绝对值),限定符(正或负符号)和质量数。数据位于字符串中的索引应该是准确的。

1 个答案:

答案 0 :(得分:1)

首先,让我们尝试正确获取索引和类型。要将数字子串转换为整数,我们使用toInteger函数:

WITH '0067011990999991958051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999' AS line
RETURN
  toInteger(substring(line, 15, 4)) AS year,
  toInteger(substring(line, 88, 2)) AS temp,
  substring(line, 87, 1) AS qlfr,
  toInteger(substring(line, 92, 1)) AS qual

这给出了:

╒══════╤══════╤══════╤══════╕
│"year"│"temp"│"qlfr"│"qual"│
╞══════╪══════╪══════╪══════╡
│1958  │0     │"+"   │1     │
└──────┴──────┴──────┴──────┘

如果结果很好看,请添加LOAD CSV MERGE条款WITH HEADERS。两件事:

  • 我觉得使用row[0]没有意义,因为在这种情况下标题是无用的。只需加载该行,然后使用line作为MERGE进行拆分。
  • 通过将前两个MERGE子句与CREATE子句相结合,可以简化LOAD CSV FROM 'file:/c:/itw/Ltemps.csv' AS row WITH row[0] AS line WITH toInteger(substring(line, 15, 4)) AS year, toInteger(substring(line, 88, 2)) AS temp, substring(line, 87, 1) AS qlfr, toInteger(substring(line, 92, 1)) AS qual MERGE (y:Year {year: year})-[r:HAS_TEMP]->(t:Temp {temp: temp}) MERGE (f:Qlfr {qlfr: qlfr}) MERGE (q:Qual {qual: qual})

所以加载器代码如下:

full join