【问题标题】:Mutual words in files using hadoop mapreduce使用hadoop mapreduce的文件中的互词
【发布时间】:2012-04-15 00:52:46
【问题描述】:

我一直在尝试执行一些代码,让我“只”列出多个文件中存在的单词;到目前为止我所做的是使用 wordcount 示例和感谢 Chris White 我设法编译它。我尝试在这里和那里阅读以使代码正常工作,但我得到的只是一个没有数据的空白页。映射器假设收集每个单词及其对应的位置;减速器应该收集常用词关于可能是什么问题的任何想法?代码是:

    package org.myorg;

import java.io.IOException;
import java.util.*;
import java.lang.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {



    public static class Map extends MapReduceBase implements Mapper<Text, Text, Text, Text> 
    {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

          private Text outvalue=new Text();
          private String filename = null;

        public void map(Text key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {
        if (filename == null) 
        {
          filename = ((FileSplit) reporter.getInputSplit()).getPath().getName();
        }

        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);

        while (tokenizer.hasMoreTokens()) 
        {
          word.set(tokenizer.nextToken());
          outvalue.set(filename);
          output.collect(word, outvalue);
        }

        }
    }



    public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
    {


        private Text src = new Text();
        public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
        {


        int sum = 0;
        //List<Text> list = new ArrayList<Text>(); 

            while (values.hasNext()) // I believe this would have all locations of the same word in different files?
            {

                sum += values.next().get();
                src =values.next().get();

            }
        output.collect(key, src);
            //while(values.hasNext()) 
            //{ 
                //Text value = values.next(); 
                //list.add(new Text(value)); 
                //System.out.println(value.toString());       
            //} 
            //System.out.println(values.toString()); 
            //for(Text value : list) 
            //{ 
                //System.out.println(value.toString()); 
            //} 


        }

    }



    public static void main(String[] args) throws Exception 
    {

    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("wordcount");
    conf.setInputFormat(KeyValueTextInputFormat.class);
    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(Text.class);
    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.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);

    }

}

我错过了什么吗? 非常感谢... 我的 Hadoop 版本:0.20.203

【问题讨论】:

标签: hadoop mapreduce word-count


【解决方案1】:

首先,您似乎在使用旧的 Hadoop API (mapred),建议使用与 0.20.203 兼容的新 Hadoop API (mapreduce)

在新的 API 中,这里有一个可以使用的字数统计

import java.io.IOException;
import java.lang.InterruptedException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.conf.Configuration;
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;

public class WordCount {
/**
 * The map class of WordCount.
 */
public static class TokenCounterMapper
    extends Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
/**
 * The reducer class of WordCount
 */
public static class TokenCounterReducer
    extends Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
        throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        context.write(key, new IntWritable(sum));
    }
}
/**
 * The main entry point.
 */
public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    Job job = new Job(conf, "Example Hadoop 0.20.1 WordCount");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenCounterMapper.class);
    job.setReducerClass(TokenCounterReducer.class);
    job.setOutputKeyClass(Text.class);
    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);
}
}  

然后,我们构建这个文件并将结果打包到一个 jar 文件中:

mkdir classes
javac -classpath /path/to/hadoop-0.20.203/hadoop-0.20.203-core.jar:/path/to/hadoop-  0.20.203/lib/commons-cli-1.2.jar -d classes WordCount.java && jar -cvf wordcount.jar -C classes/ .

最后,我们在 Hadoop 的独立模式下运行 jar 文件

echo "hello world bye world" > /tmp/in/0.txt
echo "hello hadoop goodebye hadoop" > /tmp/in/1.txt
hadoop jar wordcount.jar org.packagename.WordCount /tmp/in /tmp/out

【讨论】:

  • 我建议使用新API,因为我看到很多人继续使用旧API,只是因为官方文档仍在旧API下......
  • 我了解更改谢谢;然而,自从我开始学习旧的 API 后,我想掌握它并向上爬;我知道其中一个变化是上下文对象......谢谢你......
【解决方案2】:

在reducer中,维护一组观察到的值(mapper中发出的文件名),如果你消费完所有值后,这个set size为1,那么这个词只在一个文件中使用。

public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> 
{
    private TreeSet<Text> files = new TreeSet<Text>();

    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException 
    {
        files.clear();

        for (Text file : values)
        {
            if (!files.contains(value))
            {
                // make a copy of value as hadoop re-uses the object
                files.add(new Text(value));
            }
        }

        if (files.size() == 1) {
            output.collect(key, files.first());
        }

        files.clear();
    }
}

【讨论】:

  • 嗨,克里斯;我做了你提到的改变;如果我是对的,我还用(文本值:文件)替换了(文本文件:值)行!?它可以编译一切,但我仍然得到一个空白的可执行文件......有什么想法吗?
  • 我相信错误可能在映射器中?从日志屏幕我看到“合并输入记录=0”、“减少输入记录=0”
  • 它没有太注意映射器 - 你使用什么输入格式? TextInputFormat 发出 不是吗?是否需要为 StrinkTokenizer 配置分隔符?你能写一个单元测试来测试你的字符串标记器逻辑吗?
  • 我刚刚检查了映射器;在收藏家我有这个词和超值。 outvalue 实际上确实包含文件名。但是当我尝试输出单词(键)时,它显示空字符串?可以这样吗?
  • 顺便说一句,我有这一行“conf.setInputFormat(KeyValueTextInputFormat.class);”替换为“conf.setInputFormat(TextInputFormat.class);”这可能是原因吗?只是假设因为这个词一直显示空白/空......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
相关资源
最近更新 更多