2016-11-21 17 views
0

我正在使用python腳本從SQL服務器檢索一些數據,並在neo4j服務器中創建節點。帶有Python驅動程序的Neo4j:創建的節點數遠遠少於預期?

我用一個while循環與Cypher語句來創建節點一個接一個。該循環運行〜37000次(這是SQL服務器中表的行數),所以我希望neo4j服務器上有多少個節點。但是,neo4j服務器上只有943個節點。請有任何想法嗎?

下面是代碼:

import pyodbc 
from neo4j.v1 import GraphDatabase, basic_auth 

# SQL part...# 
cursor.execute(sqlQuery) # retrieved data from SQL server.. 

print("let's connect to neo4j server....\n") 
driver = GraphDatabase.driver("bolt://192.168.1.1:7687", auth=basic_auth("neo4j", "neo4j")) 
session = driver.session() 
print("now you've connected to server... :) \n") 

j = int() 
row = cursor.fetchone() 
while row: 
    j = j + 1 
    msg = session.run("CREATE (:Person {name: '" + row[0] + "'});") # Cypher 
    row = cursor.fetchone() 

print("total nodes created:",j) 

回答

0

我試圖一個不同模式類運行Cypher支架聲明,這個時候一切都如我所料工作就好。

這個想法是創建一個事務來運行多個Cypher語句,然後在最後提交。這是我如何做的:

driver = GraphDatabase.driver("bolt://192.168.1.1:7687", auth=basic_auth("neo4j", "neo4j")) 
session = driver.session() 
print("now you've connected to server... :)") 

j = int() 
row = cursor.fetchone() 

with session.begin_transaction() as tx: 
    while row: 
     j = j + 1 
     msg = tx.run("CREATE (:Person {Name: {n}});", {"n": row[0]}) 
     row = cursor.fetchone() 
    tx.success = True # commit the cypher statements 

print("total nodes created:",j) 

現在,如果我回去的Neo4j服務器計算節點,數量會如我所料如出一轍。

還有一件事,我注意到Neo4j Bolt Driver for Python上的示例代碼是不正確的。而不是session.new_transaction()網站建議,我們應該使用session.begin_transaction()