在Neo4j中创建关系时导入CSV

时间:2017-12-06 23:56:14

标签: csv neo4j cypher

我正在尝试使用CSV文件中的信息在两个不同的图表之间创建关系。我按照我的方式构建查询,因为每个图的大小,一个是500k +,另一个是1.5m +。

这是我的查询:

LOAD CSV WITH HEADERS FROM "file:///customers_table.csv" AS row WITH row
MATCH (m:Main) WITH m
MATCH (c:Customers) USING INDEX c:Customers(customer)
WHERE m.ASIN = row.asin AND c.customer = row.customer
CREATE (c)-[:RATED]->(m)

这是我收到的错误:

Variable `row` not defined (line 4, column 16 (offset: 164))
"WHERE m.ASIN = row.asin AND c.customer = row.customer"
                ^

主表的一个例子是:

{
  "ASIN": "0827229534",
  "totalreviews": "2",
  "categories": "2",
  "title": "Patterns of Preaching: A Sermon Sampler",
  "avgrating": "5",
  "group": "Book"
}

客户的一个例子是:

{
  "customer": "A2FMUVHRO76A32"
}

在customers表csv中,我有:

Customer, ASIN, rating
A2FMUVHRO76A32, 0827229534, 5

我似乎无法弄清楚为什么它会抛出这个错误。

1 个答案:

答案 0 :(得分:1)

查询中的第一个WITH子句(WITH row)是不必要的,但您必须将该变量添加到WITH子句中。所以这个版本编译。

LOAD CSV WITH HEADERS FROM "file:///customers_table.csv" AS row
MATCH (m:Main)
WITH m, row
MATCH (c:Customers) USING INDEX c:Customers(customer)
WHERE m.ASIN = row.asin AND c.customer = row.customer
CREATE (c)-[:RATED]->(m)

原因是,实际上,WITH将两个查询部分链接在一起,同时将范围限制为其变量(在某些情况下,还执行计算,聚合等)。

话虽如此,你甚至不需要第二个WITH子句,你可以省略它,甚至将两个MATCH子句合并为一个:

LOAD CSV WITH HEADERS FROM "file:///customers_table.csv" AS row
MATCH (m:Main), (c:Customers) USING INDEX c:Customers(customer)
WHERE m.ASIN = row.asin AND c.customer = row.customer
CREATE (c)-[:RATED]->(m)