|
| 1 | +# python-arango |
| 2 | + |
| 3 | +Python driver for [ArangoDB](https://www.arangodb.com). |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +Python 3.6+ and ArangoDB 3.7+ |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```shell |
| 12 | +pip install python-arango |
| 13 | +``` |
| 14 | + |
| 15 | +## Getting Started |
| 16 | + |
| 17 | +Here is a simple usage example: |
| 18 | + |
| 19 | +```python |
| 20 | +from arango import ArangoClient |
| 21 | + |
| 22 | +# Initialize the client for ArangoDB. |
| 23 | +client = ArangoClient(hosts="http://localhost:8529") |
| 24 | + |
| 25 | +# Connect to "_system" database as root user. |
| 26 | +sys_db = client.db("_system", username="root", password="passwd") |
| 27 | + |
| 28 | +# Create a new database named "test". |
| 29 | +sys_db.create_database("test") |
| 30 | + |
| 31 | +# Connect to "test" database as root user. |
| 32 | +db = client.db("test", username="root", password="passwd") |
| 33 | + |
| 34 | +# Create a new collection named "students". |
| 35 | +students = db.create_collection("students") |
| 36 | + |
| 37 | +# Add a hash index to the collection. |
| 38 | +students.add_hash_index(fields=["name"], unique=True) |
| 39 | + |
| 40 | +# Insert new documents into the collection. |
| 41 | +students.insert({"name": "jane", "age": 39}) |
| 42 | +students.insert({"name": "josh", "age": 18}) |
| 43 | +students.insert({"name": "judy", "age": 21}) |
| 44 | + |
| 45 | +# Execute an AQL query and iterate through the result cursor. |
| 46 | +cursor = db.aql.execute("FOR doc IN students RETURN doc") |
| 47 | +student_names = [document["name"] for document in cursor] |
| 48 | +``` |
| 49 | + |
| 50 | +Here is another example with graphs: |
| 51 | + |
| 52 | +```python |
| 53 | +from arango import ArangoClient |
| 54 | + |
| 55 | +# Initialize the client for ArangoDB. |
| 56 | +client = ArangoClient(hosts="http://localhost:8529") |
| 57 | + |
| 58 | +# Connect to "test" database as root user. |
| 59 | +db = client.db("test", username="root", password="passwd") |
| 60 | + |
| 61 | +# Create a new graph named "school". |
| 62 | +graph = db.create_graph("school") |
| 63 | + |
| 64 | +# Create vertex collections for the graph. |
| 65 | +students = graph.create_vertex_collection("students") |
| 66 | +lectures = graph.create_vertex_collection("lectures") |
| 67 | + |
| 68 | +# Create an edge definition (relation) for the graph. |
| 69 | +register = graph.create_edge_definition( |
| 70 | + edge_collection="register", |
| 71 | + from_vertex_collections=["students"], |
| 72 | + to_vertex_collections=["lectures"] |
| 73 | +) |
| 74 | + |
| 75 | +# Insert vertex documents into "students" (from) vertex collection. |
| 76 | +students.insert({"_key": "01", "full_name": "Anna Smith"}) |
| 77 | +students.insert({"_key": "02", "full_name": "Jake Clark"}) |
| 78 | +students.insert({"_key": "03", "full_name": "Lisa Jones"}) |
| 79 | + |
| 80 | +# Insert vertex documents into "lectures" (to) vertex collection. |
| 81 | +lectures.insert({"_key": "MAT101", "title": "Calculus"}) |
| 82 | +lectures.insert({"_key": "STA101", "title": "Statistics"}) |
| 83 | +lectures.insert({"_key": "CSC101", "title": "Algorithms"}) |
| 84 | + |
| 85 | +# Insert edge documents into "register" edge collection. |
| 86 | +register.insert({"_from": "students/01", "_to": "lectures/MAT101"}) |
| 87 | +register.insert({"_from": "students/01", "_to": "lectures/STA101"}) |
| 88 | +register.insert({"_from": "students/01", "_to": "lectures/CSC101"}) |
| 89 | +register.insert({"_from": "students/02", "_to": "lectures/MAT101"}) |
| 90 | +register.insert({"_from": "students/02", "_to": "lectures/STA101"}) |
| 91 | +register.insert({"_from": "students/03", "_to": "lectures/CSC101"}) |
| 92 | + |
| 93 | +# Traverse the graph in outbound direction, breadth-first. |
| 94 | +result = graph.traverse( |
| 95 | + start_vertex="students/01", |
| 96 | + direction="outbound", |
| 97 | + strategy="breadthfirst" |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +Please see [documentation](http://python-driver-for-arangodb.readthedocs.io/en/master/index.html) |
| 102 | +for more details. |
0 commit comments