Difference between revisions of "MAPREDUCE basics"
From NoSQLZoo
Line 13: | Line 13: | ||
pp = pprint.PrettyPrinter(indent=4) | pp = pprint.PrettyPrinter(indent=4) | ||
</pre> | </pre> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | <p class=strong>Show a list of countries found in North America</p> | ||
+ | <pre class=def> | ||
+ | from bson.code import Code | ||
+ | temp = db.world.map_reduce( | ||
+ | query={"continent":"Africa"}, | ||
+ | map=Code("function(){emit(this.continent, this.name)}"), | ||
+ | reduce=Code("function(key, values){return values.toString()}"), | ||
+ | out={"inline":1} | ||
+ | ) | ||
+ | |||
+ | pp.pprint( | ||
+ | temp["results"] | ||
+ | ) | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | from bson.code import Code | ||
+ | temp = db.world.map_reduce(query={"continent":"North America"},map=Code("function(){emit(this.continent, this.name)}"), reduce=Code("function(key, values){return values.toString()}"),out={"inline":1},) | ||
+ | pp.pprint(temp["results"]) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | <div class=q data-lang="py3"> | ||
+ | <p class=strong>Show the number of countries on each continent</p> | ||
+ | <pre class=def> | ||
+ | </pre> | ||
+ | <div class=ans> | ||
+ | from bson.code import Code | ||
+ | temp = db.world.map_reduce(map=Code("function(){emit(this.continent, this.name)}"), reduce=Code("function(key, values){return values.length}"),out={"inline":1}) | ||
+ | pp.pprint(temp["results"]) | ||
+ | </div> | ||
+ | </div> |
Revision as of 14:49, 29 July 2015
#ENCODING import io import sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16') #MONGO from pymongo import MongoClient client = MongoClient() client.progzoo.authenticate('scott','tiger') db = client['progzoo'] #PRETTY import pprint pp = pprint.PrettyPrinter(indent=4)
Show a list of countries found in North America
from bson.code import Code temp = db.world.map_reduce( query={"continent":"Africa"}, map=Code("function(){emit(this.continent, this.name)}"), reduce=Code("function(key, values){return values.toString()}"), out={"inline":1} ) pp.pprint( temp["results"] )
from bson.code import Code temp = db.world.map_reduce(query={"continent":"North America"},map=Code("function(){emit(this.continent, this.name)}"), reduce=Code("function(key, values){return values.toString()}"),out={"inline":1},) pp.pprint(temp["results"])
Show the number of countries on each continent
from bson.code import Code temp = db.world.map_reduce(map=Code("function(){emit(this.continent, this.name)}"), reduce=Code("function(key, values){return values.length}"),out={"inline":1}) pp.pprint(temp["results"])