• 热门专题

向MapReduce转换:生成用户向量

作者:  发布日期:2016-02-23 21:04:13
Tag标签:向量  用户  
  • 分两部分:

     

    /***
     * @author YangXin
     * @date 2016/2/21
     * @ info 主要功能是mahout实现解析Wikipedia链接文件的Mapper接口
     */
    package unitSix;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.mahout.math.VarLongWritable;
    
    public class WikipediaToItemPrefsMapper extends Mapper<LongWritable, Text, VarLongWritable, VarLongWritable>{
    	private static final Pattern NUMBERS = Pattern.compile("(\d+)");
    	public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
    		String line = value.toString();
    		Matcher m = NUMBERS.matcher(line);
    		//定位用户ID
    		m.find();                              
    		VarLongWritable userID = new VarLongWritable(Long.parseLong(m.group()));
    		VarLongWritable itemID = new VarLongWritable();
    		while(m.find()){
    			itemID.set(Long.parseLong(m.group()));
    			//为每个物品ID生成用户-物品对
    			context.write(userID, itemID);
    		}
    	}
    }

     

     


    <strong>/***
     * @author YangXin
     * @info 功能是mahout实现从用户物品偏好中生成Vector的reducer接口
     */
    package unitSix;
    import java.io.IOException;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.mahout.math.RandomAccessSparseVector;
    import org.apache.mahout.math.VarLongWritable;
    import org.apache.mahout.math.Vector;
    import org.apache.mahout.math.VectorWritable;
    
    public class WikipediaToUserVectorReducer extends Reducer<VarLongWritable, VarLongWritable, VarLongWritable, VectorWritable>{
    	public void reduce(VarLongWritable userID, Iterable<VarLongWritable> itemPrefs, Context context) throws IOException, InterruptedException{
    		Vector userVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 100);
    		for(VarLongWritable itemPref : itemPrefs){
    			userVector.set((int)itemPref.get(), 1.0f);
    		}
    		context.write(userID, new VectorWritable(userVector));
    	}
    }
    </strong>

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规