解题思路:
map输出key value是什么?
reduce输出key value是什么?
package com.nefu.zhangna.reducejoin;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class ScoreBeann implements Writable {
private String uid;
private String sid;
private int score;
private String name;
private String flag;
public ScoreBeann() {
}
public String getUid(){
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(uid);
out.writeUTF(sid);
out.writeInt(score);
out.writeUTF(name);
out.writeUTF(flag);
}
@Override
public void readFields(DataInput in) throws IOException {
this.uid=in.readUTF();
this.sid=in.readUTF();
this.score=in.readInt();
this.name=in.readUTF();
this.flag=in.readUTF();
}
@Override
public String toString(){
return "uid="+this.uid+"\t"+"name:"+this.name+"\t"+"score"+this.score;
}
}
ScoreMapper
package com.nefu.zhangna.reducejoin;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import java.io.IOException;
public class ScoreMapper extends Mapper<LongWritable, Text,Text, ScoreBeann>{
private Text outk=new Text();
private ScoreBeann outv=new ScoreBeann();
private String filename;
@Override
protected void setup(Context context){
FileSplit split=(FileSplit) context.getInputSplit();
filename=split.getPath().getName();
}
public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
String line=value.toString();
if (filename.contains("score")){
String[] sp=line.split("\t");
outk.set(sp[1]);
outv.setSid(sp[1]);
outv.setUid(sp[0]);
outv.setName("");
outv.setScore(Integer.parseInt(sp[2]));
outv.setFlag("score");
}else {
String[] sp1=line.split("\t");
outk.set(sp1[0]);
outv.setUid("");
outv.setName(sp1[1]);
outv.setScore(0);
outv.setSid(sp1[0]);
outv.setFlag("name");
}
context.write(outk,outv);
}
}
ScoreReducer
package com.nefu.zhangna.reducejoin;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
public class ScoreReducer extends Reducer<Text, ScoreBeann,ScoreBeann,NullWritable> {
@Override
protected void reduce(Text key,Iterable<ScoreBeann> values,Context context) throws IOException, InterruptedException {
ArrayList<ScoreBeann> scoreBeanns=new ArrayList<ScoreBeann>();
ScoreBeann namebean=new ScoreBeann();
for (ScoreBeann value:values){
if("score".equals(value.getFlag())){
ScoreBeann tmpbean=new ScoreBeann();
try {
BeanUtils.copyProperties(tmpbean,value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
scoreBeanns.add(tmpbean);
}else {
try {
BeanUtils.copyProperties(namebean,value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
for(ScoreBeann scoreBeann:scoreBeanns){
scoreBeann.setName(namebean.getName());
context.write(scoreBeann,NullWritable.get());
}
}
}
ScoreDriver
package com.nefu.zhangna.maxcount;
import com.nefu.zhangna.reducejoin.ScoreBeann;
import com.nefu.zhangna.reducejoin.ScoreMapper;
import com.nefu.zhangna.reducejoin.ScoreReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.File;
import java.io.IOException;
public class ScoreDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration configuration=new Configuration();
Job job=Job.getInstance(configuration);
job.setJarByClass(ScoreDriver.class);
job.setMapperClass(ScoreMapper.class);
job.setReducerClass(ScoreReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(ScoreBeann.class);
job.setOutputKeyClass(ScoreBeann.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job,new Path("D:\\cluster\\input"));
FileOutputFormat.setOutputPath(job,new Path("D:\\cluster\\score"));
boolean result=job.waitForCompletion(true);
System.exit(result?0:1);
}
}