neo4j官方驅動支持Python語言,驅動程序主要包含Driver類型和Session類型。Driver對象包含Neo4j數據庫的詳細信息,包括主機url、安全驗證等配置,還管理着鏈接池(Connection Pool);Session對象是執行事務單元的邏輯上下文,事務是在Session的上下文中執行的。因爲Session不是線程安全的,並可以從Driver對象管理的鏈接池中回收利用(Recycle)鏈接,所以,Session對象是輕量級的(lightweight),用完以後應當即銷燬(disposable)。node
Driver對象和Session對象的關係是:Driver對象負責管理鏈接池,從鏈接池中分配鏈接建立Session對象;Session對象在單個線程中接收Cypher和啓動事務,在事務執行完成以後,當即銷燬Session對象;Driver對象負責回收鏈接,等待爲下一個Session對象分配鏈接。python
若是不關注驅動的版本,能夠安裝最新版本的Python驅動數據庫
pip install neo4j-driver
也能夠在pip命令中指定python驅動的版本:api
pip install neo4j-driver==$PYTHON_DRIVER_VERSION pip install neo4j-driver==1.4.0
在安裝neo4j驅動以後,在python代碼中導入GraphDatabase模塊,用於查詢和更新圖數據庫:緩存
from neo4j.v1 import GraphDatabase
1,建立Driver對象實例安全
輸入neo4j數據庫的uri,用戶的安全驗證,實例化Driver對象,並建立鏈接池:session
from neo4j.v1 import GraphDatabase uri = "bolt://localhost:7687" _driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
使用close()函數關閉Driver對象分配的任何鏈接:app
_driver.close()
2,使用Driver對象來建立Session對象ide
Driver對象從鏈接池中分配鏈接,建立Session對象:函數
_session = _driver.session()
Session的建立是一個輕量級的操做,因爲Session不是線程安全的,所以,Session一般應該在單個線程中短暫存續,用完以後當即銷燬。在Python中,推薦在with上下文中建立和銷燬Session對象:
def add_person(name): with _driver.session() as session: session.run("CREATE (a:Person {name: $name})", name=name)
Session對象是執行事務的邏輯上下文,Cypher支持兩種方式來提交事務。
1,以自動提交方式提交事務
以自動提交事務的方式執行Cypher查詢,在Session對象執行Cypher語句以後,事務當即提交,所以,一次事務只能執行一個Cyper查詢,返回的結果是StatementResult對象:
_session.run(statement, parameters=None)
2,以事務函數方式來提交事務
事務函數包含事務的工做單元,以事務函數方式提交事務是neo4j推薦的提交事務的方式,在事務函數方式中,一個事務能夠執行多個Cypher查詢。
首先,定義事務函數,傳遞相應的參數(Cypher語句和參數):
def create_person_node(tx, name): tx.run("CREATE (a:Person {name: $name}) RETURN id(a)", name=name)
而後,在Session對象中啓動寫事務(write_transaction)來調用事務函數,返回的結果是StatementResult對象:
def add_person(driver, name): with _driver.session() as session: # Caller for transactional unit of work return session.write_transaction(create_person_node, name)
Session對象執行Cypher查詢的結果是StatementResult類型,該類型其實是由Record對象構成的集合,該類型的經常使用函數以下:
Record類型是一個有序的Key/Value對的序列,這意味着,Record對象相似於由Key:Value構成的列表,Key字段的值能夠經過字段名稱或索引來訪問:
class BookmarksExample(object): def __init__(self, uri, user, password): self._driver = GraphDatabase.driver(uri, auth=(user, password)) def close(self): self._driver.close() # Create a person node. @classmethod def create_person(cls, tx, name): tx.run("CREATE (:Person {name: $name})", name=name) # Create an employment relationship to a pre-existing company node. # This relies on the person first having been created. @classmethod def employ(cls, tx, person_name, company_name): tx.run("MATCH (person:Person {name: $person_name}) " "MATCH (company:Company {name: $company_name}) " "CREATE (person)-[:WORKS_FOR]->(company)", person_name=person_name, company_name=company_name) # Create a friendship between two people. @classmethod def create_friendship(cls, tx, name_a, name_b): tx.run("MATCH (a:Person {name: $name_a}) " "MATCH (b:Person {name: $name_b}) " "MERGE (a)-[:KNOWS]->(b)", name_a=name_a, name_b=name_b) # Match and display all friendships. @classmethod def print_friendships(cls, tx): result = tx.run("MATCH (a)-[:KNOWS]->(b) RETURN a.name, b.name") for record in result: print("{} knows {}".format(record["a.name"] ,record["b.name"])) def main(self): saved_bookmarks = [] # To collect the session bookmarks # Create the first person and employment relationship. with self._driver.session() as session_a: session_a.write_transaction(self.create_person, "Alice") session_a.write_transaction(self.employ, "Alice", "Wayne Enterprises") saved_bookmarks.append(session_a.last_bookmark()) # Create the second person and employment relationship. with self._driver.session() as session_b: session_b.write_transaction(self.create_person, "Bob") session_b.write_transaction(self.employ, "Bob", "LexCorp") saved_bookmarks.append(session_b.last_bookmark()) # Create a friendship between the two people created above. with self._driver.session(bookmarks=saved_bookmarks) as session_c: session_c.write_transaction(self.create_friendship, "Alice", "Bob") session_c.read_transaction(self.print_friendships) class Neo4jProvider: def __init__(self, uri, user, password): self._driver = GraphDatabase.driver(uri, auth=(user, password)) def close(self): self._driver.close() def add_greeting_node(self, message): with self._driver.session() as session: session.write_transaction(self._create_greeting, message) @staticmethod def _create_greeting(tx, message): tx.run("CREATE (a:Greeting) SET a.message = $message ", message=message)
參考文檔: