python - 使用 py2neo 将数据从 XML 加载到 neo4j

标签 python xml neo4j py2neo

我正在尝试使用 py2neo 将数据从 xml 文件加载到 Neo4j 数据库

这个 python 脚本工作正常,但速度太慢,因为我首先添加节点,然后添加与两个异常处理程序的关系。除此之外,XML 文件大小约为 200MB。

我想知道是否有更快的方法来执行此任务?

XML 文件:

<Persons>
    <person>
        <id>XA123</id>
        <first_name>Adam</first_name>
        <last_name>John</last_name>
        <phone>01-12322222</phone>
    </person>
    <person>
        <id>XA7777</id>
        <first_name>Anna</first_name>
        <last_name>Watson</last_name>
        <relationship>
            <type>Friends</type>
            <to>XA123</to>
        </relationship>
    </person>
</Persons>

Python 脚本:

#!/usr/bin/python3

from xml.dom import minidom
from py2neo import Graph, Node, Relationship, authenticate


graph = Graph("http://localhost:7474/db/data/")
authenticate("localhost:7474", "neo4j", "admin")

xml_file = open("data.xml")
xml_doc = minidom.parse(xml_file)
persons = xml_doc.getElementsByTagName('person')

# Adding Nodes
for person in persons:
    ID_ = person.getElementsByTagName('id')[0].firstChild.data
    fName = person.getElementsByTagName('first_name')[0].firstChild.data
    lName = person.getElementsByTagName('last_name')[0].firstChild.data

    # not every person has phone number
    try:
        phone = person.getElementsByTagName('phone')[0].firstChild.data
    except IndexError:
        phone = "None"

    label = "Person"
    node = Node(label, ID=ID_, LastName=fName, FirstName=lName, Phone=phone)
    graph.create(node)


# Adding Relationships
for person in persons:
    ID_ = person.getElementsByTagName('id')[0].firstChild.data

    label = "Person"
    node1 = graph.find_one(label, property_key="ID", property_value=ID_)

    # relationships
    try:
        has_relations = person.getElementsByTagName('relationship')
        for relation in has_relations:
            node2 = graph.find_one(label,
                                   property_key="ID",
                                   property_value=relation.getElementsByTagName('to')[0].firstChild.data)

            relationship = Relationship(node1,
                                        relation.getElementsByTagName('type')[0].firstChild.data, node2)
            graph.create(relationship)
    except IndexError:
        continue

最佳答案

通过对特定标签使用独特的属性约束,将数据加载到 Neo4j 所需的时间显着减少。

graph.cypher.execute("CREATE CONSTRAINT ON (n:Person) ASSERT n.ID IS UNIQUE")

关于python - 使用 py2neo 将数据从 XML 加载到 neo4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022525/

相关文章:

java - dumpToString 中的一个奇怪的类转换异常

Neo4j 节点或关系支持 ttl 吗?

Neo4j 2.2.0 社区尝试启动 shell

python - 通过python过滤函数传递参数

python - 如何在 Python 中向 YAML 文件添加注释

python - Django Admin,使用自定义函数排序

java - 无法映射第二类 : ** org. hibernate.MappingException:表员工中的关联引用了未映射的类**

python - python切片中 "a[:,1]"的含义是什么

java - 为什么我的 JAR 的 Maven 依赖项没有包含在我的 WAR 中?

java - 如何使用 XML Modifier 保存不断更新并最终将输出保存为文件