MongoDB MapReduce Combat <5>

Source: Internet
Author: User
Tags mongodb sort
Start real actual combat, table data about 100w, today first to solve the first demand, that is, to find the average record time, directly run "Combat 2" has been written mapreduce. An exception, no results, as long as the {sort}, there is no result, find the data, said must be indexed to join the sort (but before the data volume is small, the program runs well), after indexing, at sort, enter {' Create_date ':-1}, the result, problem solving In the results, many records in the Times field are not sorted, and irregular, such as this time the completion of MapReduce is the order of the times of this record is a problem, but once again run MapReduce, this record of the Times sort is no problem, suspicion is related to multithreading. You can only reorder results in finalize by adding finalize to sort the dates
function Finalize (key, reduced) {
	if (reduced.times) {  
		Reduced.times.sort (function (x, y) {
			return x < y? 1 :-1;
		});
    }  
	return reduced;
}
In & Out also make corresponding changes to remove the useless sorting operation
OK, continue to run the second averaging of MapReduce ("Combat 3"), continue to report abnormal NaN, non-numeric anomalies, the cause of this problem is very simple, but it took me a lot of time, because the maximum range of data types, need to modify the MapReduce, Do not do the overall accumulation after the entire number of times, instead of two number one plus, directly on the average to modify the map
function Map () {
	var temp = 0.0;
	var times = this.value.times;
	if (times) {
		if (Times.length > 1) {for
			(var i = 0;i < times.length;i++) {
				if (i! = times.length-1) {
					te MP + = Times[i]-times[i+1];
					temp = Temp/2
				}}}
		emit (
			' Result ', 
			temp
		); 
	}
}
Modify reduce
function Reduce (key, values) {
	var ret = {Msg:key};
	var temp = 0.0;
	for (var i = 0;i < values.length;i++) {
		if (i > 0) {
			temp + = values[i];
			temp = TEMP/2
		}
	}
	ret.arv_time = temp;
	return ret;
}
Because of the double precision, finalize also needs to be modified slightly
function Finalize (key, reduced) {
	//turn to seconds 
	var second = parseint (reduced.arv_time/1000.00);
    Turn minute
	var minute = parseint (SECOND/60);
	Remaining seconds
	var s = second%;
	Turn hours
	var hour = parseint (MINUTE/60);
	Remaining points
	var m = minute%;
	The day after
	var day = parseint (hour/24);
	Remaining hours
	var h = hour%;
    Return day + "days" + H + "hour" + M + "minute" + S + "second";
}
Final result
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.