Python MongoDB MapReduce

Source: Internet
Author: User
Tags emit mongoclient

#-*-Coding:utf-8-*-
Import OS
Import CSV
Import Pymongo
From Pymongo import mongoclient
From Bson.code Import Code
From Pymongo import mongoclient

#建立连接
Client = mongoclient (' 10.20.4.79 ', 27017)
#client = mongoclient (' 10.20.66.106 ', 27017)
db_name = ' Ta ' #数据库名
db = Client[db_name]

Insert test data:

For I in xrange (1000):
Rid=math.floor (Random.random () *10);
Price = Round (Random.random () *10,2);
If RID < 4:
Db.test.insert ({"_id": I, "user": "Joe", "Product": RID, "Price":p rice});
Elif rid>=4 and Rid<7:
Db.test.insert ({"_id": I, "user": "Josh", "Product": RID, "Price":p rice});
Else
Db.test.insert ({"_id": I, "user": "Ken", "Product": RID, "Price":p rice});

The result data is:

{"_id": 0, "price": 5.9, "Product": 9, "user": "Ken"}
{"_id": 1, "price": 7.59, "Product": 7, "User": "Ken"}
{"_id": 2, "price": 4.72, "Product": 0, "user": "Joe"}
{"_id": 3, "price": 1.35, "product": 1, "User": "Joe"}
{"_id": 4, "price": 2.31, "Product": 0, "user": "Joe"}
{"_id": 5, "price": 5.29, "Product": 5, "User": "Josh"}
{"_id": 6, "price": 3.34, "Product": 1, "User": "Joe"}
{"_id": 7, "price": 7.2, "Product": 4, "User": "Josh"}
{"_id": 8, "price": 8.1, "Product": 6, "user": "Josh"}
{"_id": 9, "price": 2.57, "Product": 3, "user": "Joe"}
{"_id": Ten, "Price": 0.54, "Product": 2, "User": "Joe"}
{"_id": One, "price": 0.66, "Product": 1, "User": "Joe"}
{"_id": "Price": 5.51, "Product": 1, "User": "Joe"}
{"_id": "Price": 3.74, "Product": 6, "user": "Josh"}
{"_id": +, "price": 4.82, "Product": 0, "user": "Joe"}
{"_id": "Price": 9.79, "Product": 3, "user": "Joe"}
{"_id": +, "price": 9.6, "Product": 5, "User": "Josh"}
{"_id": +, "price": 4.06, "Product": 7, "User": "Ken"}
{"_id": "Price": 1.37, "Product": 5, "User": "Josh"}
{"_id": +, "price": 6.77, "Product": 9, "user": "Ken"}

Test 1, how many products each user has purchased?
The SQL statement is implemented as: Select User,count (product) from the test group by user

Mapper = Code ("" "function () {Emit (This.user,{count:1})}" "")

reduce = Code ("function (key, values) {"
"var total = 0;"
"for (var i = 0; i < values.length; i++) {"
"Total + = Values[i].count;"
" }"
"Return {count:total};"
"}")

Result=db.test.map_reduce (mapper,reduce,out = ' myresults ')

  For Doc in Db.myresults.find ():

Print doc

Test 2, query each user, buy the number of goods, the total price, and evaluate the price condition is more than 5 of the SQL implementation: Select User,count (SKU), SUM (price),

Round (sum (price)/count (SKU), 2) as Avgprice from test where prince>5 group by user


Mapper=code ("" "function () {Emit (this.user,{amount:this.price,count:1,avgprice:0})}" "")

reduce = Code ("function (key, values) {"
"Var res={amount:0,count:0,avgprice:0};"
"for (var i = 0; i < values.length; i++) "
" {"
"Res.count + = Values[i].count;"
"Res.amount + = Values[i].amount;"
" }"
"Res.avgprice = (res.amount/res.count). toFixed (2);"
"Return res;"
"}")

result = Db.test.map_reduce (mapper,reduce,out = ' myresults ', query={' price ': {' $gt ': 6}})

For Doc in Db.myresults.find ():
Print doc

Python MongoDB MapReduce

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.