组合文件输入格式始终只启动一张地图 Hadoop 1.2.1

2024-01-14

我正在尝试使用测试CombineFileInputFormat 来处理每个8 MB 的几个小文件(20 个文件)。我按照this中给出的示例进行操作blog http://yaseminavcular.blogspot.in/2011_03_01_archive.html。我能够实施并测试它。最终结果是正确的。但令我惊讶的是,它最终总是只有一张地图。我尝试将属性“mapred.max.split.size”设置为16MB、32MB等各种值(当然以字节为单位),但没有成功。我还需要做什么或者这是正确的行为吗?

我正在运行一个默认复制为 2 的两节点集群。下面给出的是开发的代码。非常感谢任何帮助。

package inverika.test.retail;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import org.apache.hadoop.mapreduce.Reducer;

public class CategoryCount {

    public static class CategoryMapper
        extends Mapper<LongWritable, Text, Text, IntWritable>    {

        private final static IntWritable one = new IntWritable(1);
        private String[] columns = new String[8];

        @Override
        public void map(LongWritable key, Text value, Context context)
                throws     IOException, InterruptedException {
            columns = value.toString().split(",");  
            context.write(new Text(columns[4]), one);
        }
    }

    public static class CategoryReducer
        extends Reducer< Text, IntWritable, Text, IntWritable>    {

        @Override
        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));
        }
    }

    public static void main(String args[]) throws Exception    {
        if (args.length != 2)  {
                System.err.println("Usage: CategoryCount <input Path> <output Path>");
                System.exit(-1);
        } 

        Configuration conf = new Configuration();
        conf.set("mapred.textoutputformat.separator", ",");
        conf.set("mapred.max.split.size", "16777216");   // 16 MB

        Job job = new Job(conf, "Retail Category Count");
        job.setJarByClass(CategoryCount.class);
        job.setMapperClass(CategoryMapper.class);
        job.setReducerClass(CategoryReducer.class);
        job.setInputFormatClass(CombinedInputFormat.class);
        //CombineFileInputFormat.setMaxInputSplitSize(job, 16777216);
        CombinedInputFormat.setMaxInputSplitSize(job, 16777216);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.addInputPath(job, new Path(args[0]) );
        FileOutputFormat.setOutputPath(job, new Path(args[1]) );
        //job.submit();
        //System.exit(job.waitForCompletion(false) ?  0 : 1);
        System.exit(job.waitForCompletion(true) ?  0 : 1);
    }
}

这是实现的组合 FileInputFormat

package inverika.test.retail;

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;
import org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;

public class CombinedInputFormat extends CombineFileInputFormat<LongWritable, Text> {

    @Override
    public RecordReader<LongWritable, Text>
            createRecordReader(InputSplit split, TaskAttemptContext context)
                    throws IOException {

        CombineFileRecordReader<LongWritable, Text> reader = 
                new CombineFileRecordReader<LongWritable, Text>(
                        (CombineFileSplit) split, context, myCombineFileRecordReader.class);        
        return reader;
    }

    public static class myCombineFileRecordReader extends RecordReader<LongWritable, Text> {
        private LineRecordReader lineRecordReader = new LineRecordReader();

        public myCombineFileRecordReader(CombineFileSplit split, 
                TaskAttemptContext context, Integer index) throws IOException {

            FileSplit fileSplit = new FileSplit(split.getPath(index), 
                                                split.getOffset(index),
                                                split.getLength(index), 
                                                split.getLocations());
            lineRecordReader.initialize(fileSplit, context);
        }

        @Override
        public void initialize(InputSplit inputSplit, TaskAttemptContext context)
                throws IOException, InterruptedException {
            //linerecordReader.initialize(inputSplit, context);
        }

        @Override
        public void close() throws IOException {
            lineRecordReader.close();
        }

        @Override
        public float getProgress() throws IOException {
            return lineRecordReader.getProgress();
        }

        @Override
        public LongWritable getCurrentKey() throws IOException,
                InterruptedException {
            return lineRecordReader.getCurrentKey();
        }

        @Override
        public Text getCurrentValue() throws IOException, InterruptedException {
            return lineRecordReader.getCurrentValue();
        }

        @Override
        public boolean nextKeyValue() throws IOException, InterruptedException {
            return lineRecordReader.nextKeyValue();
        }        
    }
}

使用时需要设置最大分割大小CombineFileInputFormat作为输入格式类。或者你可能会得到准确的ONLY ONE当所有块都来自同一机架时映射器。

您可以通过以下方式之一实现此目的:

  • 打电话给CombineFileInputFormat.setMaxSplitSize() https://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/mapreduce/lib/input/CombineFileInputFormat.html#setMaxSplitSize(long) method
  • set mapreduce.input.fileinputformat.split.maxsize or mapred.max.split.size(deprecated) configuration parameter
    For exmaple, by issuing the following call

    job.getConfiguration().setLong("mapreduce.input.fileinputformat.split.maxsize", (long)(256*1024*1024));
    

    您将最大分割大小设置为 256MB。


参考:

  • https://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/mapreduce/lib/input/CombineFileInputFormat.html https://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/mapreduce/lib/input/CombineFileInputFormat.html
  • http://mail-archives.apache.org/mod_mbox/hadoop-common-user/201004.mbox/%[电子邮件受保护]%3E http://mail-archives.apache.org/mod_mbox/hadoop-common-user/201004.mbox/%3C35374.30384.qm@web63402.mail.re1.yahoo.com%3E
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

组合文件输入格式始终只启动一张地图 Hadoop 1.2.1 的相关文章

随机推荐