bulk write in py2neo from a list

时间:2017-02-02 14:32:48

标签: neo4j cypher py2neo

I have the following data, which represents the distances between two objects.

data = [[('123','234'), 10],
        [('134','432'), 12],

       ]

I would like to insert this into neo4j via py2neo v3:

for e, p in enumerate(data):
    #
    id_left  = p[0][0]
    id_right = p[0][1]    
    distance = p[1]
    #
    left  = Node("_id", id_left)
    right = Node("_id", id_right)
    G.merge(left)
    G.merge(right)
    r = Relationship(left,'TO', right, distance=distance)
    G.create(r)
    #

But I find this to be very, very slow. What's the best of speeding this up? I've looked around but haven't found any code example that illustrates clearly how to go about it

1 个答案:

答案 0 :(得分:11)

显然您使用错误的py2neo来创建节点,您当前的代码产生以下内容: 如您所见,您为Node对象提供的第一个参数是标签,第二个参数应该是属性的映射。 这很慢,因为MERGE无法匹敌。 这是代码的更正版本,它将使用标签MyNode和属性ID: 来自py2neo import Graph,Node,Relationship graph =图表(密码="密码") data = [     [(' 123',' 234'),10],     [(' 134',' 432'),12], ] 对于e,p在枚举(数据)中:     #     id_left = p [0] [0]     id_right = p [0] [1]     距离= p [1]     #     left = Node(" MyNode",id = id_left)     right = Node(" MyNode",id = id_right)     graph.merge(左)     graph.merge(右)     r =关系(左,' TO',右,距离=距离)     graph.create(r)的 这将产生以下图表: 对于大多数性能,当您开始拥有数千个MyNode节点时,可以在id属性上添加唯一约束: CREATE CONSTRAINT ON(m:MyNode)ASSERT m.id IS UNIQUE; 现在这段代码正在对Neo4j进行3次调用,性能最高的是直接使用cypher: data = [     [(' 123',' 234'),10],     [(' 134',' 432'),12], ] params = [] 对于数据中的x:     params.append({" left&#34 ;: x [0] [0]," right":x [0] [1]," distance":x [ 1]}) q =""" UNWIND {datas} AS数据 MERGE(m:MyNode {id:data.left}) MERGE(m2:MyNode {id:data.right}) MERGE(m) - [r:TO] - >(m2) SET r.distance = data.distance """ graph.run(q,{" datas":params}) 这将导致与上面相同的图形。