【问题标题】:Hadoop Mapreduce: Custom Input FormatHadoop Mapreduce:自定义输入格式
【发布时间】:2014-09-20 10:54:53
【问题描述】:

我有一个数据文件,其中包含文本和“^”:

一些文字^转到这里^
还有一些^更多
去这里

我正在编写自定义输入格式以使用“^”字符分隔行。即映射器的输出应该是这样的:

一些文字
去这里
还有一些
更多内容在这里

我编写了一个扩展 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
        {

        }


    }

【问题讨论】:

    标签: hadoop mapreduce


    【解决方案1】:

    没有必要自己实现。您可以简单地将配置值textinputformat.record.delimiter 设置为抑扬符。

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

    这应该适用于普通的TextInputFormat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多