hadoop----mapreduce的案例(三)(自定义InputFormat)

该博客介绍了如何在Hadoop MapReduce中自定义InputFormat,通过MyRecordReader和MyInputFormat类实现奇数行作为key,偶数行作为value的功能。内容包括map函数的实现和驱动类的配置,最终展示了运行结果。
摘要由CSDN通过智能技术生成

案例三

将奇数行记录转成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);
	}
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值