如何在neo4j中建立csv列之间的关系?

3wabscal  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(168)

我有三个csv文件:
示例_用户.csv

user_id, first_name, location

样品订单.csv

order_id, user_id, product,order_ts

样品_产品.csv

product_id, product

我成功地建立了sample_users与sample_orders**but now I want to get the product_id of each product ordered in the sample_orders table.**之间的关系,但我失败了。没有错误,但关系没有发生。哪里出了问题?

//load user nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row 
MERGE(u:User {user_id:row.user_id, name:row.first_name, location:row.location})
RETURN count(u);

//load order nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_orders.csv' AS row WITH row WHERE row.order_id IS NOT NULL 
MERGE(o:Orders {order_id:row.order_id, order_ts:row.order_ts, user_id_2:row.user_id, product:row.product})
RETURN count(o);

//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row WITH row where row.p_name IS NOT NULL
MERGE(p:Products {product_id:row.p_id, product_name:row.p_name})
RETURN count(p);

//Create relationships 
LOAD CSV WITH HEADERS FROM 'file:///sample_users.csv' AS row 
MATCH(u:User{user_id:row.user_id})
MATCH(o:Orders{user_id_2:row.user_id})
MERGE(u)-[:HAS_ORDERED]->(o)
RETURN *;

LOAD CSV WITH HEADERS FROM 'file:///sample_products_v2.csv' AS row
MATCH(o:Orders{product:row.product})
MATCH(p:Products{product_name:row.p_name})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;
waxmsbnn

waxmsbnn1#

您的脚本中存在打字错误。您的“产品”列名为product_id,product。
这应该是加载脚本:

//load product nodes
LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row WITH row where row.product IS NOT NULL
MERGE(p:Products {product_id:row.product_id, product_name:row.product})
RETURN count(p);

因此,您应该使用

LOAD CSV WITH HEADERS FROM 'file:///sample_products.csv' AS row
MATCH(o:Orders{product:row.product_id})
MATCH(p:Products{product_name:row.product})
MERGE(p)-[:IS_ITEM]->(o)
RETURN *;

另外,通过运行以下查询,检查是否在Products.product_id中找到Orders.product:

MATCH (o:Orders) 
MATCH (p:Products) WHERE p.product_id = o.product
RETURN n LIMIT 5

如果查询未返回任何结果,则表示您的订单和产品不匹配

相关问题