hadoopの簡単なテスト例
3614 ワード
ネット上でテスト例を探して、統計テキストに単語が現れる回数を指定しました.
デバッグしていくつかのバグを見つけて、修正したものを共有します.
eclipseでコンパイル
vmパラメータ:-Xms 64 m-Xmx 512 m
プログラムパラメータ
デバッグしていくつかのバグを見つけて、修正したものを共有します.
eclipseでコンパイル
vmパラメータ:-Xms 64 m-Xmx 512 m
プログラムパラメータ
package com.run.ayena.distributed.test;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
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;
////
public class SingleWordCount {
public static class SingleWordCountMapper extends
Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text val = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
String keyword = context.getConfiguration().get("word");
while (itr.hasMoreTokens()) {
String nextkey = itr.nextToken();
if (nextkey.trim().equals(keyword)) {
val.set(nextkey);
context.write(val, one);
} else {
// do nothing
}
}
}
}
public static class SingleWordCountReducer extends
Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 3) {
System.err.println("Usage: wordcount ");
System.exit(2);
}
//
conf.set("word", otherArgs[2]);
//
conf.set("mapred.system.dir", "/cygdrive/e/workspace_hadoop/SingleWordCount/");
// job
Job job = new Job(conf, "word count");
// job
job.setJarByClass(SingleWordCount.class);
// Mapper
job.setMapperClass(SingleWordCountMapper.class);
// , Reduer
job.setCombinerClass(SingleWordCountReducer.class);
// Reduer
job.setReducerClass(SingleWordCountReducer.class);
// Map
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// Reducer key
job.setOutputKeyClass(Text.class);
// Reducer value
job.setOutputValueClass(IntWritable.class);
//
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// ,
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}