【发布时间】:2014-01-26 00:08:54
【问题描述】:
我正在尝试使用 Hadoop 找出文本中最常见的单词。 Hadoop 是一个允许跨计算机集群分布式处理大型数据集的框架。
我知道这可以通过使用 Unix 命令轻松完成:job: sort -n -k2 txtname | tail。但这不适用于大型数据集。所以我试图分解问题,然后结合结果。
这是我的WordCount 课程:
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
public static void runJob(String[] input, String output) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
Path outputPath = new Path(output);
FileInputFormat.setInputPaths(job, StringUtils.join(input, ","));
FileOutputFormat.setOutputPath(job, outputPath);
outputPath.getFileSystem(conf).delete(outputPath,true);
job.waitForCompletion(true);
}
public static void main(String[] args) throws Exception {
runJob(Arrays.copyOfRange(args, 0, args.length-1), args[args.length-1]);
}
}
我知道我需要做一个额外的工作来与 map reduce classes 并行工作。
这是我的TokenizerMapper 课程:
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text data = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString(), "-- \t\n\r\f,.:;?![]'\"");
while (itr.hasMoreTokens()) {
data.set(itr.nextToken().toLowerCase());
context.write(data, one);
}
}
}
这是我的IntSumReducer 课程:
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class IntSumReducer 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 value : values) {
// TODO: complete code here
sum+=value.get();
}
result.set(sum);
// TODO: complete code here
if (sum>3) {
context.write(key,result);
}
}
}
我需要做的是定义另一个 map 和 reduce 类,它们将与当前的类并行工作。出现次数最多的单词会出现,这是我目前所拥有的 reduce 类:
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
public class reducer2 extends Reducer<Text, IntWritable, Text, IntWritable> {
int max_sum =0;
Text max_occured_key = new Text();
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
// TODO: complete code here
sum+=value.get();
}
if (sum >max_sum) {
max_sum = sum;
max_occured_key.set(key);
}
context.write(max_occured_key, new IntWritable(max_sum));
//result.set(sum);
// TODO: complete code here
/*
if (sum>3) {
context.write(key,result);
}
*/
}
protected void cleanup(Context context) throws IOException, InterruptedException {
context.write(max_occured_key, new IntWritable(max_sum));
}
}
mapper2的代码:
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
public class mapper2 {
private final IntWritable one = new IntWritable(1);
private Text data = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString(), "-- \t\n\r\f,.:;?![]'\"");
int count =0;
while (itr.hasMoreTokens()) {
//data.set(itr.nextToken().toLowerCase());
context.write(data, one);
}
}
}
我还编辑了WordCount 类,以便可以同时运行两个作业:
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
public static void runJob(String[] input, String output) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
Path outputPath = new Path(output);
FileInputFormat.setInputPaths(job, StringUtils.join(input, ","));
FileOutputFormat.setOutputPath(job, outputPath);
outputPath.getFileSystem(conf).delete(outputPath,true);
job.waitForCompletion(true);
Job job2 = new Job(conf);
job2.setJarByClass(WordCount.class);
job2.setMapperClass(TokenizerMapper.class);
job2.setReducerClass(reducer2.class);
job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(IntWritable.class);
Path outputPath2 = new Path(output);
FileInputFormat.setInputPaths(job, StringUtils.join(input, ","));
FileOutputFormat.setOutputPath(job, outputPath);
outputPath.getFileSystem(conf).delete(outputPath,true);
job.waitForCompletion(true);
}
public static void main(String[] args) throws Exception {
runJob(Arrays.copyOfRange(args, 0, args.length-1), args[args.length-1]);
}
}
如何使用 hadoop 找出文本中最常用的词?
【问题讨论】:
-
你有什么问题?
-
我正在尝试使用 hadoop 找出文本中最常见的单词并打印出来