【问题标题】:Creating nodes and Realtionships in Neo4j Dynamically在 Neo4j 中动态创建节点和关系
【发布时间】:2019-05-25 19:58:46
【问题描述】:

我有一个如下目录结构

C:\Users\Shiva\Desktop\Internship\shiva.txt

我想为每个文件夹创建节点,并且我想要它们之间的关系。

示例图表将是这样的

C <-contains-> Users <-contains-> Shiva <-contains-> desktop <-contains-> 

Internship <-contains-> shiva.txt

我将以动态方式传递路径。

【问题讨论】:

  • 您能分享一下您到目前为止所做的事情吗?以及您面临的问题是什么?
  • 我已经在那个链接中分享了......我想像上面提到的那样为相邻列表创建节点
  • createListForFiles(dirName) - 它将返回所有路径
  • Pathlist - ['maven_project', 'maven-simple-master', 'src', 'pom.xml'] 拥有 createrootGraph(pathlist): Hve created node,...现在我想建立关系

标签: python-3.x neo4j py2neo


【解决方案1】:

我想一个解决方案可能是使用 Python 的 os.walk()

这个函数可以这样使用:

from os import walk

def add_node(root, elem):
    query = 'MATCH (r: Directory {{name: "{0}"}})\
                CREATE (e: Directory {{name: "{1}")\
                CREATE (r)-[l: CONTAINS]->(e)\
                RETURN id(e), id(l)'.format(root, elem)
    # Run the query with your driver instance here

# Add the node for root here

for root, dirs, files in os.walk(your_dynamic_path, topdown = False):
    for dir in dirs:
        add_node(root, dir)
    for file in files:
        add_node(root, file)

add_node(root, elem) 是一个函数,将节点 elem 添加/合并到 Neo4J 图形并添加所需的关系。

【讨论】:

  • 我想在他们之间建立关系
  • 看来我无法将代码放入 cmets,所以我将更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多