码迷,mamicode.com
首页 > 移动开发 > 详细

按字段分组的Mapper

时间:2016-03-06 10:03:40      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

<strong><span style="font-size:18px;">/***
 * @author YangXin
 * @info 按字段分组的Mapper
 */
package unitTwelve;

import java.io.IOException;
import java.util.regex.Pattern;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class ByKeyMapper extends Mapper<LongWritable, Text, Text, Text>{
	private Pattern splitter = Pattern.compile("\t");
	private int selectedField = 1;
	private int groupByField = 0;
	
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
		String[] fields = splitter.split(value.toString());
		if(fields.length - 1 < selectedField || fields.length - 1 < groupByField){
			context.getCounter("Map", "LinesWithErrors").increment(1);
			return;
		}
		String oKey = fields[groupByField];
		String oValue = fields[selectedField];
		context.write(new Text(oKey), new Text(oValue));
	}
}
</span></strong>

按字段分组的Mapper

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/50811604

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!