我有一个文件,其中包含文本和"^"之间的数据:
一些文字^在这里走了^
并且很少^在
这里
我正在编写一个自定义输入格式来使用"^"字符分隔行.即映射器的输出应该是:
有些文字在
这里
,还有
更多的东西在这里
我编写了一个自定义输入格式,它扩展了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
{
}
}
Run Code Online (Sandbox Code Playgroud)
没有必要自己实现.您只需将配置值textinputformat.record.delimiter设置为抑扬符.
conf.set("textinputformat.record.delimiter", "^");
Run Code Online (Sandbox Code Playgroud)
这应该可以正常工作TextInputFormat.
| 归档时间: |
|
| 查看次数: |
4182 次 |
| 最近记录: |