地図:
function () {
emit(this.thread,
{max_year:this.date.getFullYear(),
min_year:this.date.getFullYear(),
max_month:this.date.getMonth(),
min_month:this.date.getMonth(),count:1});
};
Reduce:
function (key, values) {
max_year= values[0].max_year;
min_year = values[0].min_year;
max_month= values[0].max_month;
min_month = values[0].min_month;
var sum = 0;
if (values.length > 1){
for(i in values){
if(values[i].max_year > max_year){
max_year = values[i].max_year;
};
if(values[i].min_year < min_year){
min_year = values[i].min_year;
};
if(values[i].max_month > max_month){
max_month = values[i].max_month;
};
if(values[i].min_month < min_month){
min_month = values[i].min_month;
};
sum+=values[i].count
};
};
return {"max year":max_year, "min year":min_year, "max month":max_month, "min month":min_month, "No of posts": sum};
}
};
出力:
{u'_id': u'Sujet Top 5 TED POST', u'value': {u'No of posts': 8.0, u'min month': 0.0, u'max month': 6.0, u'max year': 2011.0, u'min year': 2010.0}}
{u'_id': u'Sujet Top 5 des meilleurs guitaristes de lhistoire du Rock', u'value': {u'No of posts': 42.0, u'min month': 2.0, u'max month': 10.0, u'max year': 2011.0, u'min year': 2009.0}}
{u'_id': u'Sujet Top ALEJANDRO GONZALEZ INARRITU', u'value': {u'No of posts': 29.0, u'min month': 0.0, u'max month': 9.0, u'max year': 2011.0, u'min year': 2008.0}}
{u'_id': u'Sujet Top ANDY et LARRY WACHOWSKY', u'value': {u'No of posts': 40.0, u'min month': 0.0, u'max month': 11.0, u'max year': 2011.0, u'min year': 2008.0}}
{u'_id': u'Sujet Top BRYAN SINGER', u'value': {u'No of posts': 50.0, u'min month': 0.0, u'max month': 11.0, u'max year': 2011.0, u'min year': 2006.0}}
{u'_id': u'Sujet Top Cinma 2010', u'value': {u'No of posts': nan, u'min month': None, u'max month': None, u'max year': None, u'min year': None}}
{u'_id': u'Sujet Top Cinma 2011', u'value': {u'No of posts': nan, u'min month': None, u'max month': None, u'max year': None, u'min year': None}}
ご覧のとおり、一部のフィールド(「投稿数なし」)では「Nan」と出力され、他のフィールドでは非出力になります。これは、タイムスタンプを処理せずに投稿の数をカウントするためだけにMapReduceを実行した場合には発生しません。また、「投稿数」が多い場合(1000程度程度)にNanが印刷されていることにも気づきました。また、「カウント」と「合計」がない場合、最大年、最小年、月のすべての操作は適切です。ありがとうございました。