我希望使用Neo4j将很多文件名导入图形数据库。数据来自外部来源,并以CSV文件格式提供。我想根据数据创建树形结构,以便以后可以在查询中轻松地“导航”该结构(即查找某个目录下的所有文件,出现在多个目录中的所有文件等)。
因此,给出示例输入:
/foo/bar/example.txt
/bar/baz/another.csv
/example.txt
/foo/bar/onemore.txt
Run Code Online (Sandbox Code Playgroud)
我想要创建以下图形:
( / ) <-[:in]- ( foo ) <-[:in]- ( bar ) <-[:in]- ( example.txt )
<-[:in]- ( onemore.txt )
<-[:in]- ( bar ) <-[:in]- ( baz ) <-[:in]- ( another.csv )
<-[:in]- ( example.txt )
Run Code Online (Sandbox Code Playgroud)
(其中每个节点标签实际上是一个属性,例如path :)。
使用固定数量的目录级别时,我已经能够达到预期的效果;例如,当每个文件位于三个级别的深度时,我可以创建一个包含4列的CSV文件:
dir_a,dir_b,dir_c,file
foo,bar,baz,example.txt
foo,bar,ban,example.csv
foo,bar,baz,another.txt
Run Code Online (Sandbox Code Playgroud)
并使用密码查询将其导入:
LOAD CSV WITH HEADERS FROM "file:///sample.csv" AS row
MERGE (dir_a:Path {name: row.dir_a})
MERGE (dir_b:Path {name: row.dir_b}) <-[:in]- (dir_a)
MERGE (dir_c:Path {name: row.dir_c}) <-[:in]- (dir_b)
MERGE (:Path {name: row.file}) <-[:in]- (dir_c) …
Run Code Online (Sandbox Code Playgroud)