Created
April 10, 2015 08:09
-
-
Save SaschaDittmann/88d84c388a15e10527a9 to your computer and use it in GitHub Desktop.
MapReduce Entwurfsmuster – Numerische Aggregation (Average)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AverageCombiner | |
: JsonInOutReducerCombinerBase<CountAverageData, CountAverageData> | |
{ | |
public override void Reduce(string key, | |
IEnumerable<CountAverageData> values, | |
JsonReducerCombinerContext<CountAverageData> context) | |
{ | |
float sum = 0; | |
float count = 0; | |
foreach (var value in values) | |
{ | |
sum += value.Count * value.Average; | |
count += value.Count; | |
} | |
context.EmitKeyValue(key, new CountAverageData | |
{ | |
Average = sum / count, | |
Count = count, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AverageMapper | |
: JsonOutMapperBase<CountAverageData> | |
{ | |
public override void Map(string inputLine, | |
JsonMapperContext<CountAverageData> context) | |
{ | |
var parsed = XmlUtils.ParseXml(inputLine); | |
if (parsed == null | |
|| !parsed.ContainsKey("CreationDate") | |
|| !parsed.ContainsKey("Text")) | |
{ | |
context.CoreContext.IncrementCounter( | |
"Average Mapper", "Invalid Rows", 1); | |
return; | |
} | |
DateTime creationDate; | |
if (!DateTime.TryParse(parsed["CreationDate"], out creationDate)) | |
{ | |
context.CoreContext.IncrementCounter( | |
"Average Mapper", "Invalid Creation Dates", 1); | |
return; | |
} | |
var text = parsed["Text"]; | |
context.EmitKeyValue( | |
creationDate.Hour.ToString(CultureInfo.InvariantCulture), | |
new CountAverageData | |
{ | |
Average = text.Length, | |
Count = 1, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AverageReducer | |
: JsonInReducerCombinerBase<CountAverageData> | |
{ | |
public override void Reduce(string key, | |
IEnumerable<CountAverageData> values, | |
ReducerCombinerContext context) | |
{ | |
float sum = 0; | |
float count = 0; | |
foreach (var value in values) | |
{ | |
sum += value.Count * value.Average; | |
count += value.Count; | |
} | |
context.EmitKeyValue(key, new CountAverageData | |
{ | |
Average = sum / count, | |
Count = count, | |
}.ToString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CountAverageData | |
{ | |
public float Average { get; set; } | |
public float Count { get; set; } | |
public override string ToString() | |
{ | |
return String.Format("{0}t{1}", Average, Count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment