package com.movie.step;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import com.movie.arith.SortHashMap;
/**
*
* @author yourkkc
* filter qu diao unuser yong de dianying
*/
public class ScoreFilter {
//input collect
public static class TheMapper extends Mapper<Object, Text, Text, Text>{
private String path;
private static final Text k = new Text();
private static final Text v = new Text();
@Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
FileSplit inputSplit = (FileSplit) context.getInputSplit();
path = inputSplit.getPath().getParent().getName();
String type ="";
if(path.equals("step4_Out")){//plus to the result of matrix to multiply
type ="W:";
}else if(path.equals("step42_Out")){//src
type ="S:";
}
Text k = new Text(key.toString());
Text v = new Text(type+value.toString());
context.write(k,v);
}
}
public static class TheReduce extends Reducer<Text,Text,Text,Text>{
@Override
protected void reduce(Text key, Iterable<Text> values,
Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
HashMap<String,String> wMap = new HashMap<String,String>();
HashMap<String,String> sMap = new HashMap<String,String>();
for(Text value:values){
String v = value.toString().substring(2);
if (value.toString().startsWith("W:")) {//W
wMap.put(v.split(":")[0], v.split(":")[1]);
}else if (value.toString().startsWith("S:")){// S
sMap.put(v.split(":")[0], v.split(":")[1]);
}
}
//过滤
HashMap<String,Float> filterMap = new HashMap<String,Float>();
Iterator<String> iter = wMap.keySet().iterator();
while(iter.hasNext()){
String k = iter.next();//item
if(!sMap.containsKey(k))
filterMap.put(k, Float.valueOf(wMap.get(k)));
}
//排序
List<Entry<String,Float>> list = new LinkedList<Entry<String,Float>>();
list=SortHashMap.sortHashMap(filterMap);
for(Entry<String,Float> l : list){
Text k = new Text(key);
Text v = new Text(l.getKey().toString() + "\t" + l.getValue().toString());
context.write(k,v);
}
}
}
}