案例三
将奇数行记录转成key,偶数行转成value从而进行输出
MyRecordReader
public class MyRecordReader extends RecordReader<Text, Text>{
private long start;
private long end;
private LineReader in;
private FSDataInputStream fileIn;
private Text key;
private Text value;
@Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
// TODO Auto-generated method stub
FileSplit filesplit = (FileSplit) split;
Path path = filesplit.getPath();
start = filesplit.getStart();
end = start+filesplit.getLength();
Configuration conf = context.getConfiguration();
FileSystem fs = path.getFileSystem(conf);
fileIn = fs.open(path);
fileIn.seek(start);//将开始读取的位置定位到当前split的start位置
in = new LineReader(fileIn);
}
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
// TODO Auto-generated method stub
if(key==null){
key = new Text();
}
if(in.readLine(key)==0){
return false;
}
if(value==null){
value = new Text();
}
in.readLine(value);
return true;
}
@Override
public Text getCurrentKey() throws IOException, InterruptedException {
// TODO Auto-generated method stub
return key;
}
@Override
public Text getCurrentValue() throws IOException, InterruptedException {
// TODO Auto-generated method stub
return value;
}
@Override
public float getProgress() throws IOException, InterruptedException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void close() throws IOException {
// TODO Auto-generated method stub
in.close();
}
}
MyInputFormat
public class MyInputFormat extends FileInputFormat<Text, Text>{
@Override
public RecordReader<Text, Text> createRecordReader(InputSplit split, TaskAttemptContext context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
return new MyRecordReader();
}
@Override
protected boolean isSplitable(JobContext context, Path filename) {
// TODO Auto-generated method stub
return true;
}
}
map函数
public class MyMapper extends Mapper<Text, Text, Text, Text>{
@Override
protected void map(Text key, Text value, Mapper<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
context.write(key, value);
}
}
驱动类
public class MyDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path = new Path("file:///E:/data/output");
if(fs.exists(path)){
fs.delete(path);
}
Job job = Job.getInstance();
job.setJobName("name");
job.setJarByClass(MyDriver.class);
job.setInputFormatClass(MyInputFormat.class);
job.setMapperClass(MyMapper.class);
job.setNumReduceTasks(0);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("file:///E:/data/input/name.txt"));
FileOutputFormat.setOutputPath(job, new Path("file:///E:/data/output"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
运行结果