Skip to content

Instantly share code, notes, and snippets.

@avegancafe
Forked from NicoHinderling/mappy
Last active August 29, 2015 14:20
Show Gist options
  • Save avegancafe/ba3d69b2f83566f27a3f to your computer and use it in GitHub Desktop.
Save avegancafe/ba3d69b2f83566f27a3f to your computer and use it in GitHub Desktop.
package wordcount;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import java.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class fourB {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable zero = new IntWritable(0);
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
Text k = new Text ();
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens ()) {
String wordy = itr.nextToken ();
for (int m = 0; m < 15; m++){
if(m == wordy.length ()){
int counter = 0;
for(int x = 0; x < wordy.length (); x++){
if(wordy.charAt (x)== 'a' || wordy.charAt (x)== 'e' || wordy.charAt (x)== 'i' || wordy.charAt (x)== 'o' || wordy.charAt (x)== 'u' || wordy.charAt (x)== 'y' || wordy.charAt (x)== 'A' || wordy.charAt (x)== 'E' || wordy.charAt (x)== 'I' || wordy.charAt (x)== 'O' || wordy.charAt (x)== 'U' || wordy.charAt (x)== 'Y'){
counter++;
}
}
System.out.println(wordy + " of length " + m + " contains " + counter + " values!");
k.set (Integer.toString (m));
context.write( k, new IntWritable (counter) );
}
k.set (Integer.toString (m));
context.write ( k, zero );
}
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,DoubleWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int key_val = Integer.parseInt (key.toString());
double sum = 0;
double denom = 0;
int[] arr = new int[key_val+1];
for (IntWritable val : values) {
denom++;
arr[val.get ()]++;
}
Text fin = new Text ();
for (int i = 0; i < arr.length; i++) {
fin.set(Integer.toString (key_val) + " " + i);
context.write ( fin, new DoubleWritable(arr[i] / denom) );
}
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(fourB.class);
conf.setOutputKeyClass(IntWritable.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(EmitVowels.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment