Example #1
0
        /// <summary>Combines values for a given key.</summary>
        /// <param name="key">
        /// the key is expected to be a Text object, whose prefix indicates
        /// the type of aggregation to aggregate the values.
        /// </param>
        /// <param name="values">the values to combine</param>
        /// <param name="output">to collect combined values</param>
        /// <exception cref="System.IO.IOException"/>
        public override void Reduce(Text key, IEnumerator <Text> values, OutputCollector <Text
                                                                                          , Text> output, Reporter reporter)
        {
            string          keyStr     = key.ToString();
            int             pos        = keyStr.IndexOf(ValueAggregatorDescriptor.TypeSeparator);
            string          type       = Sharpen.Runtime.Substring(keyStr, 0, pos);
            ValueAggregator aggregator = ValueAggregatorBaseDescriptor.GenerateValueAggregator
                                             (type);

            while (values.HasNext())
            {
                aggregator.AddNextValue(values.Next());
            }
            IEnumerator outputs = aggregator.GetCombinerOutput().GetEnumerator();

            while (outputs.HasNext())
            {
                object v = outputs.Next();
                if (v is Text)
                {
                    output.Collect(key, (Text)v);
                }
                else
                {
                    output.Collect(key, new Text(v.ToString()));
                }
            }
        }
        /// <param name="key">
        /// the key is expected to be a Text object, whose prefix indicates
        /// the type of aggregation to aggregate the values. In effect, data
        /// driven computing is achieved. It is assumed that each aggregator's
        /// getReport method emits appropriate output for the aggregator. This
        /// may be further customiized.
        /// </param>
        /// <param name="values">the values to be aggregated</param>
        /// <exception cref="System.IO.IOException"/>
        public override void Reduce(Text key, IEnumerator <Text> values, OutputCollector <Text
                                                                                          , Text> output, Reporter reporter)
        {
            string keyStr = key.ToString();
            int    pos    = keyStr.IndexOf(ValueAggregatorDescriptor.TypeSeparator);
            string type   = Sharpen.Runtime.Substring(keyStr, 0, pos);

            keyStr = Sharpen.Runtime.Substring(keyStr, pos + ValueAggregatorDescriptor.TypeSeparator
                                               .Length);
            ValueAggregator aggregator = ValueAggregatorBaseDescriptor.GenerateValueAggregator
                                             (type);

            while (values.HasNext())
            {
                aggregator.AddNextValue(values.Next());
            }
            string val = aggregator.GetReport();

            key = new Text(keyStr);
            output.Collect(key, new Text(val));
        }