<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>echolibre blog &#187; couchdb</title>
	<atom:link href="http://blog.echolibre.com/category/couchdb/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.echolibre.com</link>
	<description></description>
	<pubDate>Tue, 25 May 2010 13:52:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CouchDB, Custom Erlang Map Functions</title>
		<link>http://blog.echolibre.com/2010/02/couchdb-custom-erlang-map-functions/</link>
		<comments>http://blog.echolibre.com/2010/02/couchdb-custom-erlang-map-functions/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 15:49:56 +0000</pubDate>
		<dc:creator>David</dc:creator>
		
		<category><![CDATA[couchdb]]></category>

		<category><![CDATA[database]]></category>

		<category><![CDATA[erlang]]></category>

		<category><![CDATA[futon]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[nosql]]></category>

		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://blog.echolibre.com/?p=824</guid>
		<description><![CDATA[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 (&#62;= 0.10.0) and I would like to start with writing your [...]]]></description>
			<content:encoded><![CDATA[<p>Some of you know we are working with <a title="CouchDB #nosql document based database" href="http://couchdb.apache.org/">CouchDB</a> 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 <a title="CouchDB #nosql document based database" href="http://couchdb.apache.org/">CouchDB</a> (&gt;= 0.10.0) and I would like to start with writing your first CouchDB view in <a title="Erlang programming language" href="http://en.wikipedia.org/wiki/Erlang_(programming_language)">Erlang</a>.</p>
<p>Obviously, you have to make sure that you enabled native Erlang views. Make sure to read on how to enabled your <a title="Enable Native Erlang Views CouchDB" href="http://wiki.apache.org/couchdb/EnableErlangViews">Erlang views on the CouchDB wiki</a></p>
<p><span id="more-824"></span></p>
<p>Once you are done, you can go into Futon and you should see &#8220;Erlang&#8221; in the list of available view languages:</p>
<div class="wp-caption alignnone" style="width: 550px"><img class=" " title="CouchDB Futon" src="http://img.skitch.com/20100215-csta4k1q627thbpjyns4hqmg73.png" alt="Futon CouchDB" width="540" height="54" /><p class="wp-caption-text">Futon CouchDB</p></div>
<p>Ok, let&#8217;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 &#8220;name&#8221; and a &#8220;value&#8221; field. In javascript the view would be quite simple, it would look somewhat like:</p>
<script src="http://gist.github.com/304691.js"></script>
<p>Which is perfectly fine, however in Erlang it&#8217;s a bit different, if you want to use a field, you have to make sure it&#8217;s there.</p>
<p>For the purpose of this post and trying to keep it short, I&#8217;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.</p>
<script src="http://gist.github.com/304696.js"></script>
<p>or the compacted version:</p>
<script src="http://gist.github.com/304740.js"></script>
<p>Go to Futon&#8217;s temp view editor, select language Erlang, paste the previous code, click on Save and save your view.</p>
<p>Go to Futon&#8217;s temp view editor, select language Javascript, paste the javascript code above, click on Save and save your view.</p>
<p>They should both return the same output and you now have your first Erlang view working <img src='http://blog.echolibre.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><em><strong>Note that if you want to have a reduce function, this map function has a major flaw. I&#8217;ll point it out with an explanation in the second post regarding CouchDB Map/Reduce functions</strong></em></p>
<p><em><strong>Special thanks to Adam Kocoloski from <a title="Cloudant couchDB Hosting" href="http://cloudant.com">Cloudant</a> for the peer review and compact version of the Erlang map function and Jan </strong></em><strong>Lehnardt</strong><em><strong> from <a title="Jan Lenhardt CouchIO" href="http://couch.io">Couchio</a> for the general help with Erlang views<br />
</strong></em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.echolibre.com/2010/02/couchdb-custom-erlang-map-functions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CouchDB, the project, the crowd</title>
		<link>http://blog.echolibre.com/2009/09/couchdb-the-project-the-crowd/</link>
		<comments>http://blog.echolibre.com/2009/09/couchdb-the-project-the-crowd/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 10:04:25 +0000</pubDate>
		<dc:creator>David</dc:creator>
		
		<category><![CDATA[couchdb]]></category>

		<category><![CDATA[echolibre]]></category>

		<category><![CDATA[industry]]></category>

		<category><![CDATA[innovation]]></category>

		<category><![CDATA[json]]></category>

		<category><![CDATA[databases]]></category>

		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://blog.echolibre.com/?p=559</guid>
		<description><![CDATA[There are many decisions involved when using new technologies and new products. Many people will often go for open source software because it&#8217;s free and you can modify the code. To me being &#8220;an open source&#8221; project involves a whole lot more than simply having an opened code base that you can modify and use [...]]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-570 alignright" title="couchdb-logo" src="http://blog.echolibre.com/wp-content/uploads/2009/09/couchdb-logo.png" alt="couchdb-logo" width="125" height="101" align="right" />There are many decisions involved when using new technologies and new products. Many people will often go for open source software because it&#8217;s free and you can modify the code. To me being &#8220;an open source&#8221; project involves a whole lot more than simply having an opened code base that you can modify and use for free, but it also involves a large amount of factors as such as technical documentation, user examples, tools for a software, the community based around a project and the likes of actual response time from developers.</p>
<p>This article is a major Kudos to the <a title="CouchDB" href="http://couchdb.apache.org/">CouchDB</a> developers. In particular I would like to thank <a title="Jan Lenhardt " href="http://jan.prima.de/">Jan Lehnardt</a>, <a title="Paul J. Davis" href="http://www.davispj.com/">Paul J. Davis</a> and <a title="Robert Newton" href="http://github.com/rnewson">Robert Newson</a> from the CouchDB fame. Let me explain why&#8230;</p>
<p><span id="more-559"></span></p>
<p>For all the skeptics (As I was and still am with some concepts) I have to admit that jumping both feet into CouchDB is scary, especially coming from an <a title="RDBMS Wikipedia" href="http://en.wikipedia.org/wiki/Relational_database_management_system">RDBMS</a>-core background. I think in joins, I see relations, I&#8217;m a normalized database person. Coming with the idea that de-normalization can actually be ok if it&#8217;s done correctly was hell for me.</p>
<p>So the first things we did when using Couch was creating databases, until then, in my head, using &#8220;many&#8221; databases was always a big no-no simply because it was making management of connections and everything hectic. However, with CouchDB it&#8217;s a bit different and that&#8217;s a concept that Jan helped me understand and it can indeed be correctly done if planned thoroughly.</p>
<p>After finding myself in many dead-ends after starting CouchDB, the previously mentioned helpers were always there even for the simplest, most ridiculous questions I could have answered myself by re-reading my questions.</p>
<p>Imagine the following scenario where you have a list of documents that have two fields. &#8220;<strong>time</strong>&#8221; and &#8220;<strong>type</strong>&#8220;. the &#8220;<strong>type</strong>&#8221; field can be either &#8220;<strong>love</strong>&#8221; or &#8220;<strong>hate</strong>&#8221; and and &#8220;<strong>time</strong>&#8221; field is a <strong>unix timestamp (php -r &#8216;echo time() . PHP_EOL;&#8217;)</strong>.</p>
<p>We need to create <a title="CouchDB views" href="http://wiki.apache.org/couchdb/Using_Views">views</a> in order to retrieve the data. The first scenario involves counting the amount of types. So how many documents with type &#8220;<strong>love</strong>&#8221; and how many documents (entries) with the field type with value &#8220;<strong>hate</strong>&#8220;.</p>
<p>You will mostly likely have to do a reduce function as well as your view. So using <a title="Futon, getting started couchdb" href="http://wiki.apache.org/couchdb/Getting_started_with_Futon">futon</a> you can create your simple view that&#8217;ll look like such:</p>
<p>View:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>doc.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        emit<span style="color: #009900;">&#40;</span>doc.<span style="color: #660066;">type</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and your reduce function to look somewhat like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> sum<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now Save this as design document &#8220;<strong>example</strong>&#8221; and view name &#8220;<strong>countdocbytype</strong>&#8220;. (See image below)</p>
<p><img class="alignnone size-full wp-image-590" title="couch-view-key1" src="http://blog.echolibre.com/wp-content/uploads/2009/09/couch-view-key1.png" alt="couch-view-key1" width="580" height="94" /></p>
<p>This is all very nice but what does that give you the ability of doing? Remember that &#8220;<strong>WHERE type = &#8216;get&#8217;</strong>&#8221; in SQL? Well now that you saved that view you can effectively search on the key you are returning in your <a title="CouchDB introduction to views" href="http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views">emit()</a> function. In this case you are returning the <strong>doc.type</strong> thus &#8220;<strong>hate</strong>&#8221; or &#8220;<strong>love</strong>&#8221; which means that if you want to get a count of type &#8220;hate&#8221; you will invoke your view as such:</p>
<blockquote><p><em>http://localhost:5984/databasename/_design/example/_view/countdocbytype?group=true&amp;key=&#8221;hate&#8221;</em></p></blockquote>
<p>Remember that this is all JSON lovin&#8217; so the key you are passing has to be JSON encoded (So <em>key=&#8221;<strong>hate</strong>&#8220;</em> instead of key=hate). This view (Query for SQL people) will return a count of entries/documents with the field &#8220;<strong>type</strong>&#8221; that has the value &#8220;<strong>hate</strong>&#8221;</p>
<p>For the second example let&#8217;s introduce a new field in our documents, let&#8217;s name the field &#8220;<strong>phone</strong>&#8221; and it&#8217;ll be &#8220;<strong>iphone</strong>&#8221; and &#8220;<strong>android</strong>&#8220;. Update your documents and put as many &#8220;<strong>iphone</strong>&#8221; and &#8220;<strong>androids</strong>&#8221; as you want. The next example is going to be finding how many people hate iphones (This implies searching for a count of field &#8220;<strong>phone</strong>&#8221; with value &#8220;<strong>iphone</strong>&#8221; and field &#8220;<strong>type</strong>&#8221; with value &#8220;<strong>hate</strong>&#8220;).</p>
<p>Documents updated? Good.</p>
<p>Now let&#8217;s make our view:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>doc.<span style="color: #660066;">phone</span> <span style="color: #339933;">&amp;&amp;</span> doc.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>doc.<span style="color: #660066;">phone</span><span style="color: #339933;">,</span> doc.<span style="color: #660066;">type</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        emit<span style="color: #009900;">&#40;</span>keys<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and the reduce to return a sum of the values:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> sum<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In Futon it would look somewhat like:</p>
<p><img class="alignnone size-full wp-image-564" title="couch-view-keys-1" src="http://blog.echolibre.com/wp-content/uploads/2009/09/couch-view-keys-1.png" alt="couch-view-keys-1" width="580" height="97" /></p>
<p>With the results looking like:</p>
<p><img class="alignnone size-full wp-image-565" title="couch-view-keys-result" src="http://blog.echolibre.com/wp-content/uploads/2009/09/couch-view-keys-result.png" alt="couch-view-keys-result" width="578" height="60" /></p>
<p>So now again save it as design &#8220;<strong>example</strong>&#8221; and view name &#8220;<strong>countdocbyphoneandtype</strong>&#8221; and now you can invoke the following URL.</p>
<blockquote><p><em>http://localhost:5984/databasename/_design/example/_view/countdocbyphoneandtype?group=true&amp;key=["iphone", "hate"]</em></p></blockquote>
<p>As you can see, we are passing an array in the &#8220;<strong>key</strong>&#8221; field with the values &#8220;<strong>iphone</strong>&#8221; and &#8220;<strong>hate</strong>&#8220;. This retrieves all the &#8220;<strong>phone</strong>&#8221; fields with value &#8220;<strong>iphone</strong>&#8221; and then from that resultset all the field &#8220;<strong>type</strong>&#8221; with value &#8220;<strong>hate</strong>&#8220;. It gives us the following type of output:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">&quot;rows&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&quot;key&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;iphone&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;hate&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">2</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You now have a query that searches for &#8220;two&#8221; fields in your document.</p>
<p>I strongly suggest to read the wiki and anything related to views if you are remotely interested in using CouchDB as they are going to be your main source of nightmares and happiness. The latter example, was nicely explained to me on the IRC channel even though it was plain black and white on the wiki&#8230; Apparently I just couldn&#8217;t see it clearly enough. There are loads of little things with the views that require user experience instead of wiki reading and the people on the IRC channel have used CouchDB, they&#8217;ll get you by <img src='http://blog.echolibre.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Moving on, on top of many exciting upcoming features (as such as continuous replication, bulk inserts, etc.) the project has online documentation, online books for people to read for free (Even though I strongly recommend buying the book), a vibrant IRC channel with the nicest helpers from extensive users to software developers/creators to book authors.</p>
<p>When in need of direction, if you really can&#8217;t find it online or don&#8217;t understand how it&#8217;s explained, you can hop on the IRC channel and get your well formulated questions answered at all times. With a thorough patch review process and code quality insurance,in my opinion, it makes CouchDB a very solid open source project that should be respected and should serve as example for any new software that wants to make it into the open source world.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.echolibre.com/2009/09/couchdb-the-project-the-crowd/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
