评论管家系统

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);
            }
        }
        
    }
    
}
 

转载于:https://my.oschina.net/u/3581306/blog/1143052

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值