Using Python to manipulate and manage Arangodb

Source: Internet
Author: User

Directory:
    • Connecting to a database
    • Create a database/collection/document
    • Search Filter
    • Update
    • Delete
    • Methods for calling the AQL
Install the Python package that you need to use:

Pip Install Pyarango

First, connect the database:
>>> from pyarango.connection import *>>> conn = connection (username="root" , password="root_passwd")

When the code executes, it initializes conn the server connection on the variable. By default, Pyarango attempts to establish a connection to http://127.0.0.1:8529.

ii. Creating databases/collections/DocumentsCreate and open a databaseMethod:

CreateDatabase ()

The method can open or create a database on the server, and Pyarango creates it on the server when the database to which it is connected does not exist. When it is present, Pyarango tries to open the database.

>>> db = Conn.createdatabase (name="school")

You can also open an existing database by using its name as a key on the server connection:

>>> db = conn["school"]>>> dbarangodb database:school 
Create a collectionMethod:

CreateCollection ()

>>> studentscollection = db.createcollection (name="Students")> >> db["Students"ID202, type:document, status Loaded
Create a documentMethod:

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'}

Because it has not been saved to Arangodb, the document shows it _id as "None". This means that the variable exists in your Python code, but does not exist in the database. Arangodb constructs a value by pairing the collection name with the __key value _id .

To Save the document:
" JohnSmith ">>> doc1.save ()>>>'students/johnsmith' : {'name''John Smith'}
Loop input data:
>>> 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)inchStudents: ... doc=studentscollection.createdocument () ... doc['name'] ="%s%s"% (First, Last) ... doc['GPA'] =GPA ... doc[' Year'] = .. .. doc._key="'.Join([First, Last]). Lower () ... doc.save ()
Iii. Search and filterto view the GPA of a particular student:
>>>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
screening students with average scores above 3.5:Method:

Fetchall ()

>>>def top_scores (col, GPA): ... print ("Top soring Students:")...     forStudentinchCol.fetchall (): ...ifstudent['GPA'] >=GPA: ... print ("-%s"% student['name'])>>> Top_scores (Studentscollection,3.5) Top scoring Students:-Mary Wollstonecraft-Kate Chopin-Percy Shelly-William Faulkner-Oscar Wilde
iv. Update

You can define a specific function to handle the update:

>>> def update_gpa (Key, New_gpa): ...     = Studentscollection[key] ...    doc['gpa'= New_gpa ...    Doc.save ()
v. DeleteMethod:

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})
Vi. methods for calling the AQL

In addition to the Python method shown above, ARANGODB provides a query language called AQL, which is used to retrieve and modify documents on the database. In Pyarango, you can use AQLQuery() methods to execute these queries.

Retrieve _key for all documents:

" For x in Students RETURN x._key ">>> queryresult = db. Aqlquery (AQL, rawresults=true, batchsize=) for in queryresult:    ... Print (key) Marywollstonecraftkatechopinpercyshelleyfyodordostoevskymarktwain ...

References:

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

Using Python to manipulate and manage Arangodb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.