Hadoop Mapreduce:自定义输入格式

时间:2014-09-20 10:54:54

标签: hadoop mapreduce

我有一个包含文本数据的文件和" ^"介于两者之间:

一些文字^走到这里^
还有一点^更多
走到这里

我正在编写一个自定义输入格式来使用" ^"来界定行。字符。即映射器的输出应该是:

一些文字
走到这里
和一个很少的 更多的事情在这里

我编写了一个自定义输入格式,它扩展了FileInputFormat,还编写了一个扩展RecordReader的自定义记录阅读器。我的自定义记录阅读器的代码如下。我不知道如何继续这个代码。在WHILE循环部分中遇到了nextKeyValue()方法的问题。我应该如何从拆分中读取数据并生成自定义键值?我正在使用所有新的mapreduce包而不是旧的mapred包。

public class MyRecordReader extends RecordReader<LongWritable, Text>
    {
        long start, current, end;
        Text value;
        LongWritable key;
        LineReader reader;
        FileSplit split;
        Path path;
        FileSystem fs;
        FSDataInputStream in;
        Configuration conf;

        @Override
        public void initialize(InputSplit inputSplit, TaskAttemptContext cont) throws IOException, InterruptedException
        {
            conf = cont.getConfiguration();
            split = (FileSplit)inputSplit;
            path = split.getPath();
            fs = path.getFileSystem(conf);
            in = fs.open(path);
            reader = new LineReader(in, conf);
            start = split.getStart();
            current = start;
            end = split.getLength() + start;
        }

        @Override
        public boolean nextKeyValue() throws IOException
        {
            if(key==null)
                key = new LongWritable();

            key.set(current);
            if(value==null)
                value = new Text();

            long readSize = 0;
            while(current<end)
            {
                Text tmpText = new Text(); 
                readSize = read //here how should i read data from the split, and generate key-value?

                if(readSize==0)
                    break;

                current+=readSize;              
            }

            if(readSize==0)
            {
                key = null;
                value = null;
                return false;
            }

            return true;

        }

        @Override
        public float getProgress() throws IOException
        {

        }

        @Override
        public LongWritable getCurrentKey() throws IOException
        {

        }

        @Override
        public Text getCurrentValue() throws IOException
        {

        }

        @Override
        public void close() throws IOException
        {

        }


    }

1 个答案:

答案 0 :(得分:9)

没有必要自己实现。您只需将配置值textinputformat.record.delimiter设置为抑扬符号即可。

conf.set("textinputformat.record.delimiter", "^");

这应该可以正常使用TextInputFormat