updated word count example

This commit is contained in:
Chris Anderson 2009-01-15 15:05:55 -08:00
parent a4144af72d
commit 50f0104173
3 changed files with 11 additions and 32 deletions

View file

@ -6,8 +6,7 @@ which I find to be concise, clear, and well designed. CouchRest lightly wraps
CouchDB's HTTP API, managing JSON serialization, and remembering the URI-paths CouchDB's HTTP API, managing JSON serialization, and remembering the URI-paths
to CouchDB's API endpoints so you don't have to. to CouchDB's API endpoints so you don't have to.
CouchRest's lighweight is designed to make a simple base for application and CouchRest is designed to make a simple base for application and framework-specific object oriented APIs. CouchRest is Object-Mapper agnostic, the parsed JSON it returns from CouchDB shows up as subclasses of Ruby's Hash. Naked JSON, just as it was mean to be.
framework-specific object oriented APIs.
## Easy Install ## Easy Install

View file

@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../../couchrest' require 'rubygems'
require 'couchrest'
couch = CouchRest.new("http://127.0.0.1:5984") couch = CouchRest.new("http://127.0.0.1:5984")
db = couch.database('word-count-example') db = couch.database('word-count-example')
@ -40,28 +41,6 @@ books.keys.each do |book|
end end
end end
# word_count = {
# :map => 'function(doc){
# var words = doc.text.split(/\W/);
# words.forEach(function(word){
# if (word.length > 0) emit([word,doc.title],1);
# });
# }',
# :reduce => 'function(key,combine){
# return sum(combine);
# }'
# }
#
# db.delete db.get("_design/word_count") rescue nil
#
# db.save({
# "_id" => "_design/word_count",
# :views => {
# :count => word_count,
# :words => {:map => word_count[:map]}
# }
# })
# puts "The books have been stored in your CouchDB. To initiate the MapReduce process, visit http://127.0.0.1:5984/_utils/ in your browser and click 'word-count-example', then select view 'words' or 'count'. The process could take about 15 minutes on an average MacBook." # puts "The books have been stored in your CouchDB. To initiate the MapReduce process, visit http://127.0.0.1:5984/_utils/ in your browser and click 'word-count-example', then select view 'words' or 'count'. The process could take about 15 minutes on an average MacBook."
#

View file

@ -1,22 +1,23 @@
require File.dirname(__FILE__) + '/../../couchrest' require 'rubygems'
require 'couchrest'
couch = CouchRest.new("http://127.0.0.1:5984") couch = CouchRest.new("http://127.0.0.1:5984")
db = couch.database('word-count-example') db = couch.database('word-count-example')
puts "Now that we've parsed all those books into CouchDB, the queries we can run are incredibly flexible." puts "Now that we've parsed all those books into CouchDB, the queries we can run are incredibly flexible."
puts "\nThe simplest query we can run is the total word count for all words in all documents:" puts "\nThe simplest query we can run is the total word count for all words in all documents:"
puts "this will take a few minutes the first time. if it times out, just rerun this script in a few few minutes."
puts db.view('word_count/count').inspect puts db.view('word_count/words').inspect
puts "\nWe can also narrow the query down to just one word, across all documents. Here is the count for 'flight' in all three books:" puts "\nWe can also narrow the query down to just one word, across all documents. Here is the count for 'flight' in all three books:"
word = 'flight' word = 'flight'
params = { params = {
:startkey => [word], :startkey => [word],
:endkey => [word,'Z'] :endkey => [word,{}]
} }
puts db.view('word_count/count',params).inspect puts db.view('word_count/words',params).inspect
puts "\nWe scope the query using startkey and endkey params to take advantage of CouchDB's collation ordering. Here are the params for the last query:" puts "\nWe scope the query using startkey and endkey params to take advantage of CouchDB's collation ordering. Here are the params for the last query:"
puts params.inspect puts params.inspect
@ -28,7 +29,7 @@ params = {
:key => [word, title] :key => [word, title]
} }
puts db.view('word_count/count',params).inspect puts db.view('word_count/words',params).inspect
puts "\nHere are the params for 'flight' in the da-vinci book:" puts "\nHere are the params for 'flight' in the da-vinci book:"