一、CSV数据文件的准备
什么是csv文件
CSV文件是一种纯文本文件,用于存储表格和电子表格信息。它以行为单位组织数据,每行数据由一系列字段组成,字段之间通常使用逗号进行分隔(也可以使用其他字符,如制表符或分号)。CSV文件因其简单易懂、易于编辑和处理的特点,被广泛应用于数据交换和存储,特别是在不同系统或应用程序之间导入导出数据时。(来源于百度搜索)
excel文件怎么转变为csv文件
excel文件----->另存为-----> 文件格式选择csv文件即可。
text文件怎么导入到excel
今天在操作的时候意外发现了一个txt文件导入excel并按照txt文件中的分割符将文本内容分隔都不同的列的方法。
选择导入数据,然后会出现如下弹窗:
接下来找到自己需要导入excel的txt文件,并按分隔符进行分割即可。
二、导入CSV文件
将CSV文件放置在neo4j数据库的数据目录下
经测试只能放在import目录下,放在别的目录下会出错,提示找不到资源。
导入用户节点的CSV文件
LOAD CSV WITH HEADERS FROM "file:///users.csv" AS row
CREATE (:User {userID: row.userID, name: row.name})
导入产品节点
LOAD CSV WITH HEADERS FROM "file:///products.csv" AS row CREATE (:Product {productID: row.productID, name: row.name, category: row.category, price: toFloat(row.price)})
导入用户和产品之间的购买关系
LOAD CSV WITH HEADERS FROM "file:///user_product.csv" AS row
MATCH (u:User {userID:row.userID})
MATCH (p:Product {productID:row.productID})
CREATE (u)-[:PURCHASED]->(p);
执行上述代码后,提示no changes no records,暂时还没找到具体的原因,该怎么办?
最终使用比较笨的办法,建立用户节点和产品节点之间的关系,具体如下:
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product1"}) CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product2"}) CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product1"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product2"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product4"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product2"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product4"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product4"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product5"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product1"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product2"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product4"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product1"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product2"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product5"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product1"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product4"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"0product2"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"0product5"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product11"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product12"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product11"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product12"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product14"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product12"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product14"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product14"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product15"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product11"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product20"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product14"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product11"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product20"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product15"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product10"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product14"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"0product12"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"0product15"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product9"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product7"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user1"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product19"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product17"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user2"}),(p:Product{productID:"product18"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product16"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product15"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user3"}),(p:Product{productID:"product13"})CREATE(u)-[:PURCHASED]->(p))
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product17"})CREATE(u)-[:PURCHASED]->(p))
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product20"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user4"}),(p:Product{productID:"product10"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product7"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user5"}),(p:Product{productID:"product9"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product8"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product10"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user6"}),(p:Product{productID:"product16"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product17"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user7"}),(p:Product{productID:"product18"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product20"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product3"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user8"}),(p:Product{productID:"product5"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product10"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product9"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user9"}),(p:Product{productID:"product7"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user10"}),(p:Product{productID:"product8"})CREATE(u)-[:PURCHASED]->(p)
MATCH(u:User{userID:"user10"}),(p:Product{productID:"product6"})CREATE(u)-[:PURCHASED]->(p)
在执行的过程中发现,一次只能执行一条语句,执行多条的话,会提示错误。
看来对neo4j的了解还是太少了,还需要更多的努力呀。
原文链接:https://blog.csdn.net/QH2107/article/details/129658674
创建用户和产品之间的购买关系,这个使用MATCH
和CREATE
命令来实现