用python操作和管理ArangoDB

來源:互聯網
上載者:User

標籤:開啟   mil   com   bat   fetchall   nbsp   custom   特定   ast   

目錄:
  • 串連資料庫
  • 建立資料庫/集合/文檔
  • 檢索篩選
  • 更新
  • 刪除
  • 調用AQL的方法
安裝需要用到的python包:

pip install pyarango

一、串連資料庫:
>>> from pyArango.connection import *>>> conn = Connection(username="root", password="root_passwd")

 當該代碼執行時,它會初始化 conn 變數上的伺服器串連。預設情況下,pyArango會嘗試建立與http://127.0.0.1:8529的串連。

二、建立資料庫/集合/文檔建立和開啟資料庫方法:

createDatabase()

該方法可以在伺服器上開啟或建立資料庫,當要串連的資料庫不存在時,pyArango會在伺服器上建立它。當它存在時,pyArango會嘗試開啟資料庫。

>>> db = conn.createDatabase(name="school")

也可以使用其名稱作為伺服器串連上的鍵來開啟現有資料庫:

>>> db = conn["school"]>>> dbArangoDB database: school
建立集合方法:

createCollection()

>>> studentsCollection = db.createCollection(name="Students")>>> db["Students"]ArangoDB Collection name: Students, id: 202, type: document, status loaded
建立文檔方法:

createDocument()

>>> doc1 = studentsCollection.createDocument()>>> doc1["name"] = "John Smith">>> doc1ArangoDoc ‘None‘: {‘name‘: ‘John Smith‘}>>> doc2 = studentsCollection.createDocument()>>> doc2["firstname"] = "Emily">>> doc2["lastname"] = "Bronte">>> doc2ArangoDoc ‘None‘: {‘firstname‘: ‘Emily‘, ‘lastname‘: ‘Bronte‘} 

因為尚未將其儲存到ArangoDB,所以該文檔顯示其 _id 為“None”。這意味著該變數存在於您的Python代碼中,但不存在於資料庫中。 ArangoDB 通過將集合名稱與 __key 值進行配對來構造 _id 值。

儲存文檔:
>>> doc1._key = "johnsmith">>> doc1.save()>>> doc1ArangoDoc ‘Students/johnsmith‘: {‘name‘: ‘John Smith‘}
迴圈輸入資料:
>>> students = [(‘Oscar‘, ‘Wilde‘, 3.5), (‘Thomas‘, ‘Hobbes‘, 3.2), ... (‘Mark‘, ‘Twain‘, 3.0), (‘Kate‘, ‘Chopin‘, 3.8), (‘Fyodor‘, ‘Dostoevsky‘, 3.1), ... (‘Jane‘, ‘Austen‘,3.4), (‘Mary‘, ‘Wollstonecraft‘, 3.7), (‘Percy‘, ‘Shelley‘, 3.5), ... (‘William‘, ‘Faulkner‘, 3.8), (‘Charlotte‘, ‘Bronte‘, 3.0)]>>> for (first, last, gpa) in students:...    doc = studentsCollection.createDocument()...    doc[‘name‘] = "%s %s" % (first, last)...    doc[‘gpa‘] = gpa ...    doc[‘year‘] = 2017...    doc._key = ‘‘.join([first, last]).lower() ...    doc.save()
三、檢索篩選查看某一個特定學生的GPA:
>>> def report_gpa(document):...    print("Student: %s" % document[‘name‘])...    print("GPA:     %s" % document[‘gpa‘])>>> kate = studentsCollection[‘katechopin‘]>>> report_gpa(kate)Student: Kate ChopinGPA:     3.8
篩選平均成績在3.5以上的學生:方法:

fetchAll()

>>> def top_scores(col, gpa):...    print("Top Soring Students:")...    for student in col.fetchAll():...       if student[‘gpa‘] >= gpa:...          print("- %s" % student[‘name‘])>>> top_scores(studentsCollection, 3.5)Top Scoring Students:- Mary Wollstonecraft - Kate Chopin- Percy Shelly- William Faulkner- Oscar Wilde
四、更新

可以定義一個特定的函數來處理更新:

>>> def update_gpa(key, new_gpa):...    doc = studentsCollection[key]...    doc[‘gpa‘] = new_gpa...    doc.save()
五、刪除方法:

delete()

>>> tom = studentsCollection["thomashobbes"]>>> tom.delete()>>> studentsCollection["thomashobbes"]KeyError: (   ‘Unable to find document with _key: thomashobbes‘, {      ‘code‘: 404,      ‘errorNum‘: 1202,      ‘errorMessage‘: ‘document Students/thomashobbes not found‘,      ‘error‘: True})
六、調用AQL的方法

除了上面顯示的Python方法之外,ArangoDB還提供了一種查詢語言(稱為AQL),用於檢索和修改資料庫上的文檔。在pyArango中,您可以使用 AQLQuery() 方法執行這些查詢。

檢索所有文檔的_key:

>>> aql = "FOR x IN Students RETURN x._key">>> queryResult = db.AQLQuery(aql, rawResults=True, batchSize=100)>>> for key in queryResult:...    print(key)marywollstonecraftkatechopinpercyshelleyfyodordostoevskymarktwain...

 

 

 

參考資料:

https://www.arangodb.com/tutorials/cn-tutorial-python/

 

用python操作和管理ArangoDB

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.