mongodb學(xué)習(xí)_and_使用mapreduce聚合

一:往數(shù)據(jù)庫里插入數(shù)據(jù)

先生成一些簡單的數(shù)據(jù)

from pymongoimport MongoClient

from randomimport randint

import datetime

client = MongoClient('localhost',27017)

db = client.get_database('229_taobao')

order = db.order_info

status = ['A','B','C']

cust_id = ['A123','B123','C123']

price = [500,200,250,300]

sku = ['mmm','nnn']

for iin range(1,100):

????items = []

????item_count =randint(2,6)

????for nin range(item_count):

????????items.append({"sku":sku[randint(0,1)],"qty":randint(1,10),"price":randint(0,5)})

????new = {

????"status":status[randint(0,2)],

????"cust_id":cust_id[randint(0,2)],

????"price":price[randint(0,3)],

????"ord_date":datetime.datetime.utcnow(),

????"items":items

}

print new

order.insert_one(new)

print i

print order.estimated_document_count()


二:進(jìn)入數(shù)據(jù)庫進(jìn)行mapreduce操作

? ?查看數(shù)據(jù)類型


(1) 查詢每個(gè)cust_id 的所有price總和

map函數(shù)

var mapFunction1 = function() {?

? ? ? ? ? ? ? ?emit(this.cust_id, this.price);

? ? ? ? ? ? ? };

emit函數(shù)是把數(shù)據(jù)按照cust_id和price進(jìn)行分組,可以將參數(shù)傳給reduce函數(shù)

reduce函數(shù)

var reduceFunction1 = function(keyCustId, valuesPrices) {

? ? ? ? ? ? return Array.sum(valuesPrices);

? ? ? };


執(zhí)行mapreduce函數(shù)并把結(jié)果輸入map_reduce_example中

db.order_info.mapReduce( mapFunction1, reduceFunction1, { out: "map_reduce_example" } )


查詢輸出的結(jié)果


(2)計(jì)算所有items 的平均庫存

? map函數(shù)

var mapFunction2 = function() {?

for (var idx = 0; idx < this.items.length; idx++) {

? ? ? ? ? ? ?var key = this.items[idx].sku;?

? ? ? ? ? ? ?var value = { count: 1, qty: this.items[idx].qty };?

? ? ? ? ? ? emit(key, value);

?}

?};


reduce函數(shù)

var reduceFunction2 = function(keySKU, countObjVals) {?

????reducedVal = { count: 0, qty: 0 };?

????for (var idx = 0; idx < countObjVals.length; idx++) {?

????????reducedVal.count += countObjVals[idx].count;?

????????reducedVal.qty += countObjVals[idx].qty;

?}?

????return reducedVal;?

};


finalize函數(shù)

var finalizeFunction2 = function (key, reducedVal) {?

????reducedVal.avg = reducedVal.qty/reducedVal.count;?

????return reducedVal;

?};


執(zhí)行mapreduce函數(shù)并把結(jié)果輸入map_reduce_example中

db.order_info.mapReduce( mapFunction2, reduceFunction2, { out: { merge: "map_reduce_example" }, finalize: finalizeFunction2 } )


查詢輸出結(jié)果


通過此實(shí)驗(yàn)可以看出,mapreduce比aggregate會更靈活

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容