我翻譯瞭如下SQL statment映射減少:MongoDB的減少功能沒有運行
select
p_brand, p_type, p_size,
count(ps_suppkey) as supplier_cnt
from
partsupp, part
where
p_partkey = ps_partkey
and p_brand <> 'Brand#45'
and p_type not like 'MEDIUM POLISHED %'
and p_size in (49, 14, 23, 45, 19, 3, 36, 9)
and ps_suppkey not in (
select
s_suppkey
from
supplier
where
s_comment like '%Customer%Complaints%'
)
group by
p_brand, p_type, p_size
order by
supplier_cnt desc, p_brand, p_type, p_size;
地圖縮小功能:
db.runCommand({
mapreduce: "partsupp",
query: {
"ps_partkey.p_size": { $in: [49, 14, 23, 45, 19, 3, 36, 9] },
"ps_partkey.p_brand": { $ne: "Brand#45" }
},
map: function() {
var pattern1 = /^MEDIUM POLISHED .*/;
var pattern2 = /.*Customer.*Complaints.*/;
var suppkey = this.ps_suppkey.s_suppkey;
if(this.ps_suppkey.s_comment.match(pattern1) == null){
if(this.ps_suppkey.s_comment.match(pattern2) != null){
emit({p_brand: this.ps_partkey.p_brand, p_type: this.ps_partkey.p_type, p_size: this.ps_partkey.p_size}, suppkey);
}
}
},
reduce: function(key, values) {
return values.length;
},
out: 'query016'
});
輸出結果(在我看來)沒有一個減少:
{
"result" : "query016",
"timeMillis" : 46862,
"counts" : {
"input" : 122272,
"emit" : 54,
"reduce" : 0,
"output" : 54
},
"ok" : 1
}
怎麼了?
你的reduce函數是不正確的,也可能是你的map函數 - 你必須從第二個參數的縮減格式中返回給你的emit函數。因此,在發出你想要第二個參數爲NUMBER(如1),並減少你需要迭代值並添加它們。此外,你應該使用聚合框架,而不是map-reduce,除非你的結果集預計會很大。 – 2013-04-24 13:20:12
其實this.ps_suppkey.s_suppkey是一個MongoDB NumberLong我想如果這是問題。你認爲Asya怎麼樣? – ulima69 2013-04-24 15:39:48
鍵(發出的第一個參數)可以是任何東西,基本類型,文檔等等。第二個參數可以是任何東西,但無論它的類型如何,它必須與reduce函數發出的相同。你是否認爲你的鑰匙的每個組合只出現一次? – 2013-04-24 18:00:21