【问题标题】:Mapreduce MultipleOutputs errorMapreduce MultipleOutputs错误
【发布时间】:2015-04-15 02:26:57
【问题描述】:

我想将 mapreduce 作业的输出存储在两个不同的目录中。 尽管我的代码旨在将相同的输出存储在不同的目录中。

下面是我的Driver类代码

public class WordCountMain {


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

Configuration conf = new Configuration();

Job myhadoopJob = new Job(conf);

myhadoopJob.setJarByClass(WordCountMain.class);
myhadoopJob.setJobName("WORD COUNT JOB");
FileInputFormat.addInputPath(myhadoopJob, new Path(args[0]));

myhadoopJob.setMapperClass(WordCountMapper.class);
myhadoopJob.setReducerClass(WordCountReducer.class);    
myhadoopJob.setInputFormatClass(TextInputFormat.class);
myhadoopJob.setOutputFormatClass(TextOutputFormat.class);

myhadoopJob.setMapOutputKeyClass(Text.class);
myhadoopJob.setMapOutputValueClass(IntWritable.class);

myhadoopJob.setOutputKeyClass(Text.class);
myhadoopJob.setOutputValueClass(IntWritable.class);

MultipleOutputs.addNamedOutput(myhadoopJob, "output1", TextOutputFormat.class, Text.class, IntWritable.class);
MultipleOutputs.addNamedOutput(myhadoopJob, "output2", TextOutputFormat.class, Text.class, IntWritable.class);
FileOutputFormat.setOutputPath(myhadoopJob, new Path(args[1]));




System.exit(myhadoopJob.waitForCompletion(true) ? 0 : 1);



}

}

我的映射器代码

   public class WordCountMapper extends Mapper<LongWritable, Text, Text,     IntWritable>

{





@Override
protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {

String line = value.toString();
String word =null;

StringTokenizer st = new StringTokenizer(line,",");


while(st.hasMoreTokens())
{
 word=  st.nextToken();



context.write(new Text(word), new IntWritable(1));




}



}  

}

我的减速器代码在下面

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>

{

MultipleOutputs mout =null;

protected void reduce(Text key, Iterable<IntWritable> values, Context context)throws IOException, InterruptedException {


int count=0;
int num =0;



    Iterator<IntWritable> ie =values.iterator();

    while(ie.hasNext())
    {
         num = ie.next().get();//1
         count= count+num;

    }
mout.write("output1", key, new IntWritable(count));

mout.write("output2", key, new IntWritable(count));

@Override
protected void setup(org.apache.hadoop.mapreduce.Reducer.Context context)
        throws IOException, InterruptedException {
    // TODO Auto-generated method stub
    super.setup(context);

     mout = new MultipleOutputs<Text, IntWritable>(context);
}




}

@Override
protected void setup(org.apache.hadoop.mapreduce.Reducer.Context context)
        throws IOException, InterruptedException {

    super.setup(context);

     mout = new MultipleOutputs<Text, IntWritable>(context);
}

}

我只是在reduce方法本身中给出输出目录

但是当我使用下面的命令运行这个 mapreduce 作业时,它什么也不做。甚至 Mapreduce 也根本没有启动。只是一个空白并保持空闲状态。

hadoop jar WordCountMain.jar /user/cloudera/inputfiles/words.txt /user/cloudera/outputfiles/mapreduce/multipleoutputs

谁能解释我出了什么问题以及如何用我的代码纠正这个问题

实际上发生的是两个不同名称的输出文件存储在 /user/cloudera/outputfiles/mapreduce/multipleoutputs 中。

但我需要将输出文件存储在不同的目录中。

在 pig 我们可以通过给出不同的目录来使用两个 STORE 语句

如何在 mapreduce 中实现同样的效果

【问题讨论】:

    标签: mapreduce


    【解决方案1】:

    你可以尝试在 Reducer 的清理方法中关闭多个输出对象吗?

    【讨论】:

    • 好的。我也试过了。我收到异常,因为“输出目录未设置”。这是什么意思。我也想知道上面的mapreduce程序能不能用
    • 您必须为作业设置输出路径。您可以设置惰性输出格式,因为您不希望创建空文件。你可以像下面这样设置。 FileOutputFormat.setOutputPath(job, outputPath); LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
    • 我再次更新了我的代码。请参见上文。我也尝试了你所说的,但它在同一个目录中产生了两个不同的输出文件。我想要两个不同名称的输出目录
    • 您可以使用以下语句指定基本路径目录:- mout.write("output1", key, new IntWritable(count),"outputDir1/output1"); mout.write("output2", key, new IntWritable(count),"outputDir2/output2");还要避免在reduce方法中实例化Writable对象。将其定义为类对象并使用setter方法重用该对象。
    • 好的。我根据您的建议更改了它 mout.write("output1", key, new IntWritable(count),"multipleoutputs1/");mout.write("output2", key, new IntWritable(count),"multipleoutputs2/") ;我得到两个目录,但在父目录中创建了一个额外的空文件。我正在使用 hadoop 0,20 cdh3 版本。从哪个版本可以使用lazyoutputformat
    猜你喜欢
    • 1970-01-01
    • 2015-12-25
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多