按字段分组的Mapper

Posted 杨鑫newlfe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按字段分组的Mapper相关的知识,希望对你有一定的参考价值。

<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的主要内容,如果未能解决你的问题,请参考以下文章

mysql 按表达式或函数分组多个字段分组排序

按字段分组的Mapper

Pandas - 按字段分组并使用显示该字段数量的列

mysql按字段分组并获取每个分组按照某个字段排序的前三条

SQLAlchemy:如何按两个字段分组并按日期过滤

按字段分组的Reducer