Created
November 4, 2012 22:35
-
-
Save richievos/4014084 to your computer and use it in GitHub Desktop.
Simple hadoop example
This file contains hidden or 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
import java.io.IOException; | |
import java.util.*; | |
import java.util.regex.Pattern; | |
import org.apache.hadoop.fs.Path; | |
import org.apache.hadoop.conf.*; | |
import org.apache.hadoop.io.*; | |
import org.apache.hadoop.util.*; | |
import org.apache.hadoop.mapreduce.Mapper; | |
import org.apache.hadoop.mapreduce.Reducer; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.conf.Configured; | |
import org.apache.hadoop.mapreduce.Job; | |
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; | |
public class MyCounter { | |
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { | |
private final static IntWritable one = new IntWritable(1); | |
private final static Pattern notALetter = Pattern.compile("[^a-zA-Z]*"); | |
private Text word = new Text(); | |
private char[] outChars = new char[1]; | |
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { | |
charMap(key, value, context); | |
} | |
protected void charMap(LongWritable key, Text value, Context context) throws IOException, InterruptedException { | |
String cleanedString = notALetter.matcher(value.toString()).replaceAll("").toLowerCase(); | |
for (int i = 0; i < cleanedString.length(); i++) { | |
char charAtLoc = cleanedString.charAt(i); | |
outChars[0] = charAtLoc; | |
word.set(new String(outChars)); | |
context.write(word, one); | |
} | |
} | |
protected void wordMap(LongWritable key, Text value, Context context) throws IOException, InterruptedException { | |
String line = value.toString(); | |
StringTokenizer tokenizer = new StringTokenizer(line); | |
while (tokenizer.hasMoreTokens()) { | |
// String cleanedString = tokenizer.nextToken(); | |
String cleanedString = notALetter.matcher(tokenizer.nextToken()).replaceAll("").toLowerCase(); | |
if (cleanedString.length() > 0) { | |
word.set(cleanedString); | |
context.write(word, one); | |
} | |
} | |
} | |
} | |
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { | |
private IntWritable result = new IntWritable(); | |
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { | |
int sum = 0; | |
for (IntWritable value : values) { | |
sum += value.get(); | |
} | |
result.set(sum); | |
context.write(key, result); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Configuration conf = new Configuration(); | |
Job job = new Job(conf, "mycounter"); | |
job.setJarByClass(MyCounter.class); | |
job.setOutputKeyClass(Text.class); | |
job.setOutputValueClass(IntWritable.class); | |
job.setMapperClass(Map.class); | |
job.setCombinerClass(Reduce.class); | |
job.setReducerClass(Reduce.class); | |
FileInputFormat.addInputPath(job, new Path(args[0])); | |
FileOutputFormat.setOutputPath(job, new Path(args[1])); | |
job.waitForCompletion(true); | |
} | |
} |
This file contains hidden or 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
# initial setup | |
curl http://mirrors.xmission.com/gutenberg/1/3/4/1342/1342.txt | hadoop fs -put - /gutenberg/1342.txt | |
# run these when I change it | |
javac -classpath /usr/local/Cellar/hadoop/1.0.4/libexec/hadoop-core-1.0.4.jar -d class src/MyCounter.java && cd class && jar cvf ../mycounter.jar * && cd - | |
hadoop fs -rmr /gutenberg-word-out && hadoop jar mycounter.jar MyCounter /gutenberg /gutenberg-word-out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment