Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

MAPREDUCE basics

From NoSQLZoo
Revision as of 16:11, 29 July 2015 by 40082593 (talk | contribs)
Jump to: navigation, search
#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)

MapReduce the basics

This tutorial introduces the MapReduce command.

MapReduce examples are available.

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"])

Show the smallest 3 countries (ignore areas of 0 or None)


from bson.code import Code temp = db.world.map_reduce(

       query={"$and":[{"area":{"$ne":None}}, {"area":{"$ne":0}}]},
       sort={"area":1},
       limit=3,
       map=Code("function(){emit(this.name, this.area)}"), 
       reduce=Code("function(key, values){}"),
       out={"inline":1},

)

pp.pprint(

  temp["results"]

)