My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎

CouchDB


Install on Linux
sudo apt-get install couchdb
Securing service:

user: WXOPACZdko
passw: fnJh17uuFc

HOST="http://WXOPACZdko:fnJh17uuFc@127.0.0.1:5984"
curl -X PUT $HOST/somedatabase

coockies auth: 
HOST="http://127.0.0.1:5984"
curl -vX POST $HOST/_session -H "Content-Type: application/x-www-form-urlencoded" -d "name=WXOPACZdko&password=fnJh17uuFc"

curl -vX PUT $HOST/mydatabase --cookie "AuthSession=V1hPUEFDWmRrbzo1MzQ3MTYzQzrAeBv3Wt2khMtUlAk5u0W0ys56uQ" -H "X-CouchDB-WWW-Authenticate: Cookie" -H "Content-Type: application/x-www-form-urlencoded"
where cookies is taken from previos request


More about ports and hosts configuration:

https://ru.wikipedia.org/wiki/CouchDB
http://couchdb.apache.org/
http://wiki.apache.org/couchdb/Introduction


Run:
curl http://localhost:5984/
to verify instalation.
http://guide.couchdb.org/ - book
or
http://127.0.0.1:5984/_utils/docs/index.html

netstat -an | grep "LISTEN "

sudo vim /etc/couchdb/local.ini
bind_address = 0.0.0.0
for sharing access
sudo iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 5984 -j ACCEPT

reset password:
  1. clear the [admin] section in your */etc/couchdb/local.ini
  2. delete the _users.couch database (located in */var/lib/couchdb/)

restart service on windows:
net.exe stop "apache couchdb"
net.exe start "apache couchdb"
linux:

Common usages:

http://pythonhosted.org/CouchDB/getting-started.html
http://pythonhosted.org/CouchDB/client.html
http://pythonhosted.org/CouchDB/mapping.html

http://guide.couchdb.org/draft/cookbook.html
http://guide.couchdb.org/draft/views.html

http://127.0.0.1:5984/_utils/docs/intro/api.html


create db, get db

shell:
curl -X PUT http://127.0.0.1:5984/base_name

py:
def get_base():
    working_base = 'base_name'
    server = Server('http://localhost:5984/')
    try:
        db = server[working_base]
    except ResourceNotFound:
        db = server.create(working_base)
    return db


drop db
    shell:
        curl -X DELETE http://127.0.0.1:5984/base_name

    py:
        server = Server('http://localhost:5984/')
        server.delete('base_name')

create record
    shell:
        if not have id - generate it:
        curl -X GET http://127.0.0.1:5984/_uuids

        and put:
        curl -X PUT http://127.0.0.1:5984/base_name/6e1295ed6c29495e54cc05947f18c8af -d '{"title":"There is Nothing Left to Lose","artist":"Foo Fighters"}'

    py:
        imperative:
        from uuid import uuid4
        doc_id = uuid4().hex
        db[doc_id] = {'type': 'person', 'name': 'John Doe'}
        oop:

        from couchdb.mapping import Document, TextField
        
        class Person(Document):
            """Represents a person user"""
            type = TextField(default='Person')
            email = TextField()


        @classmethod
            def create(cls, db, data):
                person = cls(
                    email=data['email']
                )
            person.store(db)
            return person

create two more records with one same attr
    same as previos(this is for testing unique and range search)

edit record
    shell:
        1:
            curl -X PUT http://127.0.0.1:5984/base_name/6e1295ed6c29495e54cc05947f18c8af?rev=1-4b8a35d3f70a5962f86c6dd06ceb599c -d "{\"a\":\"c\"}"
    >> {"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"2-c758cac759e533c0c64f845d1fd55052"}
        2:
      curl -X PUT http://127.0.0.1:5984/base_name/6e1295ed6c29495e54cc05947f18c8af -d "{\"a\":\"d\", \"_rev\":\"2-c758cac759e533c0c64f845d1fd55052\"}"
    >> {"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"3-0d16abac343457d64018f96d8d66ec9f"}
    
    where "rev" is latest revision of document
    
    py:
        same as save, but there are more apis for work with document parts
  

search by id (unique)
        curl:
      curl -X GET http://127.0.0.1:5984/base_name/6e1295ed6c29495e54cc05947f18c8af
        py:
            imperative:
        from uuid import uuid4
        doc_id = uuid4().hex
        db[doc_id] = {'type': 'person', 'name': 'John Doe'}
        db.get(doc_id)
        oop:

from couchdb.design import ViewDefinition
from couchdb.mapping import Document, TextField, ViewField
from couchdb import Server
from couchdb.http import ResourceNotFound


class Person(Document):
    """Represents a person user"""
    type = TextField(default='Person')

    by_id_test = ViewField('by_id_test', '''
    function(doc) {
        if(doc.type=='Person'){
            emit(doc._id, doc);
        }
    }''', include_docs=True)


if __name__ == '__main__':
    working_base = 'name_base'
    try:
        server = Server('http://localhost:5984/')
        db = server[working_base]
    except ResourceNotFound:
        db = server.create(working_base)

    person = Person()
    person.store(db)
    id_test = person.id
    print('id:', id_test)
    ViewDefinition.sync_many(db, [Person.by_id_test])
    founded = list(person.by_id_test(db)[id_test])
    print('founded:', founded[0])
    pass

note red is param and blue is a result, all in map function


search by prop(set)
    same as search by id 

agregate functions(own reduce function)


delete record

        py:
            imperative:
        from uuid import uuid4
        doc_id = uuid4().hex
        db[doc_id] = {'type': 'person', 'name': 'John Doe'}
        doc = db.get(doc_id)
        try:
            db.delete(doc)
        except ResourceConflict:
            # if item was updated in db, the _rev is differ and this throw exception
            pass

            oop:
        def delete(self, db):
            db.delete(self)
    
nesting document

from here: https://pythonhosted.org/CouchDB/mapping.html :
DictField for objects and ListField for arrays


create view

edit view
conflict solving
why mapreduce
mapreduce perfomance
indexing



more eaxmples:


Comments