MongoDB MapReduce Combat <4>

Source: Internet
Author: User
Tags mongodb
With yesterday's smooth, today is more confident, today to do is to obtain the number of interval records, for the foreground statistics chart preparation data, for example, the effect is,
In 1 days, there are xx times, within 2 days of XX times, within 3 days of XX times (excluding the previous interval of data)
Analyzed, you can manipulate the data obtained in the second step of yesterday (ttt_date_out table, data content like {id:1,times:[1,2,3,....]} ), map and reduce also write a map similar to yesterday's
function Map () {
	var times = this.value.times;
	if (times) {
		if (Times.length > 1) {for
			(var i = 0;i < times.length;i++) {
				if (i! = times.length-1) {
					VA R between_time = times[i]-times[i+1];
					Rounding up, the 1.4,1,7 will become 2, meaning "two days"
					var day = Math.ceil (Between_time/(+ *));
					Emit (Day
						, 1
					); 
				}
	}}}
Write reduce
function Reduce (key, values) {
	var reduced = {Key:key, count:0};
	Values.foreach (function (val) {
		reduced.count++; 
	});
	return reduced;	
}
Get the results, to achieve expectations, but still not ideal, for only one map of data and multiple map data, the result is a little different
The company can not Google, only ask degrees Niang, no solution, their own, think of yesterday used in finalize write finalize
function Finalize (key, reduced) {
	if (reduced.count) {
		return reduced.count;
	} else{
		return reduced;
	}
}
Wasted some time, but the boat small good turn, example small good blind try, the final effect
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.