需求
统计stu每个年龄段人数,将结果写入stu_res表
数据
stu表:
stu_res表:
代码
MySQLTest.java:
package mapReduceTest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.log4j.BasicConfigurator;
import java.io.IOException;
public class MySQLTest {
public static class MyMapper extends Mapper<LongWritable, Stu, IntWritable, IntWritable> {
@Override
protected void map(LongWritable key, Stu value, Context context) throws IOException, InterruptedException {
//System.out.println("----------------------------------------"+value.getName());
context.write(new IntWritable(value.getAge()), new IntWritable(1));
}
}
public static class MyReducer extends Reducer<IntWritable, IntWritable, Stu_res, NullWritable>{
@Override
protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
for(IntWritable iw : values){
count += iw.get();
}
Stu_res sr = <