【问题标题】:Importing CSV While Creating Relationship In Neo4j在 Neo4j 中创建关系时导入 CSV
【发布时间】:2017-12-06 23:56:14
【问题描述】:

我正在尝试使用 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"
}

在客户表 csv 中,我有:

Customer, ASIN, rating
A2FMUVHRO76A32, 0827229534, 5

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

【问题讨论】:

    标签: csv neo4j cypher


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2016-07-03
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多