/// <summary>Given an output filename, write a bunch of random records to it.</summary> /// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> protected override void Map(Text key, Text value, Mapper.Context context) { int itemCount = 0; while (numBytesToWrite > 0) { // Generate the key/value int noWordsKey = minWordsInKey + (wordsInKeyRange != 0 ? random.Next(wordsInKeyRange ) : 0); int noWordsValue = minWordsInValue + (wordsInValueRange != 0 ? random.Next(wordsInValueRange ) : 0); Text keyWords = GenerateSentence(noWordsKey); Text valueWords = GenerateSentence(noWordsValue); // Write the sentence context.Write(keyWords, valueWords); numBytesToWrite -= (keyWords.GetLength() + valueWords.GetLength()); // Update counters, progress etc. context.GetCounter(RandomTextWriter.Counters.BytesWritten).Increment(keyWords.GetLength () + valueWords.GetLength()); context.GetCounter(RandomTextWriter.Counters.RecordsWritten).Increment(1); if (++itemCount % 200 == 0) { context.SetStatus("wrote record " + itemCount + ". " + numBytesToWrite + " bytes left." ); } } context.SetStatus("done with " + itemCount + " records."); }
/// <summary>Given an output filename, write a bunch of random records to it.</summary> /// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> protected override void Map(WritableComparable key, Writable value, Mapper.Context context) { int itemCount = 0; while (numBytesToWrite > 0) { int keyLength = minKeySize + (keySizeRange != 0 ? random.Next(keySizeRange) : 0); randomKey.SetSize(keyLength); RandomizeBytes(randomKey.GetBytes(), 0, randomKey.GetLength()); int valueLength = minValueSize + (valueSizeRange != 0 ? random.Next(valueSizeRange ) : 0); randomValue.SetSize(valueLength); RandomizeBytes(randomValue.GetBytes(), 0, randomValue.GetLength()); context.Write(randomKey, randomValue); numBytesToWrite -= keyLength + valueLength; context.GetCounter(RandomWriter.Counters.BytesWritten).Increment(keyLength + valueLength ); context.GetCounter(RandomWriter.Counters.RecordsWritten).Increment(1); if (++itemCount % 200 == 0) { context.SetStatus("wrote record " + itemCount + ". " + numBytesToWrite + " bytes left." ); } } context.SetStatus("done with " + itemCount + " records."); }
/// <summary>Map method.</summary> /// <param name="offset">samples starting from the (offset+1)th sample.</param> /// <param name="size">the number of samples for this map</param> /// <param name="context">output {ture->numInside, false->numOutside}</param> /// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> protected override void Map(LongWritable offset, LongWritable size, Mapper.Context context) { QuasiMonteCarlo.HaltonSequence haltonsequence = new QuasiMonteCarlo.HaltonSequence (offset.Get()); long numInside = 0L; long numOutside = 0L; for (long i = 0; i < size.Get();) { //generate points in a unit square double[] point = haltonsequence.NextPoint(); //count points inside/outside of the inscribed circle of the square double x = point[0] - 0.5; double y = point[1] - 0.5; if (x * x + y * y > 0.25) { numOutside++; } else { numInside++; } //report status i++; if (i % 1000 == 0) { context.SetStatus("Generated " + i + " samples."); } } //output map results context.Write(new BooleanWritable(true), new LongWritable(numInside)); context.Write(new BooleanWritable(false), new LongWritable(numOutside)); }
/// <exception cref="System.IO.IOException"/> protected override void Map(LongWritable key, Text value, Mapper.Context context) { StringBuilder sb = new StringBuilder(512); for (int i = 0; i < 1000; i++) { sb.Append("a"); } context.SetStatus(sb.ToString()); int progressStatusLength = context.GetConfiguration().GetInt(MRConfig.ProgressStatusLenLimitKey , MRConfig.ProgressStatusLenLimitDefault); if (context.GetStatus().Length > progressStatusLength) { throw new IOException("Status is not truncated"); } }