目录:

安装需要用到的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"]
>>> db
ArangoDB database: school

创建集合

方法:

createCollection()

>>> studentsCollection = db.createCollection(name="Students")
>>> db["Students"]
ArangoDB Collection name: Students, id: , type: document, status loaded

创建文档

方法:

createDocument()

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

因为尚未将其保存到ArangoDB,所以该文档显示其 _id 为“None”。这意味着该变量存在于您的Python代码中,但不存在于数据库中。 ArangoDB 通过将集合名称与 __key 值进行配对来构造 _id 值。

保存文档:

>>> doc1._key = "johnsmith"
>>> doc1.save()
>>> doc1
ArangoDoc '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'] =
... 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 Chopin
GPA: 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': ,
'errorNum': ,
'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=)
>>> for key in queryResult:
... print(key)
marywollstonecraft
katechopin
percyshelley
fyodordostoevsky
marktwain
...

参考资料:

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

最新文章

  1. iOS开发系列--Swift语言
  2. ArcGIS API for JavaScript(2)-ArcGIS Server发布要素图层服务
  3. Pod(转)
  4. Socket通信代码(原理)
  5. Qt开发中的实用笔记三--关于各种类的零碎知识点:
  6. 【WP 8.1开发】自定义(RAW)通知的使用
  7. 解决Package illuminate/html is abandoned, you should avoid using it. Use laravelcollective/html instead.问题
  8. .Net免费公开课视频+资料+源码+经典牛逼 汇总篇【持续更新】
  9. c++ insert iterators 插入型迭代器
  10. Unix无缓冲文件操作函数、文件信息查询
  11. Android开发之应用程序窗体显示状态操作(requestWindowFeature()的应用)
  12. #JavaScript对象与继承
  13. ASP.NET在实际开发中验证码的用法
  14. Struts2-045验证脚本
  15. gradle 修改生成的apk的名字
  16. unity3d入门教程
  17. consoleWriter.go
  18. dubbo 使用zookeeper 出现 Dubbo客户端调用报错NullPointerException
  19. .Net 配置的简陋解决方案
  20. eclipse设置是否自动跳转切换到debug视图模式

热门文章

  1. java通过代理创建Conncection对象与自定义JDBC连接池
  2. java虚拟机体系分析
  3. 孙悟空的七十二变是那般?--java类型的七十二变揭秘
  4. Zookeeper学习笔记之 Zab协议(Zookeeper Atomic Broadcast)
  5. redis相关缓存知识
  6. 分库分表(6)--- SpringBoot+ShardingSphere实现分表+ 读写分离
  7. Redis实现分布式文件夹锁
  8. JAVA中JDK开发环搭的搭建,jvm jre
  9. App自动化环境搭建
  10. main(argc, char *argv[])