简体   繁体   English

在Neo4j中创建关系时导入CSV

[英]Importing CSV While Creating Relationship In Neo4j

I am trying to create a relationship between two different graphs, using information in a CSV file. 我正在尝试使用CSV文件中的信息在两个不同的图形之间创建关系。 I built the query the way I did because the size of each graph, one being 500k+ and the other 1.5m+. 我以这种方式构建查询,因为每个图的大小(一个为500k +,另一个为1.5m +)。

This is the query I have: 这是我的查询:

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)

This is the error I receive: 这是我收到的错误:

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

An example of the Main table is: 主表的示例是:

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

And an example of a customer is: 一个客户的例子是:

{
  "customer": "A2FMUVHRO76A32"
}

And inside the customers table csv, I have: 在客户表csv中,我有:

Customer, ASIN, rating
A2FMUVHRO76A32, 0827229534, 5

I can't seem to figure out why it's throwing back that error. 我似乎无法弄清楚为什么它会抛出该错误。

The first WITH clause in your query ( WITH row ) is unnecessary, but you have to add the variable to the WITH clause. 查询中的第一个WITH子句( WITH row )是不必要的,但是您必须将变量添加到WITH子句中。 So this version compiles. 因此,此版本进行了编译。

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)

The reason for this is, that, in essence, WITH chains two query parts together, while limiting the scope to its variables (and in some cases, also performing calculations, aggregations, etc.). 这样做的原因是,从本质上讲, WITH两个查询部分链接在一起,同时将范围限制在其变量之内(在某些情况下,还执行计算,汇总等)。

Having said that, you do not even need the second WITH clause, you can just omit it and even merge the two MATCH clauses to a single one: 话虽如此,您甚至不需要第二个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM