//自定义MyMapper类继承Mapper
public class MyMapper extends Mapper<Object, Text, Text, IntWritable>{
//定义初始次数为1
private final static IntWritable one = new IntWritable(1);
//覆写map函数
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException{
String line = value.toString();
String array[] = line.split(",");//指定,为分隔符,组成数组
String keyOutput = array[1];//提取数组中的访问日期作为key
context.write(new Text(keyOutput), one);//组成键值对
}
}
//自定义MyReducer继承Reducer
public class MyReducer extends Reducer<Text, IntWritable,Text,IntWritable>{
private IntWritable result = new IntWritable();
//覆写reduce函数
public void reduce(Text key,Iterable<IntWritable>values,Context context)
throws IOException, InterruptedException{
int sum = 0;//定义累加器,初始值为0
for(IntWritable val : values) {//读取Mapper输出的键值对
sum+=val.get();//将相同键的所有值进行累加
}
result.set(sum);
context.write(key, result);//输出<访问日期,总访问次数>
}
}
//自定义MyMapper类继承Mapperpublic class MyMapper extends Mapper<Object, Text, Text, IntWritable>{//定义初始次数为1private final static IntWritable one = new IntWritable(1);//覆写map函数public void map(Object key, Text value, Context context) throws IOExcept.