Some of you know we are working with CouchDB quite intensively here at echolibre, and so I figured we might as well share a few of our notes, hickups, ideas, implementations, etc. So I decided to make a series of short post on CouchDB (>= 0.10.0) and I would like to start with writing your first CouchDB view in Erlang.
Obviously, you have to make sure that you enabled native Erlang views. Make sure to read on how to enabled your Erlang views on the CouchDB wiki
Once you are done, you can go into Futon and you should see “Erlang” in the list of available view languages:

Futon CouchDB
Ok, let’s get into technicalities and the interesting stuff now, imagine you have a bunch of documents in your database and you want to get a list of documents that have a “name” and a “value” field. In javascript the view would be quite simple, it would look somewhat like:
Which is perfectly fine, however in Erlang it’s a bit different, if you want to use a field, you have to make sure it’s there.
For the purpose of this post and trying to keep it short, I’ve made a rather simple view with 2 functions (fun()). 1 to validate the fields are present in the document and 1 to emit what we need.
or the compacted version:
Go to Futon’s temp view editor, select language Erlang, paste the previous code, click on Save and save your view.
Go to Futon’s temp view editor, select language Javascript, paste the javascript code above, click on Save and save your view.
They should both return the same output and you now have your first Erlang view working
Note that if you want to have a reduce function, this map function has a major flaw. I’ll point it out with an explanation in the second post regarding CouchDB Map/Reduce functions
Special thanks to Adam Kocoloski from Cloudant for the peer review and compact version of the Erlang map function and Jan Lehnardt from Couchio for the general help with Erlang views
You can leave a response, or trackback from your own site.
If you’d want to make it emit the full Doc in the emit value, you could do something along those lines: http://gist.github.com/363396
Can you please let us (who are Erlang challenged) know what the problem with the above map is ? And, a possible fix ?
Thanks.
@BartJ The major flaw is that if one would like to use the values in a reduce, I am not validating the types, thus a reduce _sum could receive a value of a bad type which would cause an exception during indexing. This means that the indexing internally crashes and picks up back where it was. As a user you don’t see what happens really, however the performance of your server gets badly affected.
We like to blog about things we're passionate about. We love PHP, MySQL, CouchDB, Linux, Apache - web development standards. We also like writing about building web apps and working with web technology.
You can email us on freedom@echolibre.com
Eamon Leonard - @EamonLeonard
David Coallier - @DavidCoallier
Helgi Þormar Þorbjörnsson - @h
J.D Fitz.Gerald - @jdfitzgerald
Noah Slater - @nslater
Court Ewing - @courtewing
(2)
(5)
(2)
(4)
(3)
(5)
(2)
(1)
(35)
(1)
(3)
(1)
(1)
(28)
(12)
(1)
(2)
(2)
(3)
(1)
(1)
(3)
(1)
(1)
(15)
(1)
(5)
(6)
(6)
(1)
(21)
(3)
(1)
(2)
(2)
(1)
(5)
(3)
(1)
(2)
(3)
(1)
(3)
(4)
This works great if I just need to emit a key and a single value, but I can’t figure out how to make it emit the entire Doc as the value, or much-better-yet, to emit a subset of the Doc as in this javascript map function: http://gist.github.com/323516