Skip to content

Instantly share code, notes, and snippets.

@eiri
Created April 4, 2018 17:17
Show Gist options
  • Save eiri/7e89b31f36629fb357b6e1fa194cf0cd to your computer and use it in GitHub Desktop.
Save eiri/7e89b31f36629fb357b6e1fa194cf0cd to your computer and use it in GitHub Desktop.
Run a native erlang view in CouchDB

Prereq

Running docker, couchdb:2.1.1 image, httpie. Create dir data and etc. Place local.ini in etc

Run

$ docker run --rm -it -d -p 5984:5984 -p 5986:5986 -v $(PWD)/data:/opt/couchdb/data -v $(PWD)/etc:/opt/couchdb/etc/default.d couchdb:2.1.1
250179e806b370fea7cd99e5fa7bfae5a4586b3bfda6bded5f956424346449e2

$ http :5984
{
    "couchdb": "Welcome", 
    "features": [
        "scheduler"
    ], 
    "vendor": {
        "name": "The Apache Software Foundation"
    }, 
    "version": "2.1.1"
}

$ http put :5984/koi
{
    "ok": true
}

$ http :5984/koi a=0 b=1
{
    "id": "0928b615e892db99b78e42063d000a1b", 
    "ok": true, 
    "rev": "1-5f2f950ea674601f69834acef0f48c77"
}

$ http :5984/koi a=1 b=2
{
    "id": "0928b615e892db99b78e42063d000e4a", 
    "ok": true, 
    "rev": "1-1d11ff1d76bfac091a6ca550c9d0863d"
}

$ http :5984/koi a=2 b=3
{
    "id": "0928b615e892db99b78e42063d0018c0", 
    "ok": true, 
    "rev": "1-75bedfd75b4964c629b61d2baa2c4fbc"
}

$ http :5984/koi a=3 b=4
{
    "id": "0928b615e892db99b78e42063d0020ab", 
    "ok": true, 
    "rev": "1-9d803eead70d71b671346c2ee0567d8b"
}

$ http :5984/koi a=4 b=5
{
    "id": "0928b615e892db99b78e42063d002735", 
    "ok": true, 
    "rev": "1-d4bf24e28a7d43cd82cee0197dfef0bc"
}

$ http :5984/koi a=5 b=6
{
    "id": "0928b615e892db99b78e42063d002a9c", 
    "ok": true, 
    "rev": "1-de6888f688b256dd9829c6c444421e3f"
}

$ http :5984/koi a=6 b=7
{
    "id": "0928b615e892db99b78e42063d002ec0", 
    "ok": true, 
    "rev": "1-09965fce769b8accae2f9224e874819c"
}

$ http :5984/koi a=7 b=8
{
    "id": "0928b615e892db99b78e42063d00369d", 
    "ok": true, 
    "rev": "1-ba79373972312e4dfed0e8d2efb924f5"
}

$ http :5984/koi a=8 b=9
{
    "id": "0928b615e892db99b78e42063d00436d", 
    "ok": true, 
    "rev": "1-5ffa377ab71f3c62dd38ffd19d178b6c"
}

$ http :5984/koi a=9 b=0
{
    "id": "0928b615e892db99b78e42063d00526c", 
    "ok": true, 
    "rev": "1-05b7e09163f6cdd25769833e22665fc0"
}

$ http put :5984/koi/_design/ddoc < ddoc.erl
{
    "id": "_design/ddoc", 
    "ok": true, 
    "rev": "1-fd8613f16d79367cb4606c73b9fcc40a"
}

$ http :5984/koi/_design/ddoc/_view/keys include_docs==false
{
    "offset": 0, 
    "rows": [
        {
            "id": "0928b615e892db99b78e42063d000a1b", 
            "key": "0", 
            "value": null
        }, 
        {
            "id": "0928b615e892db99b78e42063d00526c", 
            "key": "0", 
            "value": null
        }, 
...

$ http delete :5984/koi
{
    "ok": true
}

$ docker stop nervous_archimedes
{
"language": "erlang",
"views": {
"keys": {
"map": "fun({Doc}) -> Emit(couch_util:get_value(<<\"a\">>, Doc, null), null), Emit(couch_util:get_value(<<\"b\">>, Doc, null), null) end."
}
}
}
{
"language": "javascript",
"views": {
"keys": {
"map": "function(doc) { emit(doc.a); emit(doc.b) }"
}
}
}
[chttpd]
bind_address = any
[httpd]
bind_address = any
[native_query_servers]
erlang = {couch_native_process, start_link, []}
query = {mango_native_proc, start_link, []}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment