我使用LOAD
功能将我的CSV导入Neo4j。但是,我不确定如何将所有父类别映射到子类别。
这是示例CSV:
category_id,category,description,parent_id,is_adult
4,Electronics,Electronics,0,0
5,Audio Equipment,Audio Equipment,4,0
6,Home Entertainment,Home Entertainment,4,0
7,Photography,Photography,4,0
8,Portable Audio,Portable Audio,4,0
9,Televisions,Televisions,4,0
10,Amplifiers & Receivers,Amplifiers & Receivers,5,0
11,Audio Systems,Audio Systems,5,0
12,Cassette Decks,Cassette Decks,5,0
13,CD Players,CD Players,5,0
14,Radios,Radios,5,0
15,HiFi Speakers,HiFi Speakers,5,0
17,MiniDisc Separates,MiniDisc Separates,5,0
18,Tuners,Tuners,5,0
19,DJ Equipment,DJ Equipment,5,0
20,DVD Players,DVD Players,6,0
21,DVD Recorders,DVD Recorders,6,0
22,Headphones,Headphones,6,0
23,Home Cinema,Home Cinema,6,0
24,Projectors,Projectors,6,0
25,Remote Controls,Remote Controls,6,0
26,Set Top Boxes & Receivers,Set Top Boxes & Receivers,6,0
27,VCR Players,VCR Players,6,0
29,Camcorders,Camcorders,7,0
30,Accessories,Accessories,7,0
32,Cameras,Cameras,7,0
如您所见,category_id(11)有一个parent_id(5)
我正在使用的LOAD CSV命令是:
USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///tmp/categories.csv" AS row
CREATE (:Category {category_id: row.category_id, category: row.category, description: row.description, parent_id: row.parent_id, is_adult: row.is_adult});
我还没有使用is_adult(Boolean)的转换。
我对Neo4j很新,欢迎任何帮助。
答案 0 :(得分:1)
如果您提供了load csv
我不确定您在导入数据时在节点上的标签方式中提供了什么,但是此示例应该找到尚未与数据中存在父类别的父类别匹配的子项(即更大)比等于4)。然后它应该基于父节点上的父节点匹配节点,并在子节点和父节点之间创建:CHILD_OF
关系。
match (child:Category)
where not (child-[:CHILD_OF]->())
and child.parent_id >= 4
with child
match (parent:Category)
where parent.category_id = child.parent_id
create child-[:CHILD_OF]->parent
答案 1 :(得分:1)
我使用了您的CSV文件并在Neo4j中创建了一个数据库,然后使用以下创建关系:
MATCH (child:Category) WHERE child.parent_id <> 0
MATCH (parent:Category {category_id: child.parent_id})
CREATE (child)-[:CHILD_OF]->(parent)
RETURN null;
我看到你已经选择了第一个(也是正确的)答案,但这是给猫皮肤的另一种方式。