简体   繁体   中英

Importing CSV While Creating Relationship In Neo4j

I am trying to create a relationship between two different graphs, using information in a CSV file. I built the query the way I did because the size of each graph, one being 500k+ and the other 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:

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. 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.).

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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