2013-04-29 58 views
0

我會寫一個巨大的圖形到neo4j。使用我的代碼將花費略少於兩個月的時間。py2neo:創建圖形時最小化寫入時間

我把數據從Kaggle's events recommendation challenge,該user_friends.csv文件我使用看起來像

user,friends 
3197468391,1346449342 3873244116 4226080662, ... 

我用py2neo batch設施產生的代碼。這是我能做的最好的,還是有另一種顯着縮短運行時間的方法?

這裏的代碼

#!/usr/bin/env python 

from __future__ import division 
from time import time 
import sqlite3 
from py2neo import neo4j 

graph = neo4j.GraphDatabaseService("http://localhost:7474/db/data/") 
batch = neo4j.WriteBatch(graph) 

people = graph.get_or_create_index(neo4j.Node,"people") 
friends = graph.get_or_create_index(neo4j.Relationship,"friends") 

con = sqlite3.connect("test.db") 
c = con.cursor() 
c.execute("SELECT user, friends FROM user_friends LIMIT 2;") 

t=time() 
for u_f in c: 
    u_node = graph.get_or_create_indexed_node("people",'name',u_f[0]) 

    for f in u_f[1].split(" "): 
     f_node = graph.get_or_create_indexed_node("people",'name', f) 

     if not f_node.is_related_to(u_node, neo4j.Direction.BOTH,"friends"): 
      batch.create((u_node,'friends',f_node)) 

    batch.submit() 
print time()-t 

我也無法找到一個方法來創建一個使用高級別py2neo設施無向圖?我知道cypher可以做這個有點像create (node(1) -[:friends]-node(2))

在此先感謝。

回答

1

YOu應該創建連接,而不是Direction.BOTH。選擇一個方向,然後在遍歷時忽略使用Direction.BOTH - 它沒有性能影響,但關係方向是確定性的。當你說a--b時,塞普爾確實如此。