【问题标题】:hadoop CustomInputFormat Not getting invokedhadoop CustomInputFormat 没有被调用
【发布时间】:2023-03-31 12:38:01
【问题描述】:

我已经编写了自定义输入格式并在工作中进行了配置。仍然没有调用 inputformat。我在运行代码时保留了一些 SOP 来打印,但没有一个打印。即使我在驱动程序类中评论自定义输入格式,输出仍然保持不变。我错过了哪里?

驱动类

public class TestDriver {

    public static void main(String args[]) throws IOException, InterruptedException, ClassNotFoundException{

        Configuration conf = new Configuration();
        Job job = new Job(conf,"Custom Format");
        job.setMapperClass(CustomInputFormatmapper.class);
        job.setReducerClass(CustomInputFormatReducer.class);
        job.setInputFormatClass(CustomInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.getConfiguration().set("fs.file.impl", "com.learn.WinLocalFileSystem");
        String inputPath="In\\VISA_Details.csv";
        Path inPath=new Path(inputPath);
        String outputPath = "C:\\Users\\Desktop\\Hadoop learning\\output\\run1";
        Path outPath=new Path(outputPath);

        FileInputFormat.setInputPaths(job, inPath );
        FileOutputFormat.setOutputPath(job, outPath);

        System.out.println(job.waitForCompletion(true));


    }
}

自定义输入格式

    import org.apache.hadoop.mapred.TaskAttemptContext;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;

public class CustomInputFormat extends TextInputFormat{

    public RecordReader createRecordReader(InputSplit split, TaskAttemptContext context)
    {
        System.out.println(" ------------ INSIDE createRecordReader()--------------");
        return new CustomRecordReader();
    }
}

自定义录音机

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.util.LineReader;

public class CustomRecordReader extends RecordReader {

    private CompressionCodecFactory compressionCodecs;
    private final int NLINESTOPROCESS = 3;
    private long start;
    private long pos;
    private long end;
    private LineReader in;
    private int maxLineLength;
    private LongWritable key;
    private Text value;

    @Override
    public void close() throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public Object getCurrentKey() throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getCurrentValue() throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void initialize(InputSplit inputsplit,TaskAttemptContext taskattemptcontext) 
            throws IOException, InterruptedException {
        System.out.println(" ---------- INSIDE INITILISE:  THIS IS NOT PRINTING----------");
        FileSplit split = (FileSplit)inputsplit;
        Configuration job = taskattemptcontext.getConfiguration();
        maxLineLength = job.getInt("mapred.linerecordreader.maxlength", 2147483647);
        start = split.getStart();
        end = start + split.getLength();
        Path file = split.getPath();
        compressionCodecs = new CompressionCodecFactory(job);
        CompressionCodec codec = compressionCodecs.getCodec(file);
        FileSystem fs = file.getFileSystem(job);
        FSDataInputStream fileIn = fs.open(split.getPath());
        boolean skipFirstLine = false;
        if(codec != null)
        {
            in = new LineReader(codec.createInputStream(fileIn), job);
            end = 9223372036854775807L;
        } else
        {
            if(start != 0L)
            {
                skipFirstLine = true;
                start--;
                fileIn.seek(start);
            }
            in = new LineReader(fileIn, job);
        }
        if(skipFirstLine)
            start += in.readLine(new Text(), 0, (int)Math.min(2147483647L, end - start));
        pos = start;

    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {

        System.out.println(" ---------- INSIDE nextKeyValue()------------");
        if(key==null){
            key = new LongWritable();
        }
        if(value==null){
            value = new Text();
        }
        key.set(pos);
        value.clear();

        final Text newLine = new Text("\n");
        Text newVal = new Text();
         int newSize = 0;

        for(int i =0;i<NLINESTOPROCESS;i++){
             Text v = new Text();

             while(pos<end){
                 newSize = in.readLine(v, maxLineLength,Math.max((int)Math.min(Integer.MAX_VALUE, end-pos),maxLineLength));
                 value.append(v.getBytes(),0, v.getLength());
                 value.append(newLine.getBytes(),0, newLine.getLength());

                 if (newSize == 0) {
                        break;
                    }
                    pos += newSize;
                    if (newSize < maxLineLength) {
                        break;
                    }

             }
        }


        return false;
    }

}

映射器类

    import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CustomInputFormatmapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {

    public void map(LongWritable key, Text val, Context context)throws IOException, InterruptedException{

        String value = val.toString();
        String[] totalRows = value.split("\n");
        int count =totalRows.length;

        context.write(new LongWritable(Long.valueOf(count)), new LongWritable(1L));

    }
}

减速机类

    import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;

public class CustomInputFormatReducer extends Reducer<LongWritable, LongWritable, LongWritable, LongWritable> {

    public void reduce(LongWritable key, Iterable<LongWritable> val, Context context) throws IOException, InterruptedException{
        System.out.println(" --------REDUCER--------");
        long count =0;
        for(LongWritable vals: val){
            count++;
        }
        context.write(key, new LongWritable(count));
    }

}

【问题讨论】:

  • 我认为您认为它不起作用是因为您没有看到打印语句?
  • @DebD,你试过把job.setjarByClass(TestDriver.class)放进去吗?
  • @irW:我在 Eclipse 中而不是在 VM 中运行它。所以应该显示 SOP 语句。
  • @SSaikia_JtheRocker:我没有把它作为工作来运行,所以我不需要放那个东西。即使我这样说,自定义输入格式也不会被选中。
  • 也许你正在尝试的 Windows 文件系统有问题,这是我不熟悉的代码中唯一的地方?你能告诉我们这次运行的日志吗?

标签: hadoop mapreduce


【解决方案1】:

我正在回答我自己的问题,因为这将帮助其他人解决我面临的问题。我正在导入的包有问题。 提到我犯的错误。

自定义输入格式类

1) 错过了@Override 注释 2)从import org.apache.hadoop.mapred.InputSplit而不是org.apache.hadoop.mapreduce.InputSplit导入;

客户记录阅读器

1) 导入是从 org.apache.hadoop.mapred.* 而非 org.apache.hadoop.mapreduce.* 完成的;

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    相关资源
    最近更新 更多