This is part three of a beginner series for people with a MySQL/PHP background. Apologies for the delay, this blog entry has been in draft since the 13th December of last year (2009).
Follow these links for the previous parts:
Part I introduced the CouchDB basics which included basic requests using PHP and cURL. Part II focused on create, read, update and delete operations in CouchDB. I also introduced my nifty PHP CouchDB called ArmChair!
ArmChair is my own very simple and (hopefully) easy-to-use approach to accessing CouchDB from PHP. The objective is to develop it with each part of this series to make it a more comprehensive solution.
Part three will target basic view functions in CouchDB — think of views as a
WHERE-clause in MySQL. They are similar, but also not. :-)
If you read up on CouchDB before coming to this blog, you will probably heard of map-reduce. There, or maybe elsewhere. A lot of people attribute Google's success to map-reduce. Because they are able to process a lot of data in parallel (across multiple cores and/or machines) in relatively little time.
I guess the PageRank in Google Search or Google Analytics are examples of where it could be used.
In the following, I'll try to explain what map-reduce is. For people without a science degree. (And that includes me!)
Generally, map-reduce is a way to process data. It's made off two things, map and reduce.
The idea is that the map-function is very robust and it allows data to be broken up into smaller pieces so it can be processed in parallel. In most cases the order data is processed in doesn't really matter. What generally counts is that it is processed at all. And since map allows us to run the processing in parallel, it's easier to scale out. (That's the secret sauce!)
And when I write scale-out, I don't suggest to built a cluster of 1000 servers in order to process a couple thousand documents. It's already sufficient in this case to utilize all cores in my own computer when the map task is run in parallel.
In CouchDB, the result of map is a list of keys and values.
Reduce is called once the map-part is done. It's an optional step in terms of CouchDB — not every map requires a reduce to follow.
Real world example
- take a simple photo application (such as flickr) with comments
- use map to sort through the comments and emit the names of users who left one
- use reduce to only get unique references and see how many comments were left by these user
SELECT user, count(*) FROM comments GROUP BY user
Why the fuzz?
Just so people don't feel offended. Map-reduce is slightly more complicated than my example SQL-query but it's also not some secret-super-duper thing. Its strength is really parallelization which requires the ability to break data into chunks to process them. The end.