<strong><span style= "FONT-SIZE:18PX;" >/*** * @author yangxin * @info grouped by field 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");p rivate int selectedfield = 1;private int groupbyfield = 0;protected void Map (longwritable key, Text value, Context context) throws IOException, interruptedexception{string[] fields = SPLITTER.SP Lit (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 Grouped by field